简体   繁体   English

java scheduler spring vs quartz

[英]java scheduler spring vs quartz

Currently I am building a spring standalone program in order to learn new methods and architectures. 目前我正在构建一个spring独立程序,以便学习新的方法和架构。
The last few days I tried to learn scheduler. 最近几天我试着学习调度程序。 I never used them before so I read some articles handling the different possible methods. 我之前从未使用它们,因此我阅读了一些处理不同方法的文章。 Two of them are especially interesting: The spring nativ @Scheduler and Quartz. 其中两个特别有趣:春天nativ @Scheduler和Quartz。

From what I read, Spring is a little bit smaller then Quartz and much more basic. 从我读到的内容来看,Spring比Quartz小一点,而且更基本。 And quartz is not easy to use with spring (because of the autowired and components). 石英不易与弹簧一起使用(因为自动装配和组件)。

My problem now is, that there is one thing I do not understand: 我现在的问题是,有一件事我不明白:
From my understanding, both methods are creating parallel Threads in order to asynchronously run the jobs. 根据我的理解,这两种方法都是创建并行线程,以便异步运行作业。 But what if I now have a spring @Service in my main Application, that is holding a HashMap with some information. 但是如果我现在在我的主应用程序中有一个spring @Service,那就是拿着一些带有一些信息的HashMap。 The data is updated and changed with user interaction. 通过用户交互更新和更改数据。 Parallel there are the scheduler. 并行有调度程序。 And a scheduler now whants to use this HashMap from the main application as well. 并且调度程序现在也想要从主应用程序中使用此HashMap。 Is this even possible? 这甚至可能吗?
Or do I understand something wrong? 或者我理解错了什么? Because there is also the @Async annotation and I did not understand the difference. 因为还有@Async注释,我不明白其中的区别。 Because a scheduler itself is already parallel to the main corpus, isn't it? 因为调度程序本身已经与主语料库平行,不是吗?

(summing up, two questions: (总结,两个问题:

  • can a job that is executed every five seconds, implemented with a scheduler, use a HashMap out of a service inside the main program? 可以使用调度程序实现每五秒执行一次的作业,在主程序中使用HashMap吗? (in spring @Scheduler and/or in Quartz?) (在春天@Scheduler和/或Quartz?)
  • Why is there a @Async annotation. 为什么有@Async注释。 Isn't a scheduler already parallel to the main process? 调度程序是否已经与主进程并行?

)

I have to make a few assumptions about which version of Spring you're using but as you're in the process of learning, I would assume that you're using spring-boot or a fairly new version, so please excuse if the annotations don't match your version of Spring. 我必须对你正在使用的Spring版本做一些假设,但是当你正在学习的过程中,我会假设你使用的是spring-boot或者是一个相当新的版本,所以请原谅是否有注释与您的Spring版本不符。 This said, to answer your two questions the best I can: 这就是说,尽我所能回答你的两个问题:

can a job that is executed every five seconds, implemented with a scheduler, use a HashMap out of a service inside the main program? 可以使用调度程序实现每五秒执行一次的作业,在主程序中使用HashMap吗? (in spring @Scheduler and/or in Quartz?) (在春天@Scheduler和/或Quartz?)

Yes, absolutely! 是的,一点没错! The easiest way is to make sure that the hashmap in question is declared as static. 最简单的方法是确保将有问题的hashmap声明为static。 To access the hashmap from the scheduled job, simply either autowire your service class or create a static get function for the hashmap. 要从计划作业访问hashmap,只需要自动装配服务类或为hashmap创建静态get函数。

Here is an example of a recent Vaadin project where I needed a scheduled message sent to a set of subscribers. 这是一个最近的Vaadin项目的例子,我需要一个发送给一组订户的预定消息。

SchedulerConfig.class SchedulerConfig.class

@Configuration
@EnableAsync
@EnableScheduling
public class SchedulerConfig {

    @Scheduled(fixedDelay=5000)
    public void refreshVaadinUIs() {
        Broadcaster.broadcast( 
                new BroadcastMessage(
                    BroadcastMessageType.AUTO_REFRESH_LIST 
                ) 
        );
    }

}

Broadcaster.class Broadcaster.class

public class Broadcaster implements Serializable {

    private static final long serialVersionUID = 3540459607283346649L;

    private static ExecutorService executorService = Executors.newSingleThreadExecutor();

    private static LinkedList<BroadcastListener> listeners = new LinkedList<BroadcastListener>();

    public interface BroadcastListener {
        void receiveBroadcast(BroadcastMessage message);
    }   

    public static synchronized void register(BroadcastListener listener) {
        listeners.add(listener);
    }

    public static synchronized void unregister(BroadcastListener listener) {
        listeners.remove(listener);
    }

    public static synchronized void broadcast(final BroadcastMessage message) {
        for (final BroadcastListener listener: listeners)
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    listener.receiveBroadcast(message);
                }
            });
    }
}

Why is there a @Async annotation. 为什么有@Async注释。 Isn't a scheduler already parallel to the main process? 调度程序是否已经与主进程并行?

Yes, the scheduler is running in its own thread but what occurs to the scheduler on long running tasks (ie: doing a SOAP call to a remote server that takes a very long time to complete)? 是的,调度程序在其自己的线程中运行,但调度程序在长时间运行的任务中会发生什么(即:对需要很长时间才能完成的远程服务器进行SOAP调用)?

The @Async annotation isn't required for scheduling but if you have a long running function being invoked by the scheduler, it becomes quite important. 调度不需要@Async注释,但如果调度程序调用了长时间运行的函数,则它变得非常重要。

This annotation is used to take a specific task and request to Spring's TaskExecutor to execute it on its own thread instead of the current thread. 此注释用于执行特定任务并请求Spring的TaskExecutor在其自己的线程而不是当前线程上执行它。 The @Async annotation causes the function to immediately return but execution will be later made by the TaskExecutor. @Async注释使函数立即返回,但稍后将由TaskExecutor执行。

This said, without the @EnableAsync or @Async annotation, the functions you call will hold up the TaskScheduler as they will be executed on the same thread. 这样说,如果没有@EnableAsync或@Async注释,你调用的函数将保持TaskScheduler,因为它们将在同一个线程上执行。 On a long running operation, this would cause the scheduler to be held up and unable to execute any other scheduled functions until it returns. 在长时间运行的操作中,这将导致调度程序被挂起并且在返回之前无法执行任何其他调度的函数。

I would suggest a read of Spring's Documentation about Task Execution and Scheduling It provides a great explanation of the TaskScheduler and TaskExecutor in Spring 我建议读一下Spring关于任务执行和调度的文档它提供了Spring中TaskScheduler和TaskExecutor的一个很好的解释。

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

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