Timer service in EJB3 with anotation is fairly simple, I give you
an example as follow, and it's really simple and self-explanation:
<!----> 1 package com.ramon.expejb3.session.impl;
2
3 import javax.annotation.Resource;
4 import javax.ejb.Stateless;
5 import javax.ejb.Timeout;
6 import javax.ejb.Timer;
7 import javax.ejb.TimerService;
8
9 import com.ramon.expejb3.session.Greeting;
10
11 @Stateless(name = "Greeting")
12 public class GreetingBean implements Greeting {
13
14 @Resource
15 TimerService ts;
16
17 /* (non-Javadoc)
18 * @see com.ramon.expejb3.session.Greeting#greeting(java.lang.String)
19 */
20 public void greeting(String name) {
21 System.out.println("Hello " + name + ".");
22 ts.createTimer(1 * 1000, 5 * 1000, name);
23 System.out.println("Create timer for " + name + ".");
24 }
25
26 @Timeout
27 public void timeout(Timer timer) {
28 String name = (String)timer.getInfo();
29 System.out.println(name + " TIME OUT.");
30 //timer.cancel();
31 }
32 }
33
It's the "
GreetingBean" I introduced in part one of this series article with TimerService injected,
@Resource
anotation inject the time service we want, it's totally free which is
supported by the J2EE container that means we do not need any further
steps to get the powerful schedule service. In the line 22, I crate a
new timer, I think this could be more fit if it was called Task
instead, there are several "createTimer" methods with different
parameters, the one I used here describe the task should be execute 1
second delay and every 5 second one time in the future, the third
parameter can be anything you want to pass in to the task, it can be
get back use method Timer.getInfo() like line 28 does here.
Another important anotation here is the @Timeout anotation, this
anotation tell container which method will be called when timeout for
this task to be executed, you can only specify one timeout method for
each Bean, if there is no @Timeout anotation at all, you will get some
exception like "No timeout method specified" in Jboss server. Something
funny here is that you should mark the timeout method as "public",
otherwise, you will get the same error as no timeout method does, I do
NOT think it's necessary, because the time out method should only be
called by the container, "private" signature is enought for that, with
"private" signature container still has the right to access this method
with java reflection, nevertheless timer service in EJB3 is still a
good tools for use.
I summarize what good/bad for timer service in EJB3 as follow:
Good news:
- It's free, it's supported by the EJB container.
- It's portable, it's supported by the EJB container as specified in EJB3 specification.
- It's easy to use, no need to learn more.
Bad news:
- The feature of EJB3 timer sevice is not as powerful
as some third party timer service such as Quartz, but it's enough with
EJB3 for ordinary daily job.
- There is no UI for you to monitor the timer you created.
分享到:
相关推荐
在本文中,我们将深入探讨如何使用Eclipse IDE与JBoss应用程序服务器(版本7.x)来开发和部署一个基于EJB 3.x的简单企业级Java应用程序。EJB(Enterprise JavaBeans)是Java平台上的核心组件,它提供了一种标准的...
罗时飞精通EJB3.0.part01.rar罗时飞精通EJB3.0.part01.rar罗时飞精通EJB3.0.part01.rar罗时飞精通EJB3.0.part01.rar
在JBoss 7及以上版本中,对EJB的配置过程相较于之前的版本有所变化,主要涉及到两个关键的配置文件:`jboss-ejb3.xml`和`ejb-jar.xml`。 `ejb-jar.xml`文件是EJB模块的标准配置文件,遵循Java EE规范。在这个文件中...
javax.ejb.DependsOn.class javax.ejb.DuplicateKeyException.class javax.ejb.EJB.class javax.ejb.EJBAccessException.class javax.ejb.EJBContext.class javax.ejb.EJBException.class javax.ejb.EJBHome.class ...
javax.ejb.AccessLocalException.class javax.ejb.CreateException.class javax.ejb.DuplicateKeyException.class javax.ejb.EJBContext.class javax.ejb.EJBException.class javax.ejb.EJBHome.class javax.ejb....
中文版EXPERT-ONE-ON-ONE-J2EE-DEVELOPMENT-WITHOUT-EJB.part3.rar
H:\q\罗时飞精通EJB3.0.part08.rarH:\q\罗时飞精通EJB3.0.part08.rarH:\q\罗时飞精通EJB3.0.part08.rar
标题中的“J2EE Jboss Ejb With Eclipse 2003.rar”表明这是一个关于使用Eclipse集成开发环境(IDE)在JBoss应用服务器上开发Java Enterprise Edition (J2EE) 的Enterprise JavaBeans (EJB) 的教程或资料包。...
ejb3 and jboss 多本合一(three) Using @Service annotation in eclipse JBoss中文文档.pdf JBOSS使用指南.doc JBOSS架构分析.doc JBOSS对EJB3_0的扩展 jbossgs4x.pdf JBoss-EJB3.0实例教程.pdf JBoss.in.Action....
美河制作.中文版EXPERT.ONE.ON.ONE.J2EE.DEVELOPMENT.WITHOUT.EJB.part01.rar美河制作.中文版EXPERT.ONE.ON.ONE.J2EE.DEVELOPMENT.WITHOUT.EJB.part01.rar
H:\q\罗时飞精通EJB3.0.part09.rarH:\q\罗时飞精通EJB3.0.part09.rar
H:\q\罗时飞精通EJB3.0.part06.rarH:\q\罗时飞精通EJB3.0.part06.rarH:\q\罗时飞精通EJB3.0.part06.rar
H:\q\罗时飞精通EJB3.0.part07.rarH:\q\罗时飞精通EJB3.0.part07.rarH:\q\罗时飞精通EJB3.0.part07.rar
H:\q\罗时飞精通EJB3.0.part02.rarH:\q\罗时飞精通EJB3.0.part02.rar
javax.ejb.DependsOn.class javax.ejb.DuplicateKeyException.class javax.ejb.EJB.class javax.ejb.EJBAccessException.class javax.ejb.EJBContext.class javax.ejb.EJBException.class javax.ejb.EJBHome.class ...
H:\q\罗时飞精通EJB3.0.part05.rarH:\q\罗时飞精通EJB3.0.part05.rar
中文版EXPERT-ONE-ON-ONE-J2EE-DEVELOPMENT-WITHOUT-EJB.part2.rar
jboss-ejb3-client.jar
H:\q\罗时飞精通EJB3.0.part12.rarH:\q\罗时飞精通EJB3.0.part12.rar
H:\q\罗时飞精通EJB3.0.part11.rarH:\q\罗时飞精通EJB3.0.part11.rar