我有这门课
@Service
@Profile("async")
public class MyServiceImplAsync implements MyService {
..
@Async
@Override
public void printGreetings(String name) {
//do something and sleep 10 seconds
}
@Async
@Override
public Future<String> doSomething(String text) {
//do something and sleep 25 seconds
return new AsyncResult<String>(text);
}
}
Run Code Online (Sandbox Code Playgroud)
观察,两种方法使用 @Async
现在,我也有以下内容:
@Component
public class MySchedule {
..
@Scheduled(cron="*/5 * * * * ?")
public void schedule(){
logger.info("Schedule working at {}", simpleDateFormat.format( new Date() ));
this.myService.printGreetings("Manolito");
}
@Scheduled(cron="*/30 * * * * ?")
public void scheduleFuture(){
logger.info("Schedule …Run Code Online (Sandbox Code Playgroud) 如何动态地使用Spring的@Scheduled注释?
CronTrigger(String expression, TimeZone timeZone)
Run Code Online (Sandbox Code Playgroud)
由于我在数据库中有多个时区,我该如何动态传递它们?
我在我的代码中试过这个:
TimeZone timezone = null;
String timezone1 = null;
public SchedulerBean(String timezone2)
{
this.timezone1 = timezone2;
//constructor
}
@Scheduled(cron="0 0 8 * * ?", zone =timezone.getTimeZone(timezone1) ) //Error at this line
public void sendQuestionNotif()
{
//......code
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误,
*Type mismatch: cannot convert from TimeZone to String*
Run Code Online (Sandbox Code Playgroud)
请帮我.因为我想根据时区触发cron.TIA.
我的春季服务喜欢
@Scheduled( cron="0 0 7 * * SUN")
public void doSomething() {
// do something
}
Run Code Online (Sandbox Code Playgroud)
我知道你不能拥有为指定一年保留的第7个值.使用表达式我可以告诉spring在特定时间每年运行一次,比如说在2020年12月25日上午6点?
谢谢
我有一个 Spring-Boot 应用程序,它将成为我们想要触发的其他几个进程的编排服务。我目前使用 Spring Scheduling 从数据库中动态拉取 crons 进行了设置。我加入了一个 rest 方法来触发从数据库中提取新的 cron 信息的过程。这个逻辑一切正常。唯一的“问题”是它不使用新的 cron 信息,直到下一次计划运行才能解决真正的问题。 有没有办法中断当前的触发器并使用更新的 cron 信息再次安排一个。 以下是申请参考:
package com.bts.poc;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@SpringBootApplication
@EnableScheduling
@RestController
@RequestMapping("/APSCommon/Scheduling")
public class Application implements SchedulingConfigurer {
@Autowired
private DynamicCron dynamicCron;
@Autowired
PropertyManager propertyManager;
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
private String cronConfig() {
String …Run Code Online (Sandbox Code Playgroud) @Scheduled我的应用程序中有一个任务,设置使用CRON并每4小时运行一次.我面临的问题是,CRON应用程序启动后作业不会立即启动,但它只在应用程序启动后4小时启动.
我试图@PostConstruct在任务中使用一个方法来调用它,但是由于未初始化的Spring上下文而导致错误.
请告诉我如何在应用程序部署时立即运行计划任务,然后在部署后每4小时运行一次.
编辑:
我不会使用,@PostConstruct因为我的预定方法取决于其他Bean,当这个PostConstruct方法由于某种原因运行时未初始化.
在我的Spring Boot应用程序中,基于Cron作业(每5分钟运行一次),我需要在数据库中处理2000种产品。
目前,这2000种产品的处理时间超过5分钟。我遇到了一个问题,当第一个Cron作业尚未完成时,第二个Cron作业将运行。
Spring / Cron中是否具有开箱即用的功能,该功能可以同步这些作业并等待上一个作业完成后再开始下一个作业?
请告知如何正确实施此类系统。无论如何,Neo4j,MongoDB,Kafka也可以使用以下技术。请告知如何单独或什至与上述技术一起使用Spring / Cron正确设计/实现此功能。
这很奇怪,我把玉米设置为
@Scheduled(cron= "0 0/10 * * * ? ")
Run Code Online (Sandbox Code Playgroud)
它确实每10分钟触发一次,但问题是每秒执行一次任务.
我正在做一个计划项目,该项目定期执行多个作业。我正在使用cron调度,如下例所示。作业已成功执行,没有问题。但是,出于需求,我想计算并保留DB中计划作业的下一次运行时间。对于以下配置,是否有解决方案来获取作业的上一次和下一次启动时间?
配置示例:
import java.util.Date;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class DemoServiceBasicUsageCron
{
@Scheduled(cron="1,3,30,32 * * * * ?")
public void demoServiceMethod()
{
System.out.println("Curent date time is - "+ new Date());
}
}
Run Code Online (Sandbox Code Playgroud) 我们可以在 Spring 启动应用程序中使用 @Scheduled 轻松安排作业。
有什么方法可以使用任何休息端点知道当前正在运行的计划作业吗?
我想测试我的 Cron-job 是否会在给定时间(每年 1 月 1 日 00:00)执行。有没有办法测试这个?不幸的是,我在网上找到的每个示例都是指将在固定时间段后执行的作业,例如全部 5 秒。(https://www.baeldung.com/spring-testing-scheduled-annotation)。
@Scheduled(cron = " 0 0 1 1 *")
public void myFunc() {
do something
}
Run Code Online (Sandbox Code Playgroud)