通过Spring编程安排作业(动态设置fixedRate)


72

目前我有这个:

@Scheduled(fixedRate=5000)
public void getSchedule(){
   System.out.println("in scheduled job");
}

我可以更改它以使用对属性的引用

@Scheduled(fixedRateString="${myRate}")
public void getSchedule(){
   System.out.println("in scheduled job");
}

但是,我需要使用通过编程获得的值,以便可以在不重新部署应用程序的情况下更改计划。什么是最好的方法?我意识到可能无法使用注释...


您说“不重新部署应用程序”。更改属性引用可以通过重新启动应用程序而无需重新部署(例如,通过更新系统属性然后重新启动)。这是否足够,还是您希望能够在不重新部署或重新启动的情况下进行更改?
M. Justin

Answers:


122

使用aTrigger可以动态计算下一次执行时间。

这样的事情应该可以解决问题(从Javadoc@EnableScheduling改编):

@Configuration
@EnableScheduling
public class MyAppConfig implements SchedulingConfigurer {

    @Autowired
    Environment env;

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }

    @Bean(destroyMethod = "shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
        taskRegistrar.addTriggerTask(
                new Runnable() {
                    @Override public void run() {
                        myBean().getSchedule();
                    }
                },
                new Trigger() {
                    @Override public Date nextExecutionTime(TriggerContext triggerContext) {
                        Calendar nextExecutionTime =  new GregorianCalendar();
                        Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
                        nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
                        nextExecutionTime.add(Calendar.MILLISECOND, env.getProperty("myRate", Integer.class)); //you can get the value from wherever you want
                        return nextExecutionTime.getTime();
                    }
                }
        );
    }
}

4
查看您的代码-执行以下行时,您是否未遇到NullPointerException nextExecutionTime.setTime(triggerContext.lastActualExecutionTime());null当应用程序启动时,triggerContext将返回。
jsf 2013年

2
并且有一种方法可以中断当前触发器并在其休眠时更改其值。
jsf 2013年

2
我只确保可以编译,但从未运行过。
2013年

3
我在项目中使用了它,效果很好。快速修复:lastActualExecutionTime!= null?lastActualExecutionTime:新的Date()
亚历山大

2
@AlexanderSchwarz:谢谢,纳入编辑修复
阿赫

13

您也可以为此使用Spring Expression Language(SpEL)。

初始化该值后,您将无法更新此值。

@Scheduled(fixedRateString = "#{@applicationPropertyService.getApplicationProperty()}")
public void getSchedule(){
   System.out.println("in scheduled job");
}

@Service
public class ApplicationPropertyService {

    public String getApplicationProperty(){
        //get your value here
        return "5000";
    }
}

1
我更喜欢此代码,而不是所选代码,因为它更少的代码和简洁。
佩马西(Pemassi)

您仍然可以通过将bean范围设置为RefreshScope并通过实现RefreshScopeRefreshedEvent来更改值。此处的示例应用程序github.com/winster/SpringSchedulerDynamic
Winster

我的问题是,如果我们重写AppProp.Service类并给出两个不同的延迟时间,则调度程序将运行两次?
mfaisalhyder

不,它不会,它将使用重写的属性,如果您希望调度程序多次运行,请使用CRON
Sagar Ahuja

8

您也可以使用这种简单的方法:

private int refreshTickNumber = 10;
private int tickNumber = 0; 

@Scheduled(fixedDelayString = "${some.rate}")
public void nextStep() {
    if (tickNumber < refreshTickNumber) {
        tickNumber++;
        return;
    }
    else {
        tickNumber = 0;
    }
    // some code
}

refreshTickNumber在运行时可以完全配置,并且可以与@Value注释一起使用。


11
不太有用,会引入过多的开销
Rade_303

2
其实没有那么多
祈祷

1
如果您像我一样想要做的是动态地上下调整(以的间隔some.rate// some code计划的任务实际运行的频率,则此方法有效。但这实际上并不能回答有关动态设置的值的问题fixedRateString。这样,任务仍会在每个some.rate时间间隔触发,但是the business code任务中的任务只有在“笔数计数”刷新时才运行。问题和其他答案是关于调整何时触发任务以直接控制the business code任务在何时运行。权衡。
–geneSummons

6

您可以使用TaskScheduler和ScheduledFuture管理重启计划:

@Configuration
@EnableScheduling
@Component
public class CronConfig implements SchedulingConfigurer , SchedulerObjectInterface{

    @Autowired
    private ScheduledFuture<?> future;

     @Autowired
        private TaskScheduler scheduler;

    @Bean
    public SchedulerController schedulerBean() {
        return new SchedulerController();
    }

    @Bean(destroyMethod = "shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    } 

        @Override
    public void start() {
        future = scheduler.schedule(new Runnable() {
            @Override
            public void run() {
                //System.out.println(JOB + "  Hello World! " + new Date());
                schedulerBean().schedulerJob();
            }
        }, new Trigger() {
            @Override public Date nextExecutionTime(TriggerContext triggerContext) {
                Calendar nextExecutionTime =  new GregorianCalendar();
                Date lastActualExecutionTime = triggerContext.lastActualExecutionTime(); 
           nextExecutionTime.setTime(convertExpresssiontoDate());//you can get the value from wherever you want
                return nextExecutionTime.getTime();
            }
        });

    }


    @Override
    public void stop() {
        future.cancel(true);

    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // TODO Auto-generated method stub
        start();
    }

}

启动停止界面:

public interface SchedulerObjectInterface {    
    void start();
    void stop();
}

现在您可以停止并重新开始(重新启动),使用@Autowired SchedulerObjectInterface进行调度


2

要创建和管理多个动态安排的任务,

计划配置和bean:

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Configuration
public class SchedulingConfigs implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                // Do not put @Scheduled annotation above this method, we don't need it anymore.
                System.out.println("Running Schedular..." + Calendar.getInstance().getTime());
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                Calendar nextExecutionTime = new GregorianCalendar();
                Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
                nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
                nextExecutionTime.add(Calendar.MILLISECOND, getNewExecutionTime());
                return nextExecutionTime.getTime();
            }
        });
    }

    private int getNewExecutionTime() {
        //Load Your execution time from database or property file
        return 1000;
    }

    @Bean
    public TaskScheduler poolScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        scheduler.setPoolSize(1);
        scheduler.initialize();
        return scheduler;
    }
}

计划程序服务代码:

package io.loadium.resource.service;

import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

@Service
public class ScheduleTaskService {

    // Task Scheduler
    TaskScheduler scheduler;

    // A map for keeping scheduled tasks
    Map<Integer, ScheduledFuture<?>> jobsMap = new HashMap<>();

    public ScheduleTaskService(TaskScheduler scheduler) {
        this.scheduler = scheduler;
    }


    // Schedule Task to be executed every night at 00 or 12 am
    public void addTaskToScheduler(int id, Runnable task, Date runningDate) {
        ScheduledFuture<?> scheduledTask = scheduler.schedule(task, runningDate);
        jobsMap.put(id, scheduledTask);
    }

    // Remove scheduled task
    public void removeTaskFromScheduler(int id) {
        ScheduledFuture<?> scheduledTask = jobsMap.get(id);
        if (scheduledTask != null) {
            scheduledTask.cancel(true);
            jobsMap.put(id, null);
        }
    }

    // A context refresh event listener
    @EventListener({ContextRefreshedEvent.class})
    void contextRefreshedEvent() {
        // Get all tasks from DB and reschedule them in case of context restarted
    }
}

用法示例:

// Add a new task with runtime after 10 seconds
scheduleTaskService.addTaskToScheduler(1, () -> System.out.println("my task is running -> 1"), , Date.from(LocalDateTime.now().plusSeconds(10).atZone(ZoneId.systemDefault()).toInstant()));
// Remove scheduled task
scheduleTaskService.removeTaskFromScheduler(1);

0

简单的Spring Boot示例仅限于秒,分钟和小时的间隔。此示例的目的是演示条件处理两个属性TimeUnit和interval。

特性:

snapshot.time-unit=SECONDS
snapshot.interval=5

预定方法:

@Scheduled(cron = "*/1 * * * * *")
protected void customSnapshotScheduler()
{
    LocalDateTime now = LocalDateTime.now();
    TimeUnit timeUnit = TimeUnit.valueOf(snapshotProperties.getSnapshot().getTimeUnit());
    int interval = snapshotProperties.getSnapshot().getInterval();

    if (TimeUnit.SECONDS == timeUnit
            && now.getSecond() % interval == 0)
    {
        this.camService.writeSnapshot(webcam.getImage());
    }

    if (TimeUnit.MINUTES == timeUnit
            && now.getMinute() % interval == 0)
    {
        this.camService.writeSnapshot(webcam.getImage());
    }

    if (TimeUnit.HOURS == timeUnit
            && now.getHour() % interval == 0)
    {
        this.camService.writeSnapshot(webcam.getImage());
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.