采用注解的方式来进行动态修改时间。
首先编写
Spring-timer.xml
文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:annotation-config/>
<!-- 自动调度需要扫描的包 -->
<context:component-scan base-package="cn.gpdi.util"></context:component-scan>
<!-- 定时器开关 -->
<!-- 可以指定执行线程池的初始大小、最大大小 -->
<task:executor id="executor" pool-size="5"/>
<!-- 调度线程池的大小,调度线程在被调度任务完成前不会空闲 -->
<task:scheduler id="scheduler" pool-size="10"/>
<!-- 使用注解驱动 -->
<task:annotation-driven executor="executor" scheduler="scheduler"/>
</beans>
然后在Spring的配置文件
Spring-dao.xml
中引入定时器的配置文件
<!-- 引入Spring定时器配置文件 -->
<import resource="classpath:spring/spring-timer.xml"/>
编写定时类
package cn.gpdi.util;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Rowe.青衫(qq:1635936133)
* @date 2018-11-14.
*/
@Component
public class ScheduledTest {
private static Logger logger = Logger.getLogger(ScheduledTest.class);
/**
* 每5秒执行一次
* cron:表达式控制
*/
@Scheduled(cron = "0/5 * * * * ? ")
public void print() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
logger.debug(" == 定时任务执行 == " + sdf.format(new Date()));
}
}
这样简单的spring定时器就完成了。因为我第一次尝试的时候报了个debug的错在我解决了之后想重现发现重现不了了(找不到
scheduledExecutorService
这个bean)。我把这个bean写在了动态定时器里面了,有详细的注解。
实现动态的定时器。
package cn.gpdi.util;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 使用Spring注解定时器任务
*
* @author Rowe.青衫(qq:1635936133)
* @date 2018-11-12.
*/
@Lazy(false)
@Component
@EnableScheduling
@Configuration
public class ScheduledConfig implements SchedulingConfigurer {
private static Logger logger = Logger.getLogger(ScheduledConfig.class);
/**
* 动态定时器默认时间为5秒钟
*/
private static String cron = "0/5 * * * * ?";
public static void setCron(String cron) {
ScheduledConfig.cron = cron;
}
/**
* Spring的定时任务调度器会尝试获取一个注册过的task scheduler来做任务调度,
* 它会尝试通过BeanFactory.getBean的方法来获取一个注册过的scheduler bean,
* 获取的步骤如下:
* 1.尝试从配置中找到一个TaskScheduler Bean
* 2.寻找ScheduledExecutorService Bean
* 3.使用默认的scheduler
* 前两步,如果找不到的话,就会以debug的方式抛出异常
* 解决办法:
* 1.log4j配置改info(当做没看到)
* 2.在容器中添加这个Bean(这里使用代码来进行配置)
*
* @return TaskScheduler
*/
@Bean
public TaskScheduler scheduledExecutorService() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(8);
scheduler.setThreadNamePrefix("scheduled-thread-");
return scheduler;
}
/**
* 启动时执行一次,之后每天执行一次
* cron:表达式控制
*/
@Scheduled(cron = "0 0 0 * * ?")
public void print() {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
logger.info(" == 任务每天执行一次 == " + sdf.format(new Date()));
}
/**
* 启动时执行一次,之后每隔1分钟执行一次
*/
@Scheduled(fixedRate = 1000 * 60 * 1)
public void heartbeat() {
System.out.println("执行... " + new Date());
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
/**
* Thread current = Thread.currentThread();
* logger.info("ScheduledTest.executeFileDownLoadTask 定时任务:" + current.getId() + ",name:" + current.getName());
*/
@Override
public void run() {
//逻辑任务
logger.info("cron : " + cron);
}
}, new Trigger() {
/**
*
* @param triggerContext
* @return
*/
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 任务触发,可修改任务的执行周期
CronTrigger trigger = new CronTrigger(cron);
Date nextExecutor = trigger.nextExecutionTime(triggerContext);
return nextExecutor;
}
});
}
}
最后一个动态的定时器就完成了。当需要改变定时器的时间的时候只需要简单的一步把参数set进去就可以了。
ScheduledConfig.setCron(setCron);