`

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:

相关推荐

    Weblogic10 + EJB3入门教程

    ### Weblogic10 + EJB3入门教程 #### 一、引言 随着企业级应用的发展,Java EE标准成为构建复杂应用系统的重要基石之一。Oracle公司的WebLogic Server作为一款高性能的应用服务器,在Java EE领域占据着重要的地位。...

    Ejb3.0 Local Working In Weblogic10.3

    3. **客户端访问EJB** - 在客户端,可以通过JNDI查找获取bean的引用。首先,需要导入`javax.naming.InitialContext`和`java.lang.Object`,然后执行以下代码: ```java InitialContext ic = new InitialContext()...

    weblogic 10 ejb3 入门教程 + 调试通过的源代码

    【标题】"weblogic 10 ejb3 入门教程 + 调试通过的源代码"涉及的关键知识点主要集中在两个核心领域:WebLogic Server 10 和 EJB 3.0(Enterprise JavaBeans 第三版)。WebLogic Server 是 Oracle 提供的一款企业级...

    struts_ejb.rar_EJB weblogic_ejb_ejb struts _ejb+struts_weblogic

    3. **WebLogic**:WebLogic是Oracle公司提供的一个强大的Java应用服务器,它支持Java EE标准,包括EJB的部署和运行。WebLogic提供了许多企业级特性,如集群、负载均衡、安全管理等,使得应用程序能够在一个可靠的...

    使用eclipse通过weblogic开发简单的ejb应用(weblogic 10.x & ejb3.x)

    在本文中,我们将深入探讨如何使用Eclipse IDE与WebLogic Server 10.x版本结合,开发基于EJB 3.0规范的应用程序。EJB(Enterprise JavaBeans)是Java EE平台的核心组件,用于构建可扩展、可靠且安全的企业级应用程序...

    Weblogic Ejb 学习笔记

    WebLogic EJB学习笔记主要涵盖了企业级Java Bean(Enterprise JavaBeans,简称EJB)在Oracle WebLogic Server中的应用和管理。EJB是Java EE(Java Platform, Enterprise Edition)平台的核心组件,用于构建可扩展、...

    Weblogic下ejb配置

    WebLogic Server是Oracle公司的一款企业级Java应用服务器,它提供了对EJB(Enterprise JavaBeans)的强大支持。EJB是Java EE平台的核心组件,用于构建分布式、面向服务的企业级应用程序。本篇文章将深入探讨在...

    Weblogic11g+EJB3的小例子

    这个例子对于初学者来说,是一个很好的实践平台,可以帮助理解EJB3在Weblogic 11g环境下的工作原理,以及如何构建和部署EJB3应用。通过研究和运行这个例子,开发者可以深入学习EJB3的核心特性,以及Weblogic服务器的...

    EJB 和WEBLOGIC 结合例子

    3. **部署EJB**:在WebLogic中,EJB通过部署描述符(如ejb-jar.xml、weblogic-ejb-jar.xml等)进行配置,并打包成EAR文件进行部署。这包括定义bean的行为、接口、安全性和资源连接等。 4. **客户端调用EJB**:...

    EJB3的三本好书第2本, EJB3 in Action 2007

    EJB3的三本好书第二本,最好的介绍ejb3的书,看过之后,其他的书都送人了,而且附带的源代码,几乎包括了所有的主流应用服务器的例子,glassfish, jboss, weblogic, oracleAS 3本书分别是: 1. Beginning EJB3 ...

    weblogic+ejb3.0例子

    【Weblogic + EJB3.0】:这是一个关于在Weblogic服务器上使用EJB3.0版本进行开发的教程。EJB(Enterprise JavaBeans)是Java EE平台的一部分,用于构建可部署在企业级服务器上的组件化应用程序。EJB3.0引入了许多...

    weblogic 92 cluster

    【WebLogic 9.2 集群配置详解】 WebLogic Server 9.2 集群是一种高可用性和可扩展性的解决方案,它允许你在一个物理或虚拟环境中部署多个服务器实例,共同处理工作负载,提高应用程序的可靠性和性能。本文将深入...

    ejb例子(可以在weblogic下运行)

    这是一个可以运行在weblogic上的ejb例子

    EJB3.0+weblogic操作文档

    【EJB3.0+weblogic操作文档】详解 EJB (Enterprise JavaBeans) 是Java EE平台中的核心组件,用于构建分布式、事务处理和安全的企业级应用。EJB3.0是EJB规范的一个重大改进,它引入了注解驱动的编程模型,大大简化了...

    经典JAVA EE企业应用实战基于WEBLOGIC JBOSS的JSF+EJB 3+JPA整合开发——源码.part1

    经典JAVA EE企业应用实战基于WEBLOGIC JBOSS的JSF+EJB 3+JPA整合开发——源码.part1 其他部分详见我的上传列表,全部分卷下载完成才能解压。 本书介绍了Java EE规范的三大主要规范JSF、EJB 3和JPA,其中JSF是Sun...

    经典Java EE企业应用实战:基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 part3

    经典Java EE企业应用实战:基于WebLogic/JBoss的JSF+EJB 3+JPA整合开发 part3

    经典JAVA EE企业应用实战基于WEBLOGIC JBOSS的JSF+EJB 3+JPA整合开发——源码第3章

    经典JAVA EE企业应用实战基于WEBLOGIC JBOSS的JSF+EJB 3+JPA整合开发——源码第3章 其他部分详见我的上传列表 本书介绍了Java EE规范的三大主要规范JSF、EJB 3和JPA,其中JSF是Sun公司提供的JSF RI;EJB 3部分则包含...

    Eclipse中weblogic和EJB的配置

    帮助初学这在Eclipse中配置开发EJB的容器这里以weblogic的配置来说明

    图解eclipse+myelcipse+weblogic开发EJB,.doc

    ### 图解Eclipse+MyEclipse+WebLogic开发EJB #### 一、概述 本文旨在通过图文并茂的方式详细介绍如何使用Eclipse、MyEclipse 和 WebLogic 这三种工具来开发EJB(Enterprise JavaBeans)应用。EJB 是一种用于开发...

Global site tag (gtag.js) - Google Analytics