13 Replies Latest reply on Nov 28, 2012 7:52 AM by xiang yingbing

    Ability to use alternate persistent stores for EJB timers

    Ifta Khirul Newbie

      Can I store persistent timers in a JDBC database in Jboss 7.1.1 Final ?

      We have done that in Jboss 5. But not getting a way to do that in Jboss 7.

       

      Is there any way to do that ? If yes then how ?

      If no, then what are the alternate way to do that ?

        • 1. Re: Ability to use alternate persistent stores for EJB timers
          xiang yingbing Master

          Here is and ejb timer example in JBoss AS 7.

          I do NOT think it is difficult for you to save timer info. by yourself and construct it during JBOSS AS 7 Start.

           

           

           

           

           

          package com.ybxiang.nms.ejb.timer;

           

          import javax.annotation.PostConstruct;

          import javax.annotation.Resource;

          import javax.ejb.EJB;

          import javax.ejb.Local;

          import javax.ejb.SessionContext;

          import javax.ejb.Singleton;

          import javax.ejb.Startup;

          import javax.ejb.Timeout;

          import javax.ejb.TimerConfig;

          import javax.ejb.TimerService;

          import javax.persistence.EntityManager;

          import javax.persistence.PersistenceContext;

           

          import org.jboss.logging.Logger;

           

          import com.ybxiang.nms.ejb.service.master.INmsCoreService;

          import com.ybxiang.nms.ejb.session.secure.ILocalAfnSession;

          import com.ybxiang.nms.ejb.session.secure.ISecuredRemoteSession;

           

          @Local(IAfn0cF25HistoryResponseCheckTimer.class)

          @Singleton

          @Startup

          public class Afn0cF25HistoryResponseCheckTimer implements IAfn0cF25HistoryResponseCheckTimer{

              Logger log = Logger.getLogger(Afn0cF25HistoryResponseCheckTimer.class);   

             

              @SuppressWarnings("unused")

              @Resource

              private SessionContext sessionContext;

             

              @Resource TimerService timerService;

             

              @PersistenceContext

              protected EntityManager em;

             

              @EJB

              INmsCoreService nmsCoreService;

              //@EJB

              //ISecuredRemoteSession securedRemoteSession;

              @EJB

              ILocalAfnSession localAfnSession;

           

              @PostConstruct

              public void createTimer() {

                  log.info("==>starting Afn0cF25HistoryResponseCheckTimer timer...");

                  //final TimerService timerService = sessionContext.getTimerService();

                  timerService.createIntervalTimer(

                            10000L,

                            nmsCoreService.getAfn0cF25ResponseCheckTimerPeriod(),

                            new TimerConfig(null,false)

                            );       

                  log.info("==>timer Afn0cF25HistoryResponseCheckTimer started!!!");

              }

           

              @Timeout

              public void timeout( ){

                  log.info("@@@Afn0cF25HistoryResponseCheckTimer timeout@@@");

                  try{

                      localAfnSession.findAfn0CPnF25ResponseForAfn0cF25History();

                  }catch(Exception e){

                      log.error(e);

                  }

              }

          }

          • 2. Re: Ability to use alternate persistent stores for EJB timers
            Ifta Khirul Newbie

            Hi,

             

            Thanks for your quick reply. Your sample code is not clear to me.

            How are you storing your information and how you are retriving it ?

             

            I want to store a java object in database through timer service. And will read it during timer startup.

             

            Thanks.

            • 3. Re: Ability to use alternate persistent stores for EJB timers
              Nicklas Karlsson Master

              So you are not looking for a way to move timer storage from /standalone/data/timer-service-data to the DB, you are looking for a way to execute (by a timer) a Java class file that has been stored in the DB?

              • 4. Re: Ability to use alternate persistent stores for EJB timers
                Ifta Khirul Newbie

                "So you are not looking for a way to move timer storage from /standalone/data/timer-service-data to the DB"

                - Yes, you are right. But if there is a way to do that  it will be great if you can share me the idea.

                 

                "you are looking for a way to execute (by a timer) a Java class file that has been stored in the DB"

                - Yes, you are right.

                 

                I can do the schedule operation using @Schedule annotation of javax.ejb.Schedule and also with javax.ejb.TimerService .

                What is the main difference and similarity between them.

                And when I will use @Schedule and when I will use Timer Service.

                 

                 

                Thanks in advance.

                • 5. Re: Ability to use alternate persistent stores for EJB timers
                  Stephen Coy Master

                  Hi there,

                   

                  I'm curious to discover the purpose of your question. Normally, this would be an implementation detail and EJB timers should "just work" for you.

                  • 6. Re: Ability to use alternate persistent stores for EJB timers
                    Ifta Khirul Newbie

                    Ok. Can you please explain the later part of my question ?

                    Whats the main differnece between @Schedule and Timer Service.

                    • 7. Re: Ability to use alternate persistent stores for EJB timers
                      Nicklas Karlsson Master

                      I think the underlying implementation is about the same. With @Schedule it's "fire and forget" but by using the TimerService you can store handles yourself etc. In your case I would probably do it with annotations. Your current code probably has some special classloader that works on a byte[] fetched from the DB so that one might work directly.

                       

                      You can find some more information on timers here: http://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html

                       

                      Message was edited by: Nicklas Karlsson

                      • 8. Re: Ability to use alternate persistent stores for EJB timers
                        Stephen Coy Master

                        You use the TimerService to create and/or access existing timers.

                         

                        When you use @Schedule, the timer is automatically created for you. However, you then only have the opportunity to set the timerInfo to a static String value.

                         

                        You can declare an @Schedule method with a Timer argument so that you can get access to your timer info String.

                         

                        {code:java}@Schedule(..., info="some static value")

                        void handleTimeOut(Timer timer) {

                             String info = (String)timer.getInfo();

                        }{code}

                         

                        which may seem a bit pointless until you realise that a single method can have mutliple @Schedule annotations associated with it.

                         

                        It's still not clear why you care how your timer is persisted.

                        • 9. Re: Ability to use alternate persistent stores for EJB timers
                          xiang yingbing Master

                          Please study the API yourself:

                                   timerService.createIntervalTimer(...)

                           

                           

                          Why do you expect we do everything for you?

                          • 10. Re: Ability to use alternate persistent stores for EJB timers
                            Ifta Khirul Newbie

                            ya, I have already done that and now I know the difference.

                            And now if someone in the forum ask the question then I'm going to explain that.

                            • 11. Re: Ability to use alternate persistent stores for EJB timers
                              xiang yingbing Master

                              two methods:

                               

                              1. you can get timer info through timerService.createIntervalTimer(...) method.

                               

                              @PostConstruct
                              public void createTimer() {
                                  log.info("==>starting Afn0cF25HistoryResponseCheckTimer timer...");
                                  //fetch your Timer initial info. with EntityManager from DB here, then create the timer with bellow metthod:
                                  timerService.createIntervalTimer(
                                            10000L,
                                            nmsCoreService.getAfn0cF25ResponseCheckTimerPeriod(),
                                            new TimerConfig(null,false)
                                            );
                                  log.info("==>timer Afn0cF25HistoryResponseCheckTimer started!!!");
                              }

                               

                               

                               

                              2. I guess you can use TimerConfig(...) to fetch timer initial info. :

                                     public TimerConfig(java.io.Serializable info, boolean persistent);