简体   繁体   English

在 Spring Boot 应用程序中安排任务的最佳方法是什么

[英]What is best way to schedule task in spring boot application

I am current developing an application based on Spring-Boot.我目前正在开发基于 Spring-Boot 的应用程序。

I know that annotation like @Scheduled can schedule tasks.我知道像@Scheduled 这样的注释可以调度任务。 Since users in my application wanna send mails at different time and send only once.因为我的应用程序中的用户想要在不同的时间发送邮件并且只发送一次。

I have already read the post Spring scheduling task - run only once , but it is weird always "new" an localExecutor in a Spring based application.我已经阅读了帖子Spring 调度任务 - 只运行一次,但是在基于 Spring 的应用程序中总是“新建”一个 localExecutor 是很奇怪的。

In that way , once a user schedule sending an email, I have to "new" an localExecutor for his task.这样,一旦用户安排发送电子邮件,我必须为他的任务“新建”一个 localExecutor。

So , are there any better ways?那么,有没有更好的办法呢?

The simplest way to schedule tasks in Spring is to create method annotated by @Scheduled in spring managed bean.在 Spring 中调度任务最简单的方法是在 spring 管理的 bean 中创建由@Scheduled注释的方法。 It also required @EnableScheduling in any @Configuration classes.它还需要在任何@Configuration类中使用@EnableScheduling

Spring tutorial春季教程

You can use crontab inside @Scheduled您可以在 @Scheduled 中使用 crontab

 private AtomicInteger counter = new AtomicInteger(0);

@Scheduled(cron = "*/2 * * * * *")
public void cronJob() {
    int jobId = counter.incrementAndGet();
    System.out.println("Job " + new Date() + ", jobId: " + jobId);
}

you should use quartz-scheduler and send mails at different time and send only once.您应该使用quartz-schedulersend mails at different time and send only once. - put this as a business logic in your code. - 将其作为业务逻辑放入您的代码中。 Please see for spring boot -quartz integration https://github.com/davidkiss/spring-boot-quartz-demo请参阅 spring boot -quartz 集成https://github.com/davidkiss/spring-boot-quartz-demo

You can use Quartz Scheduler . 您可以使用Quartz Scheduler

Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz是一个功能丰富的开源作业调度库,几乎可以集成在任何Java应用程序中 - 从最小的独立应用程序到最大的电子商务系统。 Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; Quartz可用于创建简单或复杂的计划,以执行数十,数百甚至数万个作业; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. 将任务定义为标准Java组件的作业,这些组件可以执行几乎任何可以编程的程序。 The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering. Quartz Scheduler包含许多企业级功能,例如支持JTA事务和集群。

You may find some blogs, illustrating how to use quartz in spring boot including defining beans of JobDetail, Trigger, factories... etc which is little overwhelming and cumbersome. 你可能会发现一些博客,说明如何在春季启动中使用石英,包括定义JobDetail,Trigger,工厂等的bean,这些都是压倒性和繁琐的。

There is simple way to do the same. 有简单的方法来做同样的事情。

You may give a try to https://github.com/mejariamol/quartz-easy . 您可以尝试https://github.com/mejariamol/quartz-easy This library simplifies the quartz scheduler integration in spring boot framework. 该库简化了spring引导框架中的quartz调度器集成。 I came up with this while setting up quartz scheduler in one of the projects at work. 我在其中一个项目中设置了石英调度程序时提出了这个问题。

  1. Annotate your Job implementation with Scheduled annotation with required configuration by setting suitable fields present in the annotation. 通过设置注释中存在的适当字段,使用具有所需配置的计划注释来注释Job实现。
import com.indusnode.quartz.annotation.Scheduled;
...
@Scheduled(interval="5", intervalType=Scheduled.IntervalType.SEC)
class TestJob implements Job {
    //...
}
  1. Set application property qe.base-package as your base package name of the project which will contain all your job implementations. 将应用程序属性qe.base-package为包含所有作业实现的项目的基本包名称。 Also, add com.indusnode as a value to basePackage in component scan. 另外,在组件扫描中将com.indusnode作为值添加到basePackage。
  2. And you are done! 你完成了! No need to define factories, JobDetail, Triggers...etc. 无需定义工厂,JobDetail,Triggers等。

To use this library, include the quartz-easy artifact in the dependencies section of your pom.xml 要使用此库,请在pom.xml的dependencies部分中包含quartz-easy工件

<dependency>
  <groupId>com.indusnode</groupId>
  <artifactId>quartz-easy</artifactId>
  <version>1.0.0</version>
</dependency>

For details, please refer https://search.maven.org/artifact/com.indusnode/quartz-easy/1.0.0/jar 有关详细信息,请参阅https://search.maven.org/artifact/com.indusnode/quartz-easy/1.0.0/jar

Spring @Scheduled annotation will execute multiple times if you have more than one instance of your app where you have @Scheduled annotation.如果您的应用程序实例中有多个 @Scheduled 注释,则 Spring @Scheduled 注释将执行多次。

If you are using PCF, you can use PCF scheduler https://docs.pivotal.io/scheduler/1-2/using-jobs.html to avoid this issue.如果您使用 PCF,则可以使用 PCF 调度程序https://docs.pivotal.io/scheduler/1-2/using-jobs.html来避免此问题。 Using Tasks can solve this issue.使用 Tasks 可以解决这个问题。

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

相关问题 在Spring Boot中对时间表任务进行软编码的最佳方法是什么? - What is the best way for softcoding schedule tasks in Spring Boot? Spring Boot 应用程序组织的最佳实践是什么? - What are best practices for Spring Boot application organization? 在 Spring Boot 中处理异常的最佳方法是什么? - What is the best way to handle exception in Spring Boot? 如何在Spring Boot中安排任务并增加延迟? - How to schedule a task in spring boot with increasing delay? 应该测试Spring Boot应用程序的最佳方法是什么? 参与服务器还是通过创建模拟环境? - What is the best way Spring Boot application should be tested ? With server involvement or by creating mock environment? 部署Spring引导应用程序的正确方法是什么 - What is the correct way to deploy a spring boot application 扩展spring应用程序上下文的最佳方法是什么? - What is the best way of extending spring application context? 安排Java中某些任务的最佳方法 - Best way to schedule certain task in Java 如果我有任务执行器,对我的 spring 启动应用程序有什么影响 - What are impact to my spring boot application if I have task executor 将一些 JSON 文件加载到 Spring Boot 应用程序中的最佳方法 - Best way to load some JSON files into a Spring Boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM