gpt4 book ai didi

java - 在下次调用之前中断 spring scheduler 任务

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:17 26 4
gpt4 key购买 nike

我有一个 Spring-Boot 应用程序,它将成为我们想要触发的其他几个进程的编排服务。我目前使用 Spring Scheduling 从数据库中动态拉取 crons 来设置它。我加入了一个 rest 方法来触发从数据库中提取新的 cron 信息的过程。这个逻辑都正常工作。唯一的“问题”是它在下一次计划运行到达真正的问题之前不使用新的 cron 信息。 有没有办法中断当前的Trigger,并使用更新的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 cronTabExpression = propertyManager.getProperty("COMPANY", "JOB_NAME","CRON_EXPRESSION");
return cronTabExpression;
}

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
dynamicCron.runJob();
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cron = cronConfig();
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
DynamicCron.cronExpression = cron;
return nextExec;
}
});
}

@RequestMapping(value = "/reloadScheduling", method = RequestMethod.GET)
public String reloadScheduling() {
PropertyManager.setResetProperties(true);
return "schedules will be altered next run";
}
}

最佳答案

因此,使用 SchedulingConfigurer->configureTasks,您无法访问我正在使用的 Spring 版本 (4.2.7.RELEASE) 中的 ScheduledFuture(s)。从我读过的几篇文章中,它被提到为 future 可能的功能。我通过执行以下操作解决了这个问题:

package com.bts.poc;

import com.bts.poc.service.DynamicCron;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ScheduledFuture;

@SpringBootApplication(exclude = MessageSourceAutoConfiguration.class)
@EnableScheduling
@RestController
public class Application extends SpringBootServletInitializer {

@Autowired
private DynamicCron dynamicCron;
@Autowired
private PropertyManager propertyManager;
private static List<ScheduledFuture> scheduledFutures = new ArrayList<>();
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
private static TaskScheduler scheduler;

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

private String cronConfig() {
return propertyManager.getProperty("COMPANY", "JOB_NAME", "CRON_EXPRESSION");
}

@RequestMapping(value = {"scheduling/start"}, method = RequestMethod.GET)
public @ResponseBody String startScheduling() {
scheduleAll();

LOGGER.info("Scheduling of jobs has been started.");
return "Scheduling of jobs has been started.";
}

@RequestMapping(value = {"scheduling/cancel"}, method = RequestMethod.GET)
public @ResponseBody String cancelScheduling() {
cancelAll();

LOGGER.info("Cancelling all scheduled jobs.");
return "Cancelling all scheduled jobs.";
}

private void scheduleAll() {
LOGGER.info("Scheduling all applications to run.");
cancelAll();

//eventually go through the database and load all jobs to be scheduled here.
schedule(cronConfig());
}

/**
* Cancel all the scheduled reports
*/
private void cancelAll() {
for (ScheduledFuture scheduledFuture : scheduledFutures) {
scheduledFuture.cancel(true);
}
scheduledFutures.clear();
}

/**
* Schedule the scheduled report with the given cron schedule information
*/
private void schedule(String cronSchedule) {
TimeZone tz = TimeZone.getDefault();
LOGGER.info("Setting up application {} to execute with cron string: '{}'.", cronSchedule);
CronTrigger trigger = new CronTrigger(cronSchedule, tz);

scheduler = scheduler();
if (scheduler == null) {
LOGGER.error("Unable to schedule job as scheduler was not found");
return;
}

ScheduledFuture<?> future = scheduler.schedule(new DynamicCron(), trigger);
scheduledFutures.add(future);
}

@Bean
public TaskScheduler scheduler() {
if (scheduler == null) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
scheduler.afterPropertiesSet();
}
return scheduler;
}
}

这基本上复制了 ScheduledTaskRegistrar 提供的允许您管理 ScheduledFuture(s) 的功能。希望这可以在将来帮助其他人。

关于java - 在下次调用之前中断 spring scheduler 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39152599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com