Java 定时器之EJB Timer:
Interface:
package test.timersession;
import javax.ejb.Remote;
import javax.ejb.Timer;
@Remote
public interface TimerSession {
public void setTimer(long intervalDuration, String timerName);
public void timeout(Timer timer);
}
EJB:
package test.timersession;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
@Stateless (name="TimerSession", mappedName="TimerSession")
public class TimerSessionBean implements TimerSession, Serializable {
/**
*
*/
private static final long serialVersionUID = 7349339140991323648L;
private static final Logger logger = Logger.getLogger("test.timersession.TimerSessionBean");
@Resource
TimerService timerService;
public void setTimer(long intervalDuration, String timerName) {
System.out.println("In TimerSessionBean -> setTimer method");
Timer timer = timerService.createTimer(
new Date(System.currentTimeMillis()),intervalDuration,
timerName);
System.out.println("Exit TimerSessionBean -> setTimer method");
}
@Timeout
public void timeout(Timer timer) {
String info = (String) timer.getInfo();
logger.info(info + " - Timeout occurred");
System.out.println("In TimerSessionBean -> timeout method");
}
@PreDestroy
public void clearTimer(){
System.out.println("In TimerSessionBean -> clearTimer method");
if (null != timerService.getTimers()) {
Iterator<?> it = timerService.getTimers().iterator();
while (it.hasNext()) {
((Timer)it.next()).cancel();
}
}
System.out.println("Exit TimerSessionBean -> clearTimer method");
}
}
Client:
package test.timersession;
import java.util.Properties;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TimerSessionClient {
@EJB
private static TimerSession timer;
private static TimerSession timer2;
private static TimerSession timer3;
public TimerSessionClient(String[] args) {
}
public static void main(String[] args) {
TimerSessionClient client = new TimerSessionClient(args);
client.doClient();
}
public void doClient() {
try {
long intervalDuration = 5000;
System.out.println(
"Creating a timer with an interval duration of "
+ intervalDuration + " ms.");
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL, "t3://192.168.81.143:8889,192.168.81.219:8888");
Context ctx = new InitialContext(prop);
timer = (TimerSession) ctx.lookup("TimerSession#test.timersession.TimerSession");
timer2 = (TimerSession) ctx.lookup("TimerSession#test.timersession.TimerSession");
timer3 = (TimerSession) ctx.lookup("TimerSession#test.timersession.TimerSession");
timer.setTimer(intervalDuration, "Timer 1");
timer2.setTimer(intervalDuration+1000, "Timer 2");
timer3.setTimer(intervalDuration+2000, "Timer 3");
//System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception.");
ex.printStackTrace();
//System.exit(1);
}
}
}
分享到:
相关推荐
EJB TIMER SERVICE API 使用测试项目 #文档和/或官方教程: ://docs.oracle.com/javaee/7/tutorial/ejb-basicexamples004.htm#BNBOY #API JavaEE7 #Examples 以及如何运行它们: 注意:在运行任何示例之前,有...
对于EJB,特别是会话Bean和消息驱动Bean,可以手动或自动管理事务,但EJB 2.0的实体Bean必须使用容器管理的事务。 EJB容器管理的事务通过事务属性来定义,这些属性描述了EJB组件如何利用事务服务。这些属性可以通过...
包括的类有: javax.ejb....javax.ejb.TimerService.class javax.ejb.TransactionRequiredLocalException.class javax.ejb.TransactionRolledbackLocalException.class javax.ejb.spi.HandleDelegate.class
javax.ejb.TimerService.class javax.ejb.TransactionAttribute.class javax.ejb.TransactionAttributeType.class javax.ejb.TransactionManagement.class javax.ejb.TransactionManagementType.class javax.ejb....
javax.ejb.TimerService.class javax.ejb.TransactionAttribute.class javax.ejb.TransactionAttributeType.class javax.ejb.TransactionManagement.class javax.ejb.TransactionManagementType.class javax.ejb....
5. **TimerService**:EJB的TimerService接口允许在应用程序中创建定时任务,执行计划的任务或触发事件。这个部分的源码可能会展示如何配置和使用定时器服务来执行周期性任务。 6. **CompositePK**:在数据库设计中...
javax.ejb.TimerService.class javax.ejb.TransactionAttribute.class javax.ejb.TransactionAttributeType.class javax.ejb.TransactionManagement.class javax.ejb.TransactionManagementType.class javax.ejb....
可以通过`Timer`对象的`cancel()`方法取消定时器,或使用`TimerService`的`modify()`或`createTimer()`方法更新定时器的调度。 7. **处理异常** 当定时任务执行时发生异常,EJB容器会捕获并记录这些异常,但不会...
除了基础的EJB开发,MyEclipse还支持EJB的高级特性,如EJB定时器(Timer Service)、EJB的并发控制(Concurrent annotations)、依赖注入(Dependency Injection)等。这些功能可以通过配置EJB元数据或使用Java EE的...
EJB3.0使用了Java元数据注释来简化EJB组件的开发。 在“实体BEAN与ORM关系对象映射”章节中,我们学习了实体Bean与会话Bean的不同,实体Bean通常代表数据表中的行,拥有持久化身份(主键),而会话Bean则代表业务...
在EJB 3.0中,定时服务(Timer Service)是一个核心特性,它允许开发者创建计划任务和周期性操作,以实现自动化工作流和后台处理。以下是对EJB 3.0定时服务的详细介绍: 1. **定时服务简介** EJB 3.0中的定时服务...
EJB 3.0的成功促使许多之前放弃EJB的开发团队重新审视这一技术栈,并开始重新使用EJB。 尽管EJB 3.0取得了显著的进步,但它仍然没有完全达到最初设定的目标。因此,在EJB 3.1中,Sun继续推进改革的步伐,针对EJB ...
实例集中可能还会包含一些高级主题,比如如何使用EJB定时器服务(Timer Service)来执行周期性或延迟的任务,以及如何利用消息驱动bean处理JMS消息。这些实例不仅加深学习者对EJB操作技巧的理解,也为他们日后在复杂...
- `javax.ejb.TimerService`:提供了定时任务的管理,可以在EJB中创建和管理定时器。 描述中提到的“开发EJB项目时需要的jar包”表明了`javax.ejb-3.1.2.2.jar`对于EJB项目的必要性。在开发环境中,这个jar需要被...
9. **定时服务(TimerService)**:EJB 3.0提供了一个定时服务,允许Bean设置定时任务,执行周期性的业务逻辑。 10. **Web服务客户端(WSClient)**:EJB 3.0支持生成和消费Web服务,使得与其他系统集成变得更加...
- **@Init**: 在某些框架或版本中可能存在,但 EJB 3.0 中标准的做法是使用 @PostConstruct 来替代。 #### 三、Interceptor - **概念**: Interceptor 提供了一种机制来拦截并修改会话 Bean 的行为,例如事务管理、...
Java EE提供定时服务(Timer Service),允许EJB在预设的时间执行任务。开发者可以创建定时器,设置触发时间或周期,实现后台任务自动化。 **5. 安全** EJB具有内置的安全机制,支持角色基访问控制(RBAC)。...