简体   繁体   English

在集群的 Java EE 应用程序中并行化作为 @Singleton 实现的计划任务

[英]Parallelization of a scheduled task implemented as @Singleton in a clustered Java EE application

I am currently developing a Java EE application which will be deployed on a Payara server 4. The payara server works on host 1 and there are also 2 instances (host 1 and host 2) available as a Payara cluster.我目前正在开发一个 Java EE 应用程序,该应用程序将部署在 Payara 服务器 4 上。 payara 服务器在主机 1 上工作,还有 2 个实例(主机 1 和主机 2)可用作 Payara 集群。

The problem which I have is, if we schedule a task in the singleton class, then the task will be executed thrice at the same time.我遇到的问题是,如果我们在单例类中安排一个任务,那么该任务将同时执行三次。 The question is 2-fold.问题是2倍。

  • Why does this phenomenon happen?为什么会发生这种现象?
  • How can I avoid such multiple execution?我怎样才能避免这种多次执行?

The entry point looks like this.入口点看起来像这样。 (Not the whole.) (不是全部。)

@ApplicationScoped
@Singleton
public class StartClass {
    public void StartClass() {}
    public void init(@Observes @Initialized(ApplicationScoped.class) ServletContext) throws ... { ... }

    @Schedule(hour="*", minute="0", persistent=false)
    public void runJob() {
        MyClass my_class = new MyClass();
        my_class.do_the_job();
    }
}

Here the scheduled task which I mentioned above is my_class.do_the_job() .这里我上面提到的计划任务是my_class.do_the_job() This method is just to insert a line into a DB and the source code is very similar to this tutorial and has several System.out.println("DO_THE_JOB") .这种方法只是在数据库中插入一行,源代码与本教程非常相似,并且有几个System.out.println("DO_THE_JOB") The target table has a column which is automatically filled with the inserted timestamp.目标表有一列会自动填充插入的时间戳。 I also put an information about the host with System.getenv("HOST") .我还使用System.getenv("HOST")放置了有关主机的信息。 And the result is that we insert three rows every hour: 2 rows from host 1 and 1 row from host 2. But the result of println() can be found once in the log file.结果是我们每小时插入三行:来自主机 1 的 2 行和来自主机 2 的 1 行。 但是println()的结果可以在日志文件中找到一次。

Notes:笔记:

  • Usage of JTA is not the point. JTA 的使用不是重点。 I would like to understand the behaviour of the application and the cluster.我想了解应用程序和集群的行为。
  • Java version is 8 Java 版本是 8
  • If there should be another relevant part, let me know.如果还有其他相关部分,请告诉我。

In the JEE spec there is no way to have a @Singleton which is cluster-wide.在 JEE 规范中,没有办法在集群范围内使用@Singleton

One possible option to get around this is to use the Payara-specific clustered singleton with the additional @Clustered annotation.解决此问题的一种可能选择是使用特定于 Payara 的集群单例和额外的@Clustered注释。 It is available starting from Payara 4.182.它从 Payara 4.182 开始可用。

Read more about it here Payara clustered singleton在这里阅读更多关于它的信息Payara 集群单例

Why does this phenomenon happen?为什么会发生这种现象?

Simply because Singleton beans are not clusterable .仅仅是因为 Singleton bean不可集群 Meaning that each node of the cluster has its own instance of StartClass .这意味着集群的每个节点都有自己的StartClass实例。 You may find additional useful information on a similar SO question .您可能会在类似的 SO 问题上找到其他有用的信息。

How can I avoid such multiple execution?我怎样才能避免这种多次执行?

With your current setup, you can't .使用您当前的设置,您不能 What you possibly need is a scheduler which supports clustering.您可能需要的是一个支持集群的调度程序。 A popular option is quartz-scheduler which supports clustering.一个流行的选项是支持集群的quartz-scheduler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM