4 Replies Latest reply on Mar 24, 2014 5:51 AM by Steam s

    Porblem in Ejb3 using TimerService with @Singleton and @Management

    Steam s Newbie

      Hi All,

       

      I am trying to understand an old code written by a colleague who has left the company now.

       

      The problem is about calling a start() method, which should then initialize a timeerservice and further invokes a method.

       

      The code was presumably working fine earlier on jboss 5, but since we have now migrated to jboss 6, it does not seems to work.

       

      The EJB is as follows:

       

      package com.abc.potsdam.middleware.cache;
      
      
      import java.util.StringTokenizer;
      
      
      import javax.annotation.Resource;
      import javax.ejb.EJB;
      import javax.ejb.SessionContext;
      import javax.ejb.Singleton;
      import javax.ejb.Stateless;
      import javax.ejb.Timeout;
      import javax.ejb.Timer;
      import javax.ejb.TimerService;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.TransactionAttributeType;
      
      
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import org.jboss.annotation.ejb.Service;
      
      
      import com.traveltainment.potsdam.common.configuration.ConfigService;
      
      
      @Singleton
      @Stateless
      @TransactionAttribute(TransactionAttributeType.NEVER)
      public class CacheLoaderBean implements CacheLoader, CacheLoaderManagement {
        private Log log = LogFactory.getLog(this.getClass());
      
      
        @EJB(mappedName = "/ConfigServiceBean/local")
        ConfigService configService;
      
      
        @EJB
        DroolsSession droolsSession;
      
      
        @Resource
        private SessionContext sessionContext;
      
      
        /**
        * after 10000 ms starting of loading persistence units
        * 
        * @param timer
        */
        @Timeout
        public void startTimeout(Timer timer) {
        loadPersistenceUnitsToCache();
        }
      
      
        public void loadPersistenceUnitsToCache() {
        String persistenceUnits = configService
        .getProperty("AvailablePersistenceUnits");
        log.info("reloading persistence units: " + persistenceUnits);
      
      
        StringTokenizer stringTokenizer = new StringTokenizer(persistenceUnits,
        ",");
      
      
        while (stringTokenizer.hasMoreTokens()) {
        String persistenceUnitName = stringTokenizer.nextToken();
      
      
        droolsSession.initSession(persistenceUnitName);
        }
        }
      
      
        public void start() {
        // create timer for 10000 ms to start loading cache
        TimerService timerService = sessionContext.getTimerService();
        Timer timer = timerService.createTimer(10000, "cache loading timer");
        }
      }
      

       

      The imported interfaces are:

       

      package com.abc.potsdam.middleware.cache;
      
      
      import org.jboss.ejb3.annotation.Management;
      
      
      @Management
      
      
      public interface CacheLoaderManagement
      {
        public void loadPersistenceUnitsToCache();
        
        public void start();
      }
      

       

      package com.abc.potsdam.middleware.cache;
      
      
      import javax.ejb.Local;
      
      
      import org.jboss.annotation.ejb.Management;
      
      
      @Local
      public interface CacheLoader
      {
        public void loadPersistenceUnitsToCache();
      }
      

       

      The problem is that in my implementation bean, I don't see the start() method ever getting executed. If it is to be invoked via JMX-Console then also I am a bit unaware as to how I can do that?

       

      Since the start method never gets executed, the rest of the functionality is also never invoked.

       

      I have done a bit of research on the topic, and found out at some threads that probably @Singleton and @Management have some issues together when using jboss 6?

       

      The compilation is all done fine, it is only then the code never gets executed and I don't see the log messages appearing.

       

      Could some please give me some pointers as to how I can set a timer for my scenario here?

       

      Thanks.

        • 1. Re: Porblem in Ejb3 using TimerService with @Singleton and @Management
          Wolf-Dieter Fink Master

          What you try to achieve? And what version of JBoss do you use, did you migrate to JBossAS6 - this is an old outdated version and it dosn't make sense - you should migrate to WildFly 8.0.0.Final.

           

          From the code @Stateless and @Singleton seems wrong the bean type is only one of it.

          • 2. Re: Porblem in Ejb3 using TimerService with @Singleton and @Management
            Steam s Newbie

            Thanks for replying, Wolf.

             

            My main objective is to execute the loadPersistenceUnitsToCache() every 10 seconds automatically.

             

            I have now removed the annotation @Stateless, but still no effect. The start method is not executed and am not sure if it should be executed through jmx-console or it gets executed automatically by the application server at deployment time?

             

            Yes the entire application is migrated from Jboss 5.1 to jboss-6.1.0.Final.

             

            Other parts of the application are working fine, but this one.

             

            Thanks.

            • 3. Re: Porblem in Ejb3 using TimerService with @Singleton and @Management
              Steam s Newbie

              I have now created a very simple Stateless bean, which is utilizing JavaEE 6 Scheduler annotation:

               

              [CODE]

              package com.abc.potsdam.middleware.cache;

               

              import javax.ejb.Schedule; 

              import javax.ejb.ScheduleExpression; 

              import javax.ejb.Stateless; 

              import javax.ejb.Timeout; 

              import javax.ejb.Timer; 

              import javax.ejb.TimerConfig; 

              import javax.ejb.TimerService; 

              import javax.naming.Context; 

              import javax.naming.InitialContext;

              import java.util.*;

               

              @Stateless 

              public class AutoTimerBean  

               

                 /**

                  * This method will be invoked every day at 5:00:00 in the morning

                  * @param timer The timer instance

                  */ 

                 //@Schedule (hour = "5", persistent=false)

                   @Schedule(second="*/10", minute="*", hour="*", dayOfWeek="*", dayOfMonth="*", month="*", year="*", info="MyTimer")

                 private void executeEveryTenSeconds(Timer timer) 

                 { 

                    // do some task here.  

                    System.out.println("Auto-timer method invoked at " + new Date() + " for bean " + this.getClass().getSimpleName()); 

                     

                 } 

               

               

              [/CODE]

               

              But strangely, I don't get to see the timer message appearing every ten seconds. In-fact it does not appear at all.

               

              I am using JEE 6 and Jboss 6.1-Final.

               

              If I see inside 'JMX MBean View', I can see the class as part of a .war deployed.

               

              NameDomainjboss.j2ee
              serviceEJB3
              nameAutoTimerBean
              ear30potsdam.middleware.ear
              jarpotsdam.middleware.cache-2.5.01-SNAPSHOT.jar
              Java Classorg.jboss.ejb3.stateless.StatelessDelegateWrapper
              DescriptionManagement Bean.

                

               

              Attribute NameAccessTypeDescriptionAttribute Value
              CreateCountRintMBean Attribute.
              0    
              InvokeStatsRorg.jboss.ejb3.statistics.InvocationStatisticsMBean Attribute.
              InvocationStatistics concurrentCalls='0' 
              CurrentSizeRintMBean Attribute.
              0    
              RemoveCountRintMBean Attribute.
              0    
              MaxSizeRintMBean Attribute.
              0    
              AvailableCountRintMBean Attribute.
              0    

                

               

              OperationReturn TypeDescriptionParameters
              getTimerServicejavax.ejb.TimerServiceMBean Operation.
              p1java.lang.Object(no description)

               

               

              Coudl someone please suggest why the timer is not getting executed and if i need to configure anything inside jmx-console?

               

              Thanks.