部署Java EE应用程序后执行任务 [英] Executing task after deployment of Java EE application

查看:70
本文介绍了部署Java EE应用程序后执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java EE应用程序,它应该在部署后与外部系统一起启动同步过程。

I have a Java EE application which should start a synchronization process with an external system once after its deployment.

我怎样才能实现这个要求?

How could I implement this requirement?

推荐答案

我测试了建议的解决方案使用 @Startup @PostConstruct 注释。事实证明,在使用 @PostConstruct 注释的所有方法完成之前,Glassfish不会完成应用程序的部署。所以在我的情况下,部署需要几分钟到一个小时。

I tested the suggested solution which uses the @Startup and @PostConstruct annotations. It turned out that Glassfish does not complete the deployment of an application until all methods annotated with @PostConstruct have finished. So in my case the deployment would take from several minutes up to an hour.

但我想出了一个不同的方式来实现我想要的东西。最好的解决方案似乎是一个定时器回调方法,它在执行后取消它的定时器。

But I figured out a different way to achive what I want. The best solution seems to be a timer callback method which cancels its timer after its execution.

@Stateless
public class SynchronisationService {
    @Schedule(hour = "*", minute = "*", persistent = false)
    protected void init(Timer timer)
    {
       doTheSync();

       timer.cancel();
    }
 }

使用非持久性计时器可以使计时器成为如果重新启动应用程序服务器,则重新创建。

Using a non-persistent timer allows the timer to be re-created if the application server is restarted.

这篇关于部署Java EE应用程序后执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆