定时器服务使用计划应用程序可以建立一个机制。例如,每月1日的工资单生成。 EJB3.0规范指定超时注释,这有助于编程一个无状态或消息驱动Bean的EJB服务。 EJB容器调用的方法,这是注释@Timeout.
EJB计时器服务是有助于创造的定时器,并安排回调计时器到期时由EJB容器提供的服务。
创建定时器的步骤
使用@ Resource注解注入SessionContext的bean
@Stateless
public class TimerSessionBean {
@Resource
private SessionContext context;
...
}
使用SessionContext对象TimerService创造定时器的。传递时间(以毫秒为单位)和消息。
public void createTimer(long duration) {
context.getTimerService().createTimer(duration, "Hello World!");
}
使用定时器的步骤
使用@Timeout批注的方法。返回类型必须为void,并传递一个参数类型的定时器。我们取消计时器后第一次执行,否则将继续运行,修正后的时间间隔。
@Timeout
public void timeOutHandler(Timer timer){
System.out.println("timeoutHandler : " + timer.getInfo());
timer.cancel();
}
示例应用程序
让我们创建一个测试测试计时器服务在EJB的EJB应用程序中。
Step描述
1Create a project with a name EjbComponent under a package com.tutorialspoint.timer as explained in the EJB - Create Application chapter.
2Create TimerSessionBean.java and TimerSessionBeanRemote as explained in the EJB - Create Application chapter. Keep rest of the files unchanged.
3Clean and Build the application to make sure business logic is working as per the requirements.
4Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.
5Now create the ejb client, a console based application in the same way as explained in theEJB - Create Application chapter under topic Create Client to access EJB.
EJBComponent (EJB Module)
TimerSessionBean.java
package com.tutorialspoint.timer;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
@Stateless
public class TimerSessionBean implements TimerSessionBeanRemote {
@Resource
private SessionContext context;
public void createTimer(long duration) {
context.getTimerService().createTimer(duration, "Hello World!");
}
@Timeout
public void timeOutHandler(Timer timer){
System.out.println("timeoutHandler : " + timer.getInfo());
timer.cancel();
}
}
TimerSessionBeanRemote.java
package com.tutorialspoint.timer;
import javax.ejb.Remote;
@Remote
public interface TimerSessionBeanRemote {
public void createTimer(long milliseconds);
}
一旦你在Jboss应用服务器部署EjbComponent项目,发现jboss日志。
JBoss已经自动为我们的会话bean创建一个JNDI条目 -TimerSessionBean/remote.
我们将使用这个查询字符串来获得远程类型的业务对象 -com.tutorialspoint.timer.TimerSessionBeanRemote
JBoss应用服务器的日志输出
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
TimerSessionBean/remote - EJB3.x Default Remote Business Interface
TimerSessionBean/remote-com.tutorialspoint.timer.TimerSessionBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=TimerSessionBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.timer.TimerSessionBeanRemote ejbName: TimerSessionBean
...
EJBTester (EJB Client)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
这些属性是用来初始化InitialContext对象的Java命名服务
InitialContext对象将被用于查找无状态会话bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.stateful.TimerSessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testTimerService();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options 1. Add Book 2. Exit Enter Choice: ");
}
private void testTimerService(){
try {
TimerSessionBeanRemote timerServiceBean = (TimerSessionBeanRemote)ctx.lookup("TimerSessionBean/remote");
System.out.println("["+(new Date()).toString()+ "]" + "timer created.");
timerServiceBean.createTimer(2000);
} catch (NamingException ex) {
ex.printStackTrace();
}
}
}
EJBTester做以下任务。
jndi.properties中加载和初始化的InitialContext对象。
在testTimerService()方法,名字完成JNDI查找 - “TimerSessionBean/remote”,以获得远程业务对象(定时器无状态EJB)。
然后的调用createTimer通过预定时间为2000毫秒。
2秒后EJB容器调用timeoutHandler,方法。
运行客户端访问EJB
在项目资源管理器中找到EJBTester.java。右键点击上EJBTester类,并选择run file.
在Netbeans控制台验证以下输出。
run:
[Wed Jun 19 11:35:47 IST 2013]timer created.
BUILD SUCCESSFUL (total time: 0 seconds)
JBoss应用服务器的日志输出
你可以找到以下的回调在JBoss日志项
...
11:35:49,555 INFO [STDOUT] timeoutHandler : Hello World!
...
(编辑:雷林鹏 来源:网络|侵删)