以编程方式使用Spring调度作业(动态设置fixedRate)

时间:2013-01-31 16:29:12

标签: java spring scheduled-tasks spring-annotations

目前我有这个:

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

我可以将其更改为使用对属性的引用

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

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

6 个答案:

答案 0 :(得分:97)

使用Trigger,您可以动态计算下一个执行时间。

这样的事情可以解决问题(改编自Javadoc for @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();
                    }
                }
        );
    }
}

答案 1 :(得分:3)

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

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注释一起使用。

答案 2 :(得分:3)

您可以使用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进行调度

答案 3 :(得分: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);

答案 4 :(得分:1)

您也可以为此使用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";
    }
}

答案 5 :(得分: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());
    }
}