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:

       

      {code}

      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>();

       

       

          @Override

          public void start(StartContext context) throws StartException {

              log.error("SingletonServiceBean started");

              started.set(true);

          }

       

          @Override

          public void stop(StopContext context) {

              log.error("SingletonServiceBean stopped");

              started.set(false);

          }

       

          @Override

          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+"!";

          }

      }

      {code}

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

      {code}

      @Singleton

      @Startup

      @Local(SingletonServicesAccess.class)

      public class SingletonServicesAccessBean implements SingletonServicesAccess {

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

       

          @PostConstruct

          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())

                      .setInitialMode(ServiceController.Mode.ACTIVE)

                      .install()

                      ;

              try {

                  log.error("starting singleton controller");

                  ServiceContainerHelper.start(controller);

              } catch (StartException e) {

                  throw new IllegalStateException(e);

              }

       

          }

       

          @PreDestroy

          public void shutdown() {

              log.error("shutting down");

              ServiceContainerHelper.remove(ServiceContainerHelper.getCurrentServiceContainer().getRequiredService(SingletonServiceBean.SERVICE_NAME));

          }

       

          @Override

          public String sayHelloTo(String name) {

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

              //what do i do now?

          }

      {code}

      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 ?