5 Replies Latest reply on Feb 28, 2006 1:13 PM by ejb3workshop

    Scheduled Event and Session Beans

    ejb3workshop

      I read the Timer section within the EJB3 specifications and find it very useful to invoke repeated tasks. Just how does one invoke the scheduler on application server / domain startup. Instead of having to call a service of the bean or use the @PostConstruct hook, I would like my Timer to be started on application deployment / server startup.

      After some experimentation I though about using a scheduled MBean to fire up my Timer by calling a suitable method on my bean. I am still looking for a solution to package my SAR which contains the MBEAN file and make it dependent on the EAR file being deployed.

      Alex

        • 1. Re: Scheduled Event and Session Beans
          bdecoste

          To create a deploy dependency of an MBean on an .ear file, add a depends tag to the -service.xml file using one of the service names (e.g. EJB Container or EJB Module) created when the .ear is deployed.

          For example:

          <mbean ...>

          jboss.j2ee:module=session2.jar,service=EJB3

          • 2. Re: Scheduled Event and Session Beans
            bdecoste

             

            "bdecoste" wrote:
            To create a deploy dependency of an MBean on an .ear file, add a depends tag to the -service.xml file using one of the service names (e.g. EJB Container or EJB Module) created when the .ear is deployed. The MBean deployment will block until the .ear is deployed.

            For example:

            <mbean ...>
            ...
            <depends>jboss.j2ee:module=session2.jar,service=EJB3<depends>
            </mbean>


            • 3. Re: Scheduled Event and Session Beans
              ejb3workshop

              Thanks. That worked rather well. I am now trying to "inject" a reference to my stateless bean via annotation. When I try to run this I get a NullPointerException.

              I think it would be nice if it was possible to inject a reference to a bean via @EJB3. I suppose I'll have to perform a "manual" lookup. Another 2 lines of code :-(

              /*
               * JBossSchedulable.java
               */
              
              package com.helveta.cis.cie.scheduler;
              
              import java.util.Date;
              import javax.annotation.EJB;
              import org.apache.log4j.Logger;
              import org.jboss.varia.scheduler.Schedulable;
              
              /**
               *
               * @author Alex
               */
              public class JBossSchedulable implements Schedulable
              {
               private static final Logger log = Logger.getLogger(JBossSchedulable.class);
               private String name;
               private long value;
              
               @EJB3
               private Scheduler scheduler;
              
               public JBossSchedulable(String name, long value)
               {
               this.name = name;
               this.value = value;
               log.info("ctor, name: " + name + ", value: " + value);
               }
              
               public void perform(Date now, long remainingRepetitions)
               {
               log.info("perform, now: " + now +", remainingRepetitions: " + remainingRepetitions +", name: " + name + ", value: " + value);
               try
               {
               scheduler.check();
               }
               catch (Exception e)
               {
               log.info("Unable to CHECK scheduler",e);
               }
               }
              }
              


              • 4. Re: Scheduled Event and Session Beans
                martinganserer

                Hi,

                did you solve your problem? I was able to get the Scheduler up and running. But when I try to inject a stateless session bean I get a null pointer exception too!
                You wrote that you have to do a manual lookup. Did it work. If yes how did you do that?

                Thanks!

                • 5. Re: Scheduled Event and Session Beans
                  ejb3workshop

                  By manual lookup I mean :

                  InitialContext ctx = new InitialContext();
                  System.out.println("Got Naming Service");
                  CalculatorServices calculator = (CalculatorServices) ctx.lookup("CalculatorBean/remote");
                  System.out.println("Got Calculator");
                  System.out.println("ADD : "+calculator.add(5,4));
                  


                  As long as you don't redeploy the bean. After redeploying I am getting a Classcast exception after the deployment as the new bean is not the same class as the previously deployed bean. This means I have to restart the application server, but other then that it is working.

                  Alex