8 Replies Latest reply on Jan 16, 2007 4:33 AM by torf

    Scheduler vs. EJB Timer Service

      Hi,

      Right now I have an application that's using the EJB Timer service (I have an MDB with a @Timeout public void timeout(javax.ejb.Timer t) method that is set to run every few seconds, indefinitely, as soon as the application is deployed.

      Previous posts to these forums have indicated that the EJB Timer service is not in any way clustered... so if I had several Jboss nodes in a cluster, only one of them is running the Timer service, and if that single node went down, my timer would stop (and the timer is critical to my application).

      So I was thinking of moving to the org.jboss.varia.scheduler.Scheduler MBean timer. But all the examples that I've seen are toy. The method invoked by the timer just logs. How do you make a Schedulable instance call an EJB method, for example? And how does deployment work if your Schedulable instance thus depends on an EJB? Right now I just have one .ear to deploy. Do I now need to deploy an .ear and a .sar? Do I need to worry about the deployment ordering?

      Thanks,
      --Erik

        • 1. Re: Scheduler vs. EJB Timer Service

          Hi,

          we call a SLSB from a org.jboss.varia.scheduler.Scheduler implementation, and our deployment goes like this:

          deploy/applications/MyApp/ contains:
          - MyApp-all.ear
          - MyApp-api.jar

          deploy-hasingleton/applications/MyApp/MyApp-timer-service.xml:

          <?xml version="1.0" encoding="UTF-8"?>
          <server>
           <classpath codebase="deploy/applications/MyApp" archives="MyApp-api.jar"/>
           <mbean code="org.jboss.varia.scheduler.Scheduler" name=":service=Scheduler,schedule=MyApp">
           <depends>jboss.j2ee:service=EARDeployment,url='MyApp-all.ear'</depends>
           <attribute name="StartAtStartup">true</attribute>
           <attribute name="SchedulableClass">our.company.framework.timer.TimedObjectSchedulableRemote</attribute>
           <attribute name="SchedulableArguments">ejb/MySchedulerTimer</attribute>
           <attribute name="SchedulableArgumentTypes">java.lang.String</attribute>
           <attribute name="InitialStartDate">0</attribute>
           <attribute name="SchedulePeriod">60000</attribute>
           <attribute name="InitialRepetitions">-1</attribute>
           </mbean>
          </server>
          


          our.company.framework.timer.TimedObjectSchedulableRemote has a String-arg constructor which is invoked with argument "ejb/MySchedulerTimer". Its perform method looks up the remote home "ejb/MySchedulerTimer" in JNDI, reflectively invokes create(), and then (non-reflectively) the SLSB business method.

          our.company.framework.timer.TimedObjectSchedulableRemote is contained in server/$SERVER_CONF/lib/our-framework.jar.

          Regards,
          Christoph

          • 2. Re: Scheduler vs. EJB Timer Service
            t_kishore

            Add this entry to jboss-app.xml for your EAR
            <jboss-app>

            my-scheduler-service.xml

            </jboss-app>


            Keep my-scheduler-service.xml directy under the EAR with entries like this:

            <?xml version="1.0" encoding="UTF-8"?>


            true
            com.mybizz.scheduler.NotificationSchedule
            0 <!-- start now --> 600000 <!-- milliseconds -->
            -1



            Thats all there is to it. The SchedulableClass can look up and invoke EJB,etc. If you try to keep the *-service.xml outside of EAR, u will have to deal with classpath issues.

            • 3. Re: Scheduler vs. EJB Timer Service
              t_kishore

              service.xml got truncated in the previous post.

              <server>
               <mbean code="org.jboss.varia.scheduler.Scheduler" name=":service=Scheduler">
               <attribute name="StartAtStartup">true</attribute>
               <attribute name="SchedulableClass">com.mybiz.scheduler.NotificationSchedule</attribute>
               <attribute name="InitialStartDate">0</attribute>
               <attribute name="SchedulePeriod">600000</attribute>
               <attribute name="InitialRepetitions">-1</attribute>
               </mbean>
              </server>
              




              • 4. Re: Scheduler vs. EJB Timer Service
                t_kishore

                arggg.. one more time.

                *** jboss-app.xml ****
                <jboss-app>
                 <module>
                 <service>kns-scheduler-service.xml</service>
                 </module>
                </jboss-app>
                


                • 5. Re: Scheduler vs. EJB Timer Service
                  krimsonnitehawk

                  Hi t_kishore,

                  I have tried what you suggest and still get the error :

                  Incomplete Deployment listing:
                  
                  --- MBeans waiting for other MBeans ---
                  ObjectName: jboss:service=Scheduler
                   State: FAILED
                   Reason: org.jboss.deployment.DeploymentException: Exception setting attribute javax.management.Attribute@1bb6a69 on mbean jboss:service=Scheduler; - nested throwable: (java.security.InvalidParameterException: Given class com.company.app.api.AppApiClean is not not found)
                  


                  In my scheduler-service.xml file I have the following -

                  <attribute name="SchedulableClass">com.company.app.api.AppApiClean</attribute>


                  This is the correct path to this class within my app and the only way I can prevent this error is to place the java class file within a jar in server/$SERVER_CONF/lib/ . When I do this the perform method is called correctly but from server/$SERVER_CONF/lib/ it has no idea about the application's EJBs so cannot call them. I notice that torf has his SchedulableClass contained in server/$SERVER_CONF/lib/our-framework.jar. I don't understand how his code calls the EJBs in his application from this lib folder. If someone could explain then that would be great. t_kishore is your schedulable class contained within you application packages or in the lib folder?

                  • 6. Re: Scheduler vs. EJB Timer Service

                    Hi,

                    this is our Schedulable implementation:

                    package de.mobilcom.messenger.scheduler;
                    
                    public class TimedObjectSchedulableRemote implements Schedulable {
                     private String sRemoteHomeName;
                     private our.company.framework.timer.TimedObjectRemote rTimedObject;
                    
                     // will be invoked by jboss with configured arg "ejb/MySchedulerTimer"
                     public TimedObjectSchedulableRemote(String sRemoteHomeName) {
                     this.sRemoteHomeName = sRemoteHomeName;
                     }
                    
                     public void perform(Date rDate, long nValue) {
                     try {
                     if (rTimedObject == null) {
                     // get MySchedulerTimer SLSB's home
                     Context rInitialContext = new InitialContext();
                     Object rHome = rInitialContext.lookup(sRemoteHomeName);
                    
                     // call rHome.create() via refection to obtain remote SLSB instance
                     Method rMethod = rHome.getClass().getDeclaredMethod("create", new Class[0]);
                     rTimedObject = (our.company.framework.timer.TimedObjectRemote)
                     rMethod.invoke(rHome, new Object[0]);
                     }
                    
                     // call SLSB business method
                     rTimedObject.onTimeout();
                     }
                     catch (Exception eEx) {
                     ... // logging
                     rTimedObject = null; // reset invalid timed object reference
                     }
                     }
                    }
                    


                    Every SLSB intended for invocation via Scheduler implements interface our.company.framework.timer.TimedObjectRemote whose only method is void onTimeout() throws RemoteException.

                    HTH,
                    Christoph

                    • 7. Re: Scheduler vs. EJB Timer Service
                      pgambino

                      Can someone post the example EAR structure that you used? I'm having a problem getting JBoss to reconignize the Service file.

                      • 8. Re: Scheduler vs. EJB Timer Service

                        MyApp-all.ear contains:

                        META-INF/
                        META-INF/MANIFEST.MF
                        META-INF/application.xml
                        MyApp-all.jar
                        MyApp-all.wsr
                        


                        MyApp-all.ear:MyApp-all.jar contains:
                        META-INF/
                        META-INF/MANIFEST.MF
                        META-INF/ejb-jar.xml
                        META-INF/jboss.xml
                        META-INF/jbosscmp-jdbc.xml
                        META-INF/lib/
                        META-INF/lib/j2ssh-commons-logging-v0.2.7.jar
                        META-INF/lib/j2ssh-core-v0.2.7.jar
                        de/
                        de/mobilcom/
                        de/mobilcom/messenger/
                        de/mobilcom/messenger/Attachment.class
                        de/mobilcom/messenger/AttachmentType.class
                        de/mobilcom/messenger/Attribute.class
                        de/mobilcom/messenger/AttributeName.class
                        de/mobilcom/messenger/Message.class
                        de/mobilcom/messenger/MessageOrder.class
                        de/mobilcom/messenger/MessagePageRequest.class
                        de/mobilcom/messenger/MessagePageResult.class
                        de/mobilcom/messenger/MessageType.class
                        de/mobilcom/messenger/MessengerException.class
                        de/mobilcom/messenger/MessengerExceptionCode.class
                        de/mobilcom/messenger/MessengerService.class
                        de/mobilcom/messenger/MessengerServiceBaseBean.class
                        de/mobilcom/messenger/MessengerServiceLocal.class
                        de/mobilcom/messenger/MessengerServiceLocator.class
                        de/mobilcom/messenger/MessengerServiceProdBean.class
                        de/mobilcom/messenger/MessengerServiceProdLocalHome.class
                        de/mobilcom/messenger/MessengerServiceProdRemoteHome.class
                        de/mobilcom/messenger/MessengerServiceRemote.class
                        de/mobilcom/messenger/MessengerServiceTestBean.class
                        de/mobilcom/messenger/MessengerServiceTestLocalHome.class
                        de/mobilcom/messenger/MessengerServiceTestRemoteHome.class
                        de/mobilcom/messenger/TransmissionInterval.class
                        de/mobilcom/messenger/TransmissionPriority.class
                        de/mobilcom/messenger/configuration/
                        de/mobilcom/messenger/configuration/Configuration.class
                        de/mobilcom/messenger/configuration/ConfigurationMBean.class
                        de/mobilcom/messenger/gateway/
                        de/mobilcom/messenger/gateway/AttachmentDataSource$InternalByteArrayOutputStream.class
                        de/mobilcom/messenger/gateway/AttachmentDataSource.class
                        de/mobilcom/messenger/gateway/Gateway.class
                        de/mobilcom/messenger/gateway/GatewayEMailProdImpl.class
                        de/mobilcom/messenger/gateway/GatewayEMailTestImpl.class
                        de/mobilcom/messenger/gateway/GatewayException.class
                        de/mobilcom/messenger/gateway/GatewayMessages.class
                        de/mobilcom/messenger/gateway/GatewayMessages.properties
                        de/mobilcom/messenger/gateway/GatewayNullImpl.class
                        de/mobilcom/messenger/gateway/GatewaySMSProdImpl.class
                        de/mobilcom/messenger/gateway/GatewaySMSTestImpl.class
                        de/mobilcom/messenger/repository/
                        de/mobilcom/messenger/repository/MessageEntryBean.class
                        de/mobilcom/messenger/repository/MessageEntryBeanLocal.class
                        de/mobilcom/messenger/repository/MessageEntryBeanLocalHome.class
                        de/mobilcom/messenger/repository/Repository.class
                        de/mobilcom/messenger/repository/RepositoryEntityImpl.class
                        de/mobilcom/messenger/repository/RepositoryException.class
                        de/mobilcom/messenger/repository/RepositoryMessages.class
                        de/mobilcom/messenger/repository/RepositoryMessages.properties
                        de/mobilcom/messenger/repository/RepositoryTestImpl$MessageEntry.class
                        de/mobilcom/messenger/repository/RepositoryTestImpl.class
                        de/mobilcom/messenger/scheduler/
                        de/mobilcom/messenger/scheduler/MessageSchedulerTimerRemote.class
                        de/mobilcom/messenger/scheduler/MessageSchedulerTimerRemoteHome.class
                        de/mobilcom/messenger/scheduler/Scheduler.class
                        de/mobilcom/messenger/scheduler/SchedulerException.class
                        de/mobilcom/messenger/scheduler/SchedulerMessageBean.class
                        de/mobilcom/messenger/scheduler/SchedulerMessages.class
                        de/mobilcom/messenger/scheduler/SchedulerMessages.properties
                        de/mobilcom/messenger/scheduler/SchedulerProdImpl.class
                        de/mobilcom/messenger/scheduler/SchedulerTestImpl.class
                        de/mobilcom/messenger/scheduler/SchedulerTimerBean.class
                        j2ssh-commons-logging-v0.2.7.jar
                        j2ssh-core-v0.2.7.jar