`
lzy190100311
  • 浏览: 7682 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

EJB3定时服务使用说明

    博客分类:
  • EJB3
阅读更多

本文以JBOSS4.2.3GA下使用EJB3定时服务为例。

 

一、 简介

作为一个简单的定时器, EJB3 定时服务提供定时任务实现的 API

 

二、 使用说明

       1、  注入定时服务

@Resource    
private TimerService timerService;

 

       2、 创建定时器

timerService.createTimer
(new Date(new Date().getTime()+ intervalDuration),intervalDuration, timerInfo);

 /**
    * Create an interval timer whose first expiration occurs at a given point in time and
    * whose subsequent expirations occur after a specified interval.
    *
    * @param initialExpiration The point in time at which the first timer expiration must occur.
    * @param intervalDuration The number of milliseconds that must elapse between timer
    *                         expiration notifications. Expiration notifications are
    *                         scheduled relative to the time of the first expiration. If
    *                         expiration is delayed(e.g. due to the interleaving of other
    *                         method calls on the bean) two or more expiration notifications
    *                         may occur in close succession to "catch up".
    * @param info Application information to be delivered along with the timer expiration
    *             notification. This can be null.
    *
    * @return The newly created Timer. 
    *
    * @throws IllegalArgumentException If initialExpiration is null, or initialExpiration.getTime()
    *                                  is negative, or intervalDuration is negative.
    * @throws IllegalStateException If this method is invoked while the instance is in
    *                               a state that does not allow access to this method.
    * @throws EJBException If this method could not complete due to a system-level failure.
    **/
   public Timer createTimer( Date initialExpiration, long intervalDuration, Serializable info )
      throws
         IllegalArgumentException,
         IllegalStateException,
         EJBException;

 

       3、 使用注解标注定时任务函数

import javax.ejb.Timeout;
@Timeout  
public void timerTask(Timer t)   
{ ...  }

 

       4、 当定时器创建后,则定时触发定时任务

 

 

三、   注意事项

       1、 只能用在无状态会话bean, 且一个bean 中只能对一个方法使用timeout 定时任务注解

       2、 TimerService 具有持久化特性, 也就是说如果一个TimerService 已经启动, 如果服务器重新启动了, 这个TimerService 会继续执行

       3、 Timer类 所在包: ${JBOSS_HOME}/ client/ jbossall-client.jar

TimerImpl类 所在包:${JBOSS_HOME}/server/default/lib/jboss.jar

TimerServiceImpl类 所在包: ${JBOSS_HOME}/server/default/lib/jboss.jar

       4、  查看定时器是否已存在的方法

timerInfo: 定时器创建时的字符串参数,可用来标识定时器

Timer timer = null;
Iterator timers = timerService.getTimers().iterator();
while(timers.hasNext())//查看定时器是否已存在
{
	Timer t = (Timer) timers.next();
	String info = (String) t.getInfo();
	if (info.equals(timerInfo))
	{
		timer = t;
	}
}

 

四、 定时服务TimerService 是如何具有持久化特性的

 

org.jboss.ejb.txtimer.TimerServiceImpl 源码 createTimer 方法看到语句

persistencePolicy.insertTimer(timerId, timedObjectId, initialExpiration, intervalDuration, info); 

作用是把定时器作为记录插入数据库。JBOSS 本地使用的是hsqldb 内存数据库并把数据存储于硬盘,数据文件存储在${JBOSS_HOME}/server/default/data/hypersonic/JBOSS 启动的时候会重新加载定时器。

 

五、 清除定时器的方法

     1   程序 : 使用 Timer 类接口 cancel()

缺点:此方法不会清除定时器的持久化数据,在服务器下次启动时定时器仍会启动。

     2、   程序:使用 TimerImpl 接口实现类public 方法 killTimer()

缺点 :此方法为 JBOSS 下的定时器实现,只能在 JBOSS 服务器下使用

     3、    手动:直接清除数据库中的定时器持久数据

方法:

               1)  编写一个 bat 批处理文件,写入以下语句:

java -cp E:\jboss-4.2.3.GA\server\default\lib\hsqldb.jar

org.hsqldb.util.DatabaseManager -url

jdbc:hsqldb:E:/jboss-4.2.3.GA/server/default/data/hypersonic/localDB

               2)  执行 bat 文件,如下图,找到你的 timer ,然后执行删除语句

              缺点:需要手动清理

 

  • 大小: 48.8 KB
分享到:
评论

相关推荐

    EJB3实例教程EJB3实例教程 是一个教程

    - **定时服务**:介绍EJB3中提供的定时服务功能,以及如何利用它来实现周期性任务。 - **安全服务**: - **自定义安全域**:讨论如何根据需求自定义安全域,以适应不同的应用场景。 #### 五、消息驱动Bean ...

    ejb3中文教程ejb3中文教程

    **3.8 定时服务(Timer Service)** 探讨EJB3.0中用于安排定期任务执行的服务。 **3.9 安全服务(Security Service)** 讲解如何利用EJB3.0的安全服务来实现权限控制和身份验证。 #### 四、消息驱动Bean(Message-...

    EJB3实例教程破解版

    - 描述如何使用EJB3进行单表映射,即将实体Bean映射到数据库中的单一表。 **3.3 成员属性映射** - 讨论实体Bean中成员变量与数据库表字段之间的映射规则。 **3.4 建议重载实体Bean的EQUALS()和HASHCODE()方法** ...

    ejb3·0入门经典教程

    - **定时服务**: 阐述定时服务的使用,允许会话Bean执行定时任务。 - **安全服务**: 分析安全服务在会话Bean中的作用,包括自定义安全域的配置。 #### 四、JMS与消息传递 **4.1 消息驱动Bean** - **队列消息**: ...

    EJB3.0实例教程

    - 讲述了EJB3中安全服务的使用方法。 - 包括身份验证、授权等安全管理机制。 - **自定义安全域**: - 说明了如何在EJB3应用程序中实现自定义的安全域。 - 包括配置安全策略、定义访问控制规则等步骤。 #### 第...

    ejb3.0实例教程

    - **定时服务**:解释如何使用EJB 3提供的定时服务功能。 - **安全服务**:探讨如何利用EJB的安全服务来保护应用程序,并包括自定义安全域的配置。 #### 五、消息驱动Bean (Message-Driven Bean) - **概述**:简要...

    EJB 3.0实例教程(pdf)

    - **定时服务(Timer Service)**:介绍如何使用EJB 3.0提供的定时服务功能来执行周期性的后台任务。 - **安全服务(Security Service)**: - **自定义安全域**:讲解如何为特定的应用程序定制安全策略。 #### 五、...

    EJB 3.0 入门教程 黎活明

    - **定时服务**:利用定时器服务来执行周期性的任务。 - **安全服务**:实现基于角色的访问控制和认证机制。 - **自定义安全域**:讲解如何根据项目需求自定义安全策略。 #### 五、JMS (Java Message Service) - ...

    ejb-sample-wildlfy:Wildfly 8.2.0 的示例 EJB 应用程序

    4. **增强的定时器服务**:提供了在bean内部定义定时任务的能力。 5. **更好的CDI集成**:与Contexts and Dependency Injection (CDI)框架更紧密地集成,提供了更多元化的依赖注入和上下文管理。 ### 示例应用分析 ...

Global site tag (gtag.js) - Google Analytics