先放一个项目结构图:
1. 首先是定时任务的配置,使用spring的task标签:
spring-schedule.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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!--FEED类目同步定时任务--> <bean id="feedCategorySchedule" class="com.aa.bb.cc.task.schedule.FeedCategorySchedule"> <property name="clusterRunningCheck" value="true" /> <property name="clusterLockKey" value="_KEY_FEEDCATEGORYSCHEDULE"/> <property name="expiredTime" value="1800" /> </bean> <task:scheduler id="schedulerCategory" /> <task:scheduled-tasks scheduler="schedulerCategory"> <task:scheduled ref="feedCategorySchedule" method="run" cron="0 */5 * * * ?" /> </task:scheduled-tasks> </beans>
method="run":设置定时任务的启动方法run(方法名可以自定义)
cron="0 */5 * * * ?":每5分钟执行一次
注:几个property标签根据业务场景设置,也可以不设置。设置的property在实现类里需要定义,并且设置get、set方法。
2. 不同环境下定时任务开关设置
SystemConfig.java
package ccccccccccccccccc /** * @author aaaaaa **/ public class SystemConfig { private Boolean feedCategorySchedule = true; public Boolean getFeedCategorySchedule() { return feedCategorySchedule; } public void setFeedCategorySchedule(Boolean feedCategorySchedule) { this.feedCategorySchedule = feedCategorySchedule; } @Override public String toString() { return "SystemConfig{" + "feedCategorySchedule=" + feedCategorySchedule + '}'; } }
spring-service.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="systemConfig" class="com.aa.bb.cc.SystemConfig" > <property name="feedCategorySchedule" value="${feed.category.schedule}" /> </bean> </beans>
system-properties.xml
feed.category.schedule=true
3. 定时任务启动类
AbstractSchedule.java
package aaaaaaaaaaaa; import cccccccccccc.SystemConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import java.util.Date; /** * @author huangxiufang **/ public abstract class AbstractSchedule { private static final Logger logger = LoggerFactory.getLogger(AbstractSchedule.class); private volatile boolean running = false; /** * 如果clusterRunningCheck == true必须设置clusterLockKey和timeout */ private boolean clusterRunningCheck = false; private String clusterLockKey; private Integer expiredTime; @Autowired protected SystemConfig systemConfig; public void run(){ if (!getScheduleEnable()){ logger.info("task[{}] is stopped ...", this.getClass().getCanonicalName()); return ; } logger.info("the schedule is running.."); if (running) { logger.info("the task has not completed..."); } else { running = true; try{ if (clusterRunningCheck){ if (null == clusterLockKey){ throw new RuntimeException("clusterLockKey has been set"); } if (null == expiredTime){ throw new RuntimeException("clusterLockKey has been set"); } } execute(new Date()); } finally { running = false; } } } protected abstract boolean getScheduleEnable(); /** * 执行调度任务 * @param date */ protected abstract void execute(Date date); private boolean checkClusterRunning() { return false; } public void setClusterRunningCheck(boolean clusterRunningCheck) { this.clusterRunningCheck = clusterRunningCheck; } public void setClusterLockKey(String clusterLockKey) { this.clusterLockKey = clusterLockKey; } public void setExpiredTime(Integer expiredTime) { this.expiredTime = expiredTime; } public void setSystemConfig(SystemConfig systemConfig) { this.systemConfig = systemConfig; } }
4. 业务实现类
FeedCategorySchedule.java
package aaaaaaaaaaaaaaaa import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; /** * @author huangxiufang * @data 2018-04-05 */ public class FeedCategorySchedule extends AbstractSchedule{ private static final Logger logger = LoggerFactory.getLogger(FeedCategorySchedule.class); @Override protected boolean getScheduleEnable() { return systemConfig.getFeedCategorySchedule(); } @Override protected void execute(Date date) { try {
//TODO 业务实现 System.out.println("12345"); } catch (Exception e){ logger.error(e.getMessage()); } } }