定时任务任务的三种⽅法1,spring整合quartz⽅式,这种⽹上资料太多,简单引⽤⼀下就ok。
<bean id="taskJob" class="d.dw.task.DataConversionTask"/>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="group" value="job_work"/>
<property name="name" value="job_work_name"/>
<!--false表⽰等上⼀个任务执⾏完后再开启新的任务-->
<property name="concurrent" value="false"/>
<property name="targetObject">
<ref bean="taskJob"/>
</property>
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!--  调度触发器 -->
<bean id="myTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="name" value="work_default_name"/>
<property name="group" value="work_default"/>
<property name="jobDetail">
<ref bean="jobDetail"/>
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- 调度⼯⼚ -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
2.spring task⽅式的定时任务(最为简单,功能也简单)
<!-- 定时器配置开始-->
<bean id="task1" class="com.XXX.Task1"></bean>
<bean id="task2" class="com.XXX.Task2"></bean>
<task:scheduled-tasks>
<task:scheduled ref="task1" method="execute" cron="0 */1 * * * ?"/>
<task:scheduled ref="task2" method="execute" cron="0 */1 * * * ?"/>
</task:scheduled-tasks>
<!-- 定时器配置结束-->
使⽤的时候,直接写⼀个类,⼀个⽅法就可以了,简单快捷。task1类添加⽅法:
public void execute(){
// TODO: 你的业务逻辑
}
3.使⽤quartz⽅式,可以动态修改任务执⾏时间和参数的⼯具⽅法。
import java.util.List;
import java.util.Set;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.DirectSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 参考
* blog.csdn/xlxxcc/article/details/52115995
* @ClassName: QuartzManager
* @author lishang
* @Description quartz定时任务管理增删改
* @date 2017年11⽉2⽇下午1:27:47
*
*/
public class QuartzManager {
protected static final Logger LOG = Logger(QuartzManager.class);
//private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private static DirectSchedulerFactory Instance(); static{
try {
} catch (SchedulerException e) {
LOG.info("创建DirectSchedulerFactory的Scheduler失败!",e);
}
}
/**
* 判断⼀个job是否存在
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @return
*/
public static boolean isExistJob(String jobName, String jobGroupName){
boolean exist=false;
try {
Scheduler sched = Scheduler();
JobKey jobKey = new JobKey(jobName, jobGroupName);
exist = sched.checkExists(jobKey);
} catch (SchedulerException e) {
e.printStackTrace();
}
return exist;
}
/**
* @Description: 添加⼀个定时任务
*
* @param jobName 任务名
* @param jobGroupName  任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass  任务
* @param cron  时间设置,参考quartz说明⽂档
*/
@SuppressWarnings("unchecked")实例化bean的三种方式
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class jobClass,JobDataMap jMap, String cron) {  try {
Scheduler sched = Scheduler();
// 任务名,任务组,任务执⾏类
JobDetail jobDetail= wJob(jobClass).withIdentity(jobName, jobGroupName)
.usingJobData(jMap).build();
// 触发器
TriggerBuilder<Trigger> triggerBuilder = wTrigger();
// 触发器名,触发器组
triggerBuilder.withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
// 触发器时间设定
triggerBuilder.Schedule(cron));
// 创建Trigger对象
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
// 调度容器设置JobDetail和Trigger
sched.scheduleJob(jobDetail, trigger);
Trigger.TriggerState triggerState = Key());
LOG.info("job的触发器状态:"+triggerState.name());
System.out.println("job的触发器状态:"+triggerState.name());
// 启动
if (!sched.isShutdown()) {
sched.start();
}
//按新的trigger重新设置job执⾏
} catch (Exception e) {
<("添加⼀个定时任务发⽣异常:",e);
throw new RuntimeException(e);
}
}
/**
* @Description: 修改⼀个任务的触发时间
*
* @param jobName
* @param jobGroupName
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param cron  时间设置,参考quartz说明⽂档
*/
public static void modifyJobTime(String jobName,
String jobGroupName, String triggerName, String triggerGroupName,@SuppressWarnings("rawtypes") Class jobClass,JobDataMap jMap,String cron) {  /** ⽅式⼀:调⽤ rescheduleJob 开始 */
// 触发器
//TriggerBuilder<Trigger> triggerBuilder = wTrigger();
// 触发器名,触发器组
//triggerBuilder.withIdentity(triggerName, triggerGroupName);
//triggerBuilder.startNow();
// 触发器时间设定
//triggerBuilder.Schedule(cron));
// 创建Trigger对象
//trigger = (CronTrigger) triggerBuilder.build();
// ⽅式⼀:修改⼀个任务的触发时间
//scheduleJob(triggerKey, trigger);
/** ⽅式⼀:调⽤ rescheduleJob 结束 */
/** ⽅式⼆:先删除,然后在创建⼀个新的Job  */
removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, jMap, cron);
/** ⽅式⼆:先删除,然后在创建⼀个新的Job */
}
/**
* @Description: 移除⼀个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
public static void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = Scheduler();
TriggerKey triggerKey = iggerKey(triggerName, triggerGroupName);
sched.pauseTrigger(triggerKey);// 停⽌触发器
sched.unscheduleJob(triggerKey);// 移除触发器
sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务
List<String> jobGroupNames = JobGroupNames();
LOG.info("任务组开始-->groupsNames=[");
for (String string : jobGroupNames) {
GroupMatcher<JobKey> matcher=GroupMatcher.jobGroupEquals(string);
Set<JobKey> jobKeys = JobKeys(matcher);
LOG.info(string+"下的JOB为[");
for (JobKey jobKey : jobKeys) {
LOG.Name()+",");
}
LOG.info("]");
}
LOG.info("]任务组结束。");
} catch (Exception e) {
<("移除job任务发⽣异常:",e);
throw new RuntimeException(e);
}
}
/**
* @Description:启动所有定时任务
*/
public static void startJobs() {
try {
Scheduler sched = Scheduler();
sched.start();
} catch (Exception e) {
<("启动所有定时任务发⽣异常:",e);
throw new RuntimeException(e);
}
}
/**
* @Description:关闭所有定时任务
*/
public static void shutdownJobs() {
try {
Scheduler sched = Scheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
<("关闭所有定时任务发⽣异常:",e);
throw new RuntimeException(e);
}
}
}
之前项⽬中使⽤的调度⼯⼚类是:StdSchedulerFactory然后定时任务动态修改的时候总是不稳定。后来改为上⾯的⼯⼚类,问题得到了解决。
原因是其他地⽅有使⽤StdSchedulerFactory 的调度器,所有的调度任务都会在这个⼯⼚中,不容易控制,⽽DirectSchedulerFactory这个⼯⼚是每个new⽣成⼀个实例,更容易控制。
3.使⽤quartz⽅式,不整合spring

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。