9 Replies Latest reply on Nov 25, 2014 11:27 AM by Wolf-Dieter Fink

    jboss 7 ha singleton service - how do i provide arguments?

    Radai Rosenblatt Newbie

      i've created a simple singleton service heavily inspired by the one in the as7 testsuite:



      public class SingletonServiceBean implements Service<Boolean> {

          private final static Logger log = LoggerFactory.getLogger(SingletonServiceBean.class);


          public static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("j2eeshop", "SingletonServiceBean");

          private final AtomicBoolean started = new AtomicBoolean(false);

          private final InjectedValue<ServerEnvironment> env = new InjectedValue<ServerEnvironment>();




          public void start(StartContext context) throws StartException {

              log.error("SingletonServiceBean started");





          public void stop(StopContext context) {

              log.error("SingletonServiceBean stopped");





          public Boolean getValue() throws IllegalStateException, IllegalArgumentException {

              if (!this.started.get()) {

                  throw new IllegalStateException();


              log.error("SingletonServiceBean called on {}", env.getValue().getNodeName());

              return null;



          public Injector<ServerEnvironment> getEnvInjector() {

              return this.env;



          public String sayHelloTo(String name) {

              log.error("SingletonServiceBean saying hi to {} on {}", name, env.getValue().getNodeName());

              return "hello, "+name+"!";




      i then start and expose this service as a cluster-wide singleton using a @Startup @Singleton like this:





      public class SingletonServicesAccessBean implements SingletonServicesAccess {

          private final Logger log = LoggerFactory.getLogger(SingletonServicesAccessBean.class);



          public void startup() {

              log.error("starting up");


              SingletonServiceBean service = new SingletonServiceBean();

              SingletonService<Boolean> singleton = new SingletonService<Boolean>(service, SingletonServiceBean.SERVICE_NAME);

              singleton.setElectionPolicy(new SimpleSingletonElectionPolicy());

              ServiceController<Boolean> controller = singleton.build(ServiceContainerHelper.getCurrentServiceContainer())

                      .addDependency(ServerEnvironmentService.SERVICE_NAME, ServerEnvironment.class, service.getEnvInjector())




              try {

                  log.error("starting singleton controller");


              } catch (StartException e) {

                  throw new IllegalStateException(e);






          public void shutdown() {

              log.error("shutting down");





          public String sayHelloTo(String name) {

              SingletonService<Boolean> singletonServiceWrapper = (SingletonService<Boolean>) CurrentServiceContainer.getServiceContainer().getService(SingletonServiceBean.SERVICE_NAME).getService();

              //what do i do now?



      my issue is that i want my service to have a method with arguments (the sayHelloTo(name) method).

      the service interface only allos for getValue()...


      so my question is - how do i write a cluster-wide singleton service that can recieve method arguments ?