How to use EJB 3 timer in a weblogic 10 cluster environment


Although there are some articles talking about EJB 3 timers or job schedulers , we were not able to find any detailed instructions on how to make EJB 3 timer work in a weblogic cluster. In this article we will go through a sample project to show how EJB 3 timers are used in a weblogic cluster. The sample project will create two recurring timers, the first recurring timer will periodically print out some simple information, the second recurring timer will create a couple of one-timer timers, each of which will print out some information. In this article, we will show you how to use weblogic admin console to configure the cluster, how the application uses the related configuration from the console and how to invoke timers, also explain what problems we faced and how we solved them.

Environment:   
web logic server 10.3.2, oracle database 11gR1, Eclipse Helios

Code:
Timer1SessionBeanLocal: local interface for creating timer
@Local
public interface Timer1SessionBeanLocal {
      public void createTimer();
}
Timer1SessionBean: a recurring timer that prints out something
@Stateless
public class Timer1SessionBean implements Timer1SessionBeanLocal {

      @Resource
      TimerService timerService ;

    public Timer1SessionBean() {
    }

    public void createTimer() {
            timerService .createTimer(
                        60000,
                        60000, null );
      }
   
      @Timeout
      public void timeout(Timer arg0) {
            System. out .println( "recurring timer1 : " + new Date());
      }
}

Timer2SessionBean: a recurring timer that creates a bunch of one-time timers,al so the number of one-time timers created is roughly based on the maximum allowed number of active timers minus the number of active timers at that time.

@Stateless
public class Timer2SessionBean implements Timer2SessionBeanLocal {
      @Resource
      TimerService timerService ;

      @EJB
      Timer3SessionBeanLocal timer3Bean ;

      public Timer2SessionBean() {
      }

      public void createTimer() {
            timerService .createTimer(120000, 300000, null );
      }

      @Timeout
      public void timeout(Timer arg0) {
            System. out .println( "recurring timer2 : " + new Date());

            // used to control the total number of threads running in the app
            // use 10 as maximum in this example.
            int numberOfActiveTimers = timer3Bean .getCountOfActiveTimers();

            if (numberOfActiveTimers < 10) {
                  int toCreateNum = 10 - numberOfActiveTimers;
                 
                  for ( int i = 0; i < toCreateNum; i++) {
                        Timer3Info info = new Timer3Info();
                        // set start delays to be 30,60,90... seconds
                        info.setDelay(30000 * (i + 1));                                                                      
                        timer3Bean .createTimer(info);
                  }
            }
            System. out .println( "Exit timeout in timer2" );
      }
}

Timer3SessionBean: one-time timer created by another timer, provides the number of active timers for this bean, and prints out something.

@Stateless
public class Timer3SessionBean implements Timer3SessionBeanLocal {

      @Resource
      TimerService timerService ;

      public Timer3SessionBean() {
      }

      public void createTimer(Timer3Info timerInfo) {
            timerService .createTimer(timerInfo.getDelay(), null );
      }

      @Timeout
      public void timeout(Timer arg0) {
            System. out .println( "one-time timer3 : " + new Date());
      }

      /**
       *
       * @return the number of active timers
       */
      public int getCountOfActiveTimers(){
            int retVal = 0;
            try {
                  //In rare occasions, could throw NullPointerException //because of a bug in weblogic
                  @SuppressWarnings ( "unchecked" )
                  Collection<Timer> timersCol = timerService .getTimers();
                 
                  if (timersCol != null )
                        retVal = timersCol.size();
            } catch (Exception e) {
                  //if it failed, use the maximum (10 in this example), so no //new timers can be created
                  retVal = 10;
            }

            return retVal;
           
      }
}


TestTimerCreateServlet: used to create recurring timers.

public class TestTimerCreateServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;

      @EJB
      Timer1SessionBeanLocal timer1 ;

      @EJB
      Timer2SessionBeanLocal timer2 ;

      /**
       * @see HttpServlet#HttpServlet()
       */
      public TestTimerCreateServlet() {
            super ();
      }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
       *      response)
       */
      protected void doGet(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
            System. out .println( "start timer creation : " + new Date());
            try {
                  timer1 .createTimer();
                  timer2 .createTimer();
            } catch (Exception e) {
                  System. out .println( "timer creation failed " );
                  throw new   RuntimeException( "timer creation failed " , e);
            }
            System. out .println( "Done timer creation : " );
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
       *      response)
       */
      protected void doPost(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
      }
}

Overall, the code is quite simple. Some things are worth noting here is that local interfaces are used for the session beans, a servlet which needs to be invoked externally is used to create timers, also ‘ timerService .getTimers()’ is used to find out the number of active timers for a session bean and also help control the number of running timers in the system, so the system will not be over stretched.

weblogic-ejb-jar.xml: some important configurations





 
<? xml version = "1.0" encoding = "UTF-8" ?>
< wls:weblogic-ejb-jar xmlns:wls = "http://xmlns.oracle.com/weblogic/weblogic-ejb-jar" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-ejb-jar http://xmlns.oracle.com/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd" >
    <!--weblogic -version:10.3.2-->
    < wls:weblogic-enterprise-bean >
        < wls:ejb-name > Timer1SessionBean </ wls:ejb-name >
        < wls:stateless-session-descriptor >
            < wls:timer-descriptor >
                < wls:persistent-store-logical-name > timerst </ wls:persistent-store-logical-name >
            </ wls:timer-descriptor >
        </ wls:stateless-session-descriptor >
     </ wls:weblogic-enterprise-bean >
    < wls:weblogic-enterprise-bean >
        < wls:ejb-name > Timer2SessionBean </ wls:ejb-name >
        < wls:stateless-session-descriptor >
            < wls:timer-descriptor >
                < wls:persistent-store-logical-name > timerst </ wls:persistent-store-logical-name >
            </ wls:timer-descriptor >
        </ wls:stateless-session-descriptor >
    </ wls:weblogic-enterprise-bean >
    < wls:weblogic-enterprise-bean >
        < wls:ejb-name > Timer3SessionBean </ wls:ejb-name >
        < wls:stateless-session-descriptor >
            < wls:timer-descriptor >
                < wls:persistent-store-logical-name > timerst </ wls:persistent-store-logical-name >
            </ wls:timer-descriptor >
        </ wls:stateless-session-descriptor >
    </ wls:weblogic-enterprise-bean >
    < wls:timer-implementation > Clustered </ wls:timer-implementation >
</ wls:weblogic-ejb-jar >

The most important things are that making sure the timers are cluster aware, and also using proper logical name for the persistent store  ( timerst ), which will be configured in the administrator console.


Admin console configuration:
<
分享到:
评论

你可能感兴趣的:(oracle,bean,weblogic,javaee,ejb)