📅  最后修改于: 2020-11-16 06:19:39             🧑  作者: Mango
计时器服务是一种可以构建计划的应用程序的机制。例如,每个月1日生成工资单。 EJB 3.0规范指定了@Timeout批注,该批注有助于在无状态或消息驱动的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,并传递Timer类型的参数。我们将在第一次执行后取消计时器,否则它将在修复间隔后继续运行。
@Timeout
public void timeOutHandler(Timer timer) {
System.out.println("timeoutHandler : " + timer.getInfo());
timer.cancel();
}
让我们创建一个测试EJB应用程序以测试EJB中的Timer Service。
Step | Description |
---|---|
1 |
Create a project with a name EjbComponent under a package com.tutorialspoint.timer as explained in the EJB – Create Application chapter. |
2 |
Create TimerSessionBean.java and TimerSessionBeanRemote as explained in the EJB – Create Application chapter. Keep rest of the files unchanged. |
3 |
Clean and Build the application to make sure business logic is working as per the requirements. |
4 |
Finally, 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. |
5 |
Now create the EJB client, a console based application in the same way as explained in the EJB – Create Application chapter under topic Create Client to access EJB. |
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();
}
}
package com.tutorialspoint.timer;
import javax.ejb.Remote;
@Remote
public interface TimerSessionBeanRemote {
public void createTimer(long milliseconds);
}
在JBOSS上部署EjbComponent项目后,请注意jboss日志。
JBoss已经为我们的会话bean − TimerSessionBean / remote自动创建了一个JNDI条目。
我们将使用此查找字符串获取类型为com.tutorialspoint.timer.TimerSessionBeanRemote的远程业务对象。
...
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
...
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
这些属性用于初始化Java命名服务的InitialContext对象。
InitialContext对象将用于查找无状态会话bean。
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 \n1. Add Book\n2. Exit \nEnter 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()方法中,使用名称“ TimerSessionBean / remote”完成jndi查找,以获取远程业务对象(定时器无状态EJB)。
然后调用createTimer并传递2000毫秒作为计划时间。
EJB容器在2秒钟后调用timeoutHandler方法。
在项目资源管理器中找到EJBTester.java。右键单击EJBTester类,然后选择运行文件。
在Netbeans控制台中验证以下输出。
run:
[Wed Jun 19 11:35:47 IST 2013]timer created.
BUILD SUCCESSFUL (total time: 0 seconds)
您可以在JBoss日志中找到以下回调条目
...
11:35:49,555 INFO [STDOUT] timeoutHandler : Hello World!
...