如何在 JBOSS 4.3 中使用 EJB 3.0 创建作业
如何创建每 6 秒运行一次的作业,并在执行应用程序部署时自动开始工作?
目前我有这个使用计时器的类,但启动部署时该过程不会自动启动。
所有这一切都通过 JBoss 4.3 中的 EJB 3.0 和 EAR 实现,无需 WAR,仅使用 EJB
@Stateless
public class MyJobBean implements MyJob {
private static final long EJECUTION_INTERVAL=6000;
@Resource
private TimerService ts;
@PostConstruct // This not work in deploy, only when somebody calls the EJB
public void init() {
System.out.println("Post Constructor Method init() Invoked");
ts.createTimer(EJECUTION_INTERVAL, null);
}
@Timeout // Fired by the timer
public void myJOBProcess(Timer timer) {
System.out.println("the job was invoked");
ts.createTimer(EJECUTION_INTERVAL, null); //create another future ejecution
}
@PreDestroy
public void tidyUp() {
System.out.println("Canceling scheduled Timers");
for (Object obj : ts.getTimers()) {
Timer timer = (Timer)obj;
timer.cancel();
System.out.println("Timer Canceled");
}
}
}
提前致谢
How I can create a job that runs every 6 seconds and the work begins automatically when performing the deploy of the application?
Currently I have this class that use timers, but the process does not start automatically when start the deploy.
All this with EJB 3.0 in JBoss 4.3 and the EAR without a WAR, only EJBs
@Stateless
public class MyJobBean implements MyJob {
private static final long EJECUTION_INTERVAL=6000;
@Resource
private TimerService ts;
@PostConstruct // This not work in deploy, only when somebody calls the EJB
public void init() {
System.out.println("Post Constructor Method init() Invoked");
ts.createTimer(EJECUTION_INTERVAL, null);
}
@Timeout // Fired by the timer
public void myJOBProcess(Timer timer) {
System.out.println("the job was invoked");
ts.createTimer(EJECUTION_INTERVAL, null); //create another future ejecution
}
@PreDestroy
public void tidyUp() {
System.out.println("Canceling scheduled Timers");
for (Object obj : ts.getTimers()) {
Timer timer = (Timer)obj;
timer.cancel();
System.out.println("Timer Canceled");
}
}
}
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

在 JBoss 中,您可以使用通过使用
@Service
注释服务来定义的 MBean。如果有public void start()
方法,则在应用程序启动时将调用该方法。In JBoss you could use MBeans which are defined by annotating a service with
@Service
. If there is apublic void start()
method this will be called when the application is started.