6 Replies Latest reply on Mar 14, 2014 5:34 PM by and and

    cluster-ha-singleton from quickstart and ejb annotation

    and and Newbie

      Hi,

      I downloaded quickstarts from wildfly/quickstart · GitHub and I wanted to change class HATimerService to get Scheduler from JNDI by @EJB annotation not by InitialContext.

       

      @EJB
      private Scheduler scheduler;
      

       

      to use it scheduler.initialize(), scheduler.stop() instead of

       

      InitialContext ic = new InitialContext();
                  ((Scheduler) ic.lookup("global/wildfly-cluster-ha-singleton-service/SchedulerBean!org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler")).initialize(......);
      

       

      After that change  HATimerService can not start due to scheduler is null and HATimerService.start can not invoke scheduler.initialize().

      Is this any possibility to use annotations to inject Scheduler and not use InitialContext?

      If not, please explain me why I can not use annotations in this example.

        • 1. Re: cluster-ha-singleton from quickstart and ejb annotation
          Paul Ferraro Master

          That's because Scheduler is not a defined business interface of SchedulerBean.  So, either Scheduler need to be annotated with @Local, or SchedulerBean needs to be annotated with @Local(Scheduler.class).  We can fix that.

          • 2. Re: Re: cluster-ha-singleton from quickstart and ejb annotation
            and and Newbie

            Thanks for your answer but it didn't help.

            Still can't inject scheduler ejb.

             

            2014-03-13 21:19:36,096 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-7) MSC000001: Failed to start service jboss.quickstart.ha.singleton.default.service: org.jboss.msc.service.StartException in service jboss.quickstart.ha.singleton.default.service: Could not initialize timer
              at org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.HATimerService.start(HATimerService.java:67)
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
              at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
            Caused by: java.lang.NullPointerException
              at org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.HATimerService.start(HATimerService.java:65)
              ... 5 more
            
            
            2014-03-13 21:19:36,097 ERROR [org.wildfly.clustering.server.singleton] (ServerService Thread Pool -- 81) JBAS010344: Failed to start jboss.quickstart.ha.singleton.default.service service: org.jboss.msc.service.StartException in service jboss.quickstart.ha.singleton.default.service: Could not initialize timer
              at org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.HATimerService.start(HATimerService.java:67)
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
              at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
              at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
            Caused by: java.lang.NullPointerException
              at org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.HATimerService.start(HATimerService.java:65)
              ... 5 more
            
            

             

            And my code:

             

            public class HATimerService implements Service<Environment> {
                public static final ServiceName DEFAULT_SERVICE_NAME = ServiceName.JBOSS.append("kndzr", "ha", "singleton", "default");
                public static final ServiceName QUORUM_SERVICE_NAME = ServiceName.JBOSS.append("kndzr", "ha", "singleton", "quorum");
                public static final String NODE_1 = "server-one";
                public static final String NODE_2 = "server-two";
            
            
                private static final Logger LOGGER = Logger.getLogger(HATimerService.class);   
                private final Value<ServerEnvironment> env;
                private final AtomicBoolean started = new AtomicBoolean(false);
            
            
                @EJB
                private SchedulerBean scheduler;
            
            
                public HATimerService(Value<ServerEnvironment> env) {
                    this.env = env;
                }
            
            
                @Override
                public Environment getValue() {
                    if (!this.started.get()) {
                        throw new IllegalStateException();
                    }
                    return new Environment(this.env.getValue().getNodeName());
                }
            
            
                @Override
                public void start(StartContext context) throws StartException {
                    if (!started.compareAndSet(false, true)) {
                        throw new StartException("The service is still started!");
                    }
                    LOGGER.info("Start HASingleton timer service '" + this.getClass().getName() + "'");
                   
                    try {
                        scheduler.initialize("HASingleton timer @" + this.env.getValue().getNodeName() + " " + new Date());
                    } catch (Exception e) {
                        throw new StartException("Could not initialize timer", e);
                    }
                }
            
            
                @Override
                public void stop(StopContext context) {
                    if (!started.compareAndSet(true, false)) {
                        LOGGER.warn("The service '" + this.getClass().getName() + "' is not active!");
                    } else {
                        LOGGER.info("Stop HASingleton timer service '" + this.getClass().getName() + "'");
                        try {
                            scheduler.stop();
                        } catch (Exception e) {
                            LOGGER.error("Could not stop timer", e);
                        }
                    }
                }
            }
            
            

             

            @Singleton
            @Local(Scheduler.class)
            public class SchedulerBean implements Scheduler {
                private static Logger LOGGER = Logger.getLogger(SchedulerBean.class);
                @Resource
                private TimerService timerService;
            
            
                @Timeout
                public void scheduler(Timer timer) {
                    LOGGER.info("HASingletonTimer: Info=" + timer.getInfo());
                }
            
            
                @Override
                public void initialize(String info) {
                    ScheduleExpression sexpr = new ScheduleExpression();
                    // set schedule to every 10 seconds for demonstration
                    sexpr.hour("*").minute("*").second("0/10");
                    // persistent must be false because the timer is started by the HASingleton service
                    timerService.createCalendarTimer(sexpr, new TimerConfig(info, false));
                }
            
            
                @Override
                public void stop() {
                    LOGGER.info("Stop all existing HASingleton timers");
                    for (Timer timer : timerService.getTimers()) {
                        LOGGER.trace("Stop HASingleton timer: " + timer.getInfo());
                        timer.cancel();
                    }
                }
            }
            
            • 3. Re: cluster-ha-singleton from quickstart and ejb annotation
              Paul Ferraro Master

              Sorry - I should have read your initial question more thoroughly... I didn't realize the ejb lookup was coming from an MSC service.  MSC services cannot resolve @EJB annotations.

              • 4. Re: cluster-ha-singleton from quickstart and ejb annotation
                and and Newbie

                So the only possibility to get bean in MSC service is to use InitialContext?