前景提要:spring自带的scheduling定时调度任务相当于轻量级的Quartz,但是不支持分布式,若要实现分布式定时任务就得使用Quartz。
1、项目整体结构:
2、定时任务核心代码:
程序入口:
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:dubbo-consumer.xml"})
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Application.class, args);
}
}
容器Bean组件类:
package com.zzg.application;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
if(SpringBeanUtil.applicationContext == null) {
SpringBeanUtil.applicationContext = applicationContext;
}
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
定时任务抽象类:
package com.zzg.cron.abstr;
/**
*
* @ClassName: CronAbstract
* @Description: 定时任务抽象类
* @author: 世纪伟图 -zzg
* @date: 2019年4月9日 上午9:39:47
*
* @Copyright: 2019 www.digipower.cn
* 注意:本内容仅限于深圳市世纪伟图科技开发有限公司内部使用,禁止用于其他的商业目的
*/
public abstract class CronAbstract implements Runnable {
public abstract void execute();
@Override
public void run() {
// TODO Auto-generated method stub
this.execute();
}
}
定时任务实列化组件:
package com.zzg.cron.component;
import org.springframework.stereotype.Component;
import com.zzg.cron.abstr.CronAbstract;
@Component("cronObject")
public class CronObject extends CronAbstract {
@Override
public void execute() {
// TODO Auto-generated method stub
System.out.println("CronObject 对象执行输出任务");
}
}
定时任务配置类:
package com.zzg.cron.config;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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 com.zzg.api.CronService;
import com.zzg.application.SpringBeanUtil;
import com.zzg.entity.Cron;
@Configuration
@EnableScheduling
public class CronScheduleConfig implements SchedulingConfigurer{
@Autowired
private CronService service;
@Autowired
private SpringBeanUtil container;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// TODO Auto-generated method stub
List<Cron> list = service.getCrons();
if(list != null && list.size() > 0){
list.stream().forEach(item ->{
// 获取task 对象
String cronName = item.getCron_name();
Runnable task = (Runnable) container.getBean(cronName);
Trigger trigger = new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// TODO Auto-generated method stub
CronTrigger trigger = new CronTrigger(item.getCron_time());
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
};
// 添加cron 任务
taskRegistrar.addTriggerTask(task, trigger);
});
}
}
}
dubbo 服务配置文件,在这里不做讲解。
sql 语句:
DROP TABLE IF EXISTS `cron`;
CREATE TABLE `cron` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cron_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`cron_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of cron
-- ----------------------------
INSERT INTO `cron` VALUES (1, 'cronObject', '0 0/2 * * * ?');