EJB3.1 Timer Service

八月 20th, 2011 4:57下午 No Comments Post a Comment

最近做了一个定时发送邮件的功能,主要是采用了EJB3.1提供的定时功能。

接触EJB的时间不长,博客又快要长草,简单记录下来正好除除草。呵呵。。

参考了oracle的 The Java EE 6 Tutorial和 JAVA EE API

http://download.oracle.com/javaee/6/tutorial/doc/bnboy.html  Using the Timer Service

http://download.oracle.com/javaee/6/api/     Class ScheduleExpression

Enterprise bean timers are either programmatic timers or automatic timers :

记录两个小demo类,注意点写在注释里了,服务器使用JBOSS AS6.0

programmatic timers:编程

@Singleton
@Startup
public class TimerBean {
	static boolean ENABLE_PERSISTENY = false;
	@Resource
	private TimerService timerService;
	static Logger logger = Logger.getLogger(TimerBean.class);
	@PostConstruct
	public void init() {
		ScheduleExpression scheduleExpression = new ScheduleExpression();
		TimerConfig timerConfig = new TimerConfig();
		//设置persistent
		timerConfig.setPersistent(ENABLE_PERSISTENY);
		timerConfig.setInfo(TimerBean.class.getName());
		//每10秒执行一次
		scheduleExpression.hour("*").minute("*").second("*/10");
		Timer timer=timerService.createCalendarTimer(scheduleExpression,timerConfig);
	}
	@Timeout
	public void execute(Timer timer) {
		logger.info(timer.getInfo()+":"+timer.getNextTimeout().getTime());
	}
}

 

automatic timers:注解

@Singleton
@Startup
public class ScheduleBean {
	static Logger logger=Logger.getLogger(ScheduleBean.class);
	//一分钟一次
	//注意second="0",不是"*"
	//persistent设置
	@Schedule(persistent=false, hour="*", minute="*/1", second="0")
	public void test(){
		logger.info("ScheduleBean....."+new Date().getTime());
	}
}

设置persistent:

timerConfig.setPersistent(false);

@Schedule(persistent=false..

这样做的原因是:

Timers are persistent by default. If the server is shut down or crashes, persistent timers are saved and will become active again when the server is restarted. If a persistent timer expires while the server is down, the container will call the @Timeout method when the server is restarted.

Timers默认会被持久化。

如果遇到timeout method 方法找不到异常,或者已经修改的定时方法仍然起效,原因就是这个参数的了

清空JBOSS主目录\server\[default]\data文件夹即可。

(一开始没注意这个。。我把先写的一个方法删掉,提交到了SVN,结果导致组里N多人无法启动。。)

eclipse工程文件 下载

运行结果:ScheduleBean的方法一分钟一次,TimerBean的10秒一次

15:18:00,046 INFO  [com.rainripple.ejb.timer.ScheduleBean] ScheduleBean.....1313824680046
15:18:10,002 INFO  [com.rainripple.ejb.timer.TimerBean] com.rainripple.ejb.timer.TimerBean:1313824700000
15:18:20,003 INFO  [com.rainripple.ejb.timer.TimerBean] com.rainripple.ejb.timer.TimerBean:1313824710000
15:18:30,003 INFO  [com.rainripple.ejb.timer.TimerBean] com.rainripple.ejb.timer.TimerBean:1313824720000
15:18:40,002 INFO  [com.rainripple.ejb.timer.TimerBean] com.rainripple.ejb.timer.TimerBean:1313824730000
15:18:50,003 INFO  [com.rainripple.ejb.timer.TimerBean] com.rainripple.ejb.timer.TimerBean:1313824740000
15:19:00,003 INFO  [com.rainripple.ejb.timer.TimerBean] com.rainripple.ejb.timer.TimerBean:1313824750000
15:19:00,005 INFO  [com.rainripple.ejb.timer.ScheduleBean] ScheduleBean.....1313824740005

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*