这篇文章将为大家详细讲解有关Spring内置任务调度实现添加、取消与重置的示例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
实现方法如下
首先,我们需要启用Spring的任务调度
<?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:context="http://www.springframework.org/schema/context" 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/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <task:annotation-driven executor="jobExecutor" scheduler="jobScheduler" /> <task:executor id="jobExecutor" pool-size="5"/> <task:scheduler id="jobScheduler" pool-size="10" /></beans>
这一部分配置在网上是很常见的,接下来我们需要联合使用@EnableScheduling与org.springframework.scheduling.annotation.SchedulingConfigurer
便携我们自己的调度配置,在SchedulingConfigurer接口中,需要实现一个void configureTasks(ScheduledTaskRegistrar taskRegistrar);
方法,传统做法是在该方法中添加需要执行的调度信息。网上的基本撒谎那个也都是使用该方法实现的,使用addTriggerTask添加任务,并结合cron表达式动态修改调度时间,这里我们并不这样做。
查看一下ScheduledTaskRegistrar源码,我们发现该对象初始化完成后会执行scheduleTasks()
方法,在该方法中添加任务调度信息,最终所有的任务信息都存放在名为scheduledFutures的集合中。
protected void scheduleTasks() { long now = System.currentTimeMillis(); if (this.taskScheduler == null) { this.localExecutor = Executors.newSingleThreadScheduledExecutor(); this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor); } if (this.triggerTasks != null) { for (TriggerTask task : this.triggerTasks) { this.scheduledFutures.add(this.taskScheduler.schedule( task.getRunnable(), task.getTrigger())); } } if (this.cronTasks != null) { for (CronTask task : this.cronTasks) { this.scheduledFutures.add(this.taskScheduler.schedule( task.getRunnable(), task.getTrigger())); } } if (this.fixedRateTasks != null) { for (IntervalTask task : this.fixedRateTasks) { if (task.getInitialDelay() > 0) { Date startTime = new Date(now + task.getInitialDelay()); this.scheduledFutures.add(this.taskScheduler.scheduleAtFixedRate( task.getRunnable(), startTime, task.getInterval())); } else { this.scheduledFutures.add(this.taskScheduler.scheduleAtFixedRate( task.getRunnable(), task.getInterval())); } } } if (this.fixedDelayTasks != null) { for (IntervalTask task : this.fixedDelayTasks) { if (task.getInitialDelay() > 0) { Date startTime = new Date(now + task.getInitialDelay()); this.scheduledFutures.add(this.taskScheduler.scheduleWithFixedDelay( task.getRunnable(), startTime, task.getInterval())); } else { this.scheduledFutures.add(this.taskScheduler.scheduleWithFixedDelay( task.getRunnable(), task.getInterval())); } } } }
所以我的思路就是动态修改该集合,实现任务调度的添加、取消、重置。实现代码如下:
package com.jianggujin.web.util.job;import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ScheduledFuture;import org.springframework.scheduling.TaskScheduler;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import org.springframework.scheduling.config.TriggerTask;import com.jianggujin.web.util.BeanUtils;@EnableSchedulingpublic class DefaultSchedulingConfigurer implements SchedulingConfigurer{ private final String FIELD_SCHEDULED_FUTURES = "scheduledFutures"; private ScheduledTaskRegistrar taskRegistrar; private Set<ScheduledFuture<?>> scheduledFutures = null; private Map<String, ScheduledFuture<?>> taskFutures = new ConcurrentHashMap<String, ScheduledFuture<?>>(); @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { this.taskRegistrar = taskRegistrar; } @SuppressWarnings("unchecked") private Set<ScheduledFuture<?>> getScheduledFutures() { if (scheduledFutures == null) { try { scheduledFutures = (Set<ScheduledFuture<?>>) BeanUtils.getProperty(taskRegistrar, FIELD_SCHEDULED_FUTURES); } catch (NoSuchFieldException e) { throw new SchedulingException("not found scheduledFutures field."); } } return scheduledFutures; } public void addTriggerTask(String taskId, TriggerTask triggerTask) { if (taskFutures.containsKey(taskId)) { throw new SchedulingException("the taskId[" + taskId + "] was added."); } TaskScheduler scheduler = taskRegistrar.getScheduler(); ScheduledFuture<?> future = scheduler.schedule(triggerTask.getRunnable(), triggerTask.getTrigger()); getScheduledFutures().add(future); taskFutures.put(taskId, future); } public void cancelTriggerTask(String taskId) { ScheduledFuture<?> future = taskFutures.get(taskId); if (future != null) { future.cancel(true); } taskFutures.remove(taskId); getScheduledFutures().remove(future); } public void resetTriggerTask(String taskId, TriggerTask triggerTask) { cancelTriggerTask(taskId); addTriggerTask(taskId, triggerTask); } public Set<String> taskIds() { return taskFutures.keySet(); } public boolean hasTask(String taskId) { return this.taskFutures.containsKey(taskId); } public boolean inited() { return this.taskRegistrar != null && this.taskRegistrar.getScheduler() != null; }}
其中用到的BeanUtils源码如下:
package com.jianggujin.web.util;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;public class BeanUtils{ public static Field findField(Class<?> clazz, String name) { try { return clazz.getField(name); } catch (NoSuchFieldException ex) { return findDeclaredField(clazz, name); } } public static Field findDeclaredField(Class<?> clazz, String name) { try { return clazz.getDeclaredField(name); } catch (NoSuchFieldException ex) { if (clazz.getSuperclass() != null) { return findDeclaredField(clazz.getSuperclass(), name); } return null; } } public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { try { return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { return findDeclaredMethod(clazz, methodName, paramTypes); } } public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { try { return clazz.getDeclaredMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { if (clazz.getSuperclass() != null) { return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes); } return null; } } public static Object getProperty(Object obj, String name) throws NoSuchFieldException { Object value = null; Field field = findField(obj.getClass(), name); if (field == null) { throw new NoSuchFieldException("no such field [" + name + "]"); } boolean accessible = field.isAccessible(); field.setAccessible(true); try { value = field.get(obj); } catch (Exception e) { throw new RuntimeException(e); } field.setAccessible(accessible); return value; } public static void setProperty(Object obj, String name, Object value) throws NoSuchFieldException { Field field = findField(obj.getClass(), name); if (field == null) { throw new NoSuchFieldException("no such field [" + name + "]"); } boolean accessible = field.isAccessible(); field.setAccessible(true); try { field.set(obj, value); } catch (Exception e) { throw new RuntimeException(e); } field.setAccessible(accessible); } public static Map<String, Object> obj2Map(Object obj, Map<String, Object> map) { if (map == null) { map = new HashMap<String, Object>(); } if (obj != null) { try { Class<?> clazz = obj.getClass(); do { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isStatic(mod)) { continue; } boolean accessible = field.isAccessible(); field.setAccessible(true); map.put(field.getName(), field.get(obj)); field.setAccessible(accessible); } clazz = clazz.getSuperclass(); } while (clazz != null); } catch (Exception e) { throw new RuntimeException(e); } } return map; } public static List<Class<?>> getSuperclassList(Class<?> clazz) { List<Class<?>> clazzes = new ArrayList<Class<?>>(3); clazzes.add(clazz); clazz = clazz.getSuperclass(); while (clazz != null) { clazzes.add(clazz); clazz = clazz.getSuperclass(); } return Collections.unmodifiableList(clazzes); }}
因为加载的延迟,在使用这种方法自定义配置任务调度是,首先需要调用inited()
方法判断是否初始化完成,否则可能出现错误。
接下来我们来测试一下:
package com.jianggujin.zft.job;import java.util.Calendar;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.config.TriggerTask;import org.springframework.scheduling.support.CronTrigger;import org.springframework.stereotype.Component;import com.jianggujin.web.util.job.DefaultSchedulingConfigurer;public class TestJob implements InitializingBean{ @Autowired private DefaultSchedulingConfigurer defaultSchedulingConfigurer; public void afterPropertiesSet() throws Exception { new Thread() { public void run() { try { // 等待任务调度初始化完成 while (!defaultSchedulingConfigurer.inited()) { Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("任务调度初始化完成,添加任务"); defaultSchedulingConfigurer.addTriggerTask("task", new TriggerTask(new Runnable() { @Override public void run() { System.out.println("run job..." + Calendar.getInstance().get(Calendar.SECOND)); } }, new CronTrigger("0/5 * * * * ? "))); }; }.start(); new Thread() { public void run() { try { Thread.sleep(30000); } catch (Exception e) { } System.out.println("重置任务............"); defaultSchedulingConfigurer.resetTriggerTask("task", new TriggerTask(new Runnable() { @Override public void run() { System.out.println("run job..." + Calendar.getInstance().get(Calendar.SECOND)); } }, new CronTrigger("0/10 * * * * ? "))); }; }.start(); }}
在该类中,我们首先使用一个线程,等待我们自己的任务调度初始化完成后向其中添加一个每五秒钟打印一句话的任务,然后再用另一个线程过30秒后修改该任务,修改的本质其实是现将原来的任务取消,然后再添加一个新的任务。
在配置文件中初始化上面的类
<bean id="defaultSchedulingConfigurer" class="com.jianggujin.web.util.job.DefaultSchedulingConfigurer"/><bean id="testJob" class="com.jianggujin.zft.job.TestJob"/>
运行程序,观察控制台输出
这样我们就实现了动态的重置任务了。
关于“Spring内置任务调度实现添加、取消与重置的示例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。