我是靠谱客的博主 勤劳画板,这篇文章主要介绍定时服务,现在分享给大家,希望可以做个参考。

定时服务与会话bean的开发过程大致相同,但比会话bean多了几个操作:

  1. 使用容器对象SeesionContext创建定时器。如:@Resource private SessionContext ctx;
  2. 使用@Timerout注释声明定时器方法。

例如:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.xixi.stateless; import java.util.Date; import javax.annotation.Resource; import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.ejb.Timeout; import javax.ejb.Timer; @Stateless public class TimerService implements TimerServiceRemote { private int count = 1; @Resource private SessionContext ctx; /* (non-Javadoc) * @see org.xixi.stateless.TimerServiceRemote#scheduleTimer(long) */ public void scheduleTimer(long milliseconds) { // TODO Auto-generated method stub count = 1; ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds), milliseconds, "*********"); } @Timeout public void timeoutHandler(Timer timer){ System.out.println("-------------------"); System.out.println("定时器事件发生时的参数为:"+timer.getInfo()); System.out.println("======================="); if (count>=5) { timer.cancel(); } count++; } }
  1.  timer就是ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds), milliseconds, "*********"); 所创建的!
  2. timer.getaInfo()返回的信息就是上面创建timer时的第三个参数,例子中是“***************”.
  3. ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds), milliseconds, "*********");  它包括三个参数分别是:
  •  第一个:定时器启动的时间,如果传入时间小于现在,定时器会立刻启动。
  • 第二个:间隔多长时间后再次触发定时事件,单位:毫秒。
  • 第三个:传给定时器的参数信息,此值必须是实现Serializable接口。

                     


 

最后

以上就是勤劳画板最近收集整理的关于定时服务的全部内容,更多相关定时服务内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部