如何在 JBOSS 4.3 中使用 EJB 3.0 创建作业

发布于 12-26 07:39 字数 982 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撞了怀2025-01-02 07:39:24

在 JBoss 中,您可以使用通过使用 @Service 注释服务来定义的 MBean。如果有 public void start() 方法,则在应用程序启动时将调用该方法。

In JBoss you could use MBeans which are defined by annotating a service with @Service. If there is a public void start() method this will be called when the application is started.

燃情2025-01-02 07:39:24

Servlet 侦听器或启动时加载 servlet 是在部署时自动运行某些代码的唯一 javaee 可移植方式,直到带有 @Schedule 注释的 EJB 3.1。

Servlet listeners or load-on-startup servlets were the only javaee-portable way to autorun some code on deploy until EJB 3.1 with @Schedule annotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文