Spring 之 任务调度 与 异步处理
1.引入Spring的pom依赖(略)
2.创建spring.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:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <!-- 自动扫描的包名 --> <context:component-scan base-package="com.wode" /> <!-- 开启AOP代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!--开启注解处理器 --> <context:annotation-config> </context:annotation-config> <!-- 任务调度 及 线程池异步任务 --> <!-- 开启注解 --> <task:annotation-driven executor="executor" scheduler="scheduler"/> <!-- 默认任务调度池 --> <task:scheduler id="scheduler" pool-size="10"/> <!-- 默认线程池 --> <task:executor id="executor" pool-size="10" queue-capacity="100"/> <!-- 自定义线程池 --> <!-- 线程池 --> <bean id="myExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心线程数 --> <property name="corePoolSize" value="8" /> <!-- 最大线程数 --> <property name="maxPoolSize" value="16" /> <!-- 队列最大长度 --> <property name="queueCapacity" value="1024" /> <!-- 最大空闲时间 --> <property name="keepAliveSeconds" value="3000" /> <!-- 线程池对拒绝策略:队列满了则交给本地方法执行 --> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> </beans>
3.任务调度使用
a.使用
@Component public class ScheduledManager { @Scheduled(fixedDelay = 10000) //执行后延时10秒再执行 public void delayScheduler(){ System.out.println("[delayScheduler] 开始延迟调度"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[delayScheduler] 结束延迟调度"); } @Scheduled(cron="0 0/1 * * * ?") //每分钟调度一次 [秒] [分] [小时] [日] [月] [周] [年] public void conScheduler(){ System.out.println("[conScheduler] 开始定时调度"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[conScheduler] 结束定时调度"); } }
b.测试
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); } }
4.线程池异步执行
a.使用
public abstract class BaseTask { public void beforeWork(){} public abstract void doWork(); public void afterWork(){} }
@Component public class TaskManager { @Async //默认线程池执行 public void addTask(BaseTask task){ task.beforeWork(); task.doWork(); task.afterWork(); } @Async(value = "myExecutor") //自定义线程池执行 public void addLocalTask(BaseTask task){ task.beforeWork(); task.doWork(); task.afterWork(); } }
b.测试
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); TaskManager manager = (TaskManager) context.getBean("taskManager"); for (int i = 0; i < 100; i++) { int count = i; manager.addTask(new BaseTask() { @Override public void doWork() { System.out.println("[Task] 任务开始: " + count); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[Task] 任务结束: " + count); } }); manager.addLocalTask(new BaseTask() { @Override public void doWork() { System.out.println("[LocalTask] 任务开始: " + count); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[LocalTask] 任务结束: " + count); } }); } } }