1、Spring异步任务
开启异步配置(@EnableAsync注解指定或<task:annotation-driven>标签配置)
1、用Java注解:
第一步:在类的申明前使用@EnableAsync注解开启异步调用功能。
第二步:public AsyncTaskExecutor taskExecutor() 方法自定义自己的线程池,线程池前缀”Anno-Executor”。如果不定义,则使用系统默认的线程池。
@EnableAsync // 启动异步调用 public class AsyncApplicationWithAnnotation { /* 略 */ /** * 自定义异步线程池 * @return */ @Bean public AsyncTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setThreadNamePrefix("Anno-Executor"); executor.setMaxPoolSize(10); // 设置拒绝策略 executor.setRejectedExecutionHandler(new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { // ..... } }); return executor; } }
2、用XML配置:
第一步:使用<task:executor>标签定义线程池
第二步:使用<task:annotation-driven>标签启动异步线程池配置
如下spring-async.xml配置:
<!-- 等价于 @EnableAsync, executor指定线程池 --> <task:annotation-driven executor="xmlExecutor"/> <!-- id指定线程池产生线程名称的前缀 --> <task:executor id="xmlExecutor" pool-size="5-25" queue-capacity="100" keep-alive="120" rejection-policy="CALLER_RUNS"/>
有关线程池的拒绝策略,参考下文:
线程池--拒绝策略RejectedExecutionHandler
编写异步任务(@Async注解指定)
1、编写普通Java类,并设置方法为@Async注解
只需要简单这么一步即可在执行注解方法时自动由Spring框架执行异步操作。
p
ackage tech.bbwang.springtask.scheduled.demo; import org.springframework.stereotype.Component; import java.util.Date; @Component public class ScheduledTaskExample { public ScheduledTaskExample(){ } @Async public void printTime(){ System.out.println((new Date()).toString()); } }
2、线程池
配置线程池
最简单的形式,只需要在spring配置文件里加一条配置即可。
<task:executor id="xmlExecutor" pool-size="2" />
添加此配置需要在Spring配置文件的<beans>头部添加下面的约束文件(task相关处的命名空间)
<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" 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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
使用线程池
线程池xmlExecutor作为一个Spring的bean存在,可以被注入到需要的地方。
比如:
定义一个使用线程池的类TaskExecutorExample,该类使用线程池跑10个打印当前时间的任务。
package tech.bbwang.springtask.demo; import org.springframework.core.task.TaskExecutor; import org.springframework.stereotype.Component; import tech.bbwang.App; import java.util.HashMap; import java.util.Map; /** * 任务执行样例 */ @Component public class TaskExecutorExample { private TaskExecutor taskExecutor; public TaskExecutorExample(TaskExecutor taskExecutor) { this.taskExecutor = taskExecutor; } public void printMessages() { for(int i = 0; i <10; i++) { taskExecutor.execute(new Runnable(){ public void run(){ System.out.println((new Date()).toString()); } }); } } }
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="tech.bbwang.springtask.demo"/> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="25" /> </bean> <bean id="taskExecutorExample" class="tech.bbwang.springtask.demo.TaskExecutorExample"> <constructor-arg ref="taskExecutor" /> </bean> </beans>
3、定时器和定时任务
定义定时器
spring配置文件:
<!-- pool-size:此参数默认为1,此时,不同的定时任务不可同时进行,因为线程数不够。需要自己设置>1的数值来保证,同一个定时任务前一个未完成情况下不启动下一个,但不用的定时任务可以同时进行--> <task:scheduler id="taskScheduler" pool-size="10"/>
定义定时任务
spring配置文件:
<task:scheduled-tasks> <!-- 配置定时任务,cron=一秒一次--> <task:scheduled ref="scheduledTaskExample" method="printTime" cron="0/5 * * * * ?"/> <!-- 配置定时任务,cron=每日0时和12时各执行一次任务--> <!--Cron表达式生成器 http://www.pdtools.net/tools/becron.jsp--> <!--<task:scheduled ref="scheduledTaskExample" method="printTime" cron="0/1 0/1 0,12 * * ?"/>--> </task:scheduled-tasks>
有关corn表达式可以参考:
定义定时任务类
package tech.bbwang.springtask.scheduled.demo; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.util.Date; @Component public class ScheduledTaskExample { public ScheduledTaskExample(){ } @Async public void printTime(){ System.out.println((new Date()).toString()); } }
定义运行类
其实已经完全由Spring配置解决了运行问题,只需要给一个运行Spring配置的主函数即可。
package tech.bbwang; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.concurrent.CountDownLatch; public class SpringAsyncTask { public static void main( String[] args ) { new ClassPathXmlApplicationContext("spring-async*.xml"); } }
相关推荐
Spring3.2.6定时任务+线程池,实现系统后台任务多线程处理
spring2.5 定时器任务 spring2.5 定时器任务 spring2.5 定时器任务 spring2.5 定时器任务
Spring Boot 自定义异步线程池的两种方式 Demo 下载,教程:https://www.ydyno.com/archives/1199.html
spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器spring定时器
spring定时器spring定时器spring定时器spring定时器spring定时器
l Spring封装Job调度:当任务信息都持久化在DB中之后,我们需要将这些信息读取出来执行具体的业务逻辑操作,这里我们...为了让系统更加轻量级,这里我们直接采用Spring中基于JDK线程池的默认封装实现,通过配置调整参数
Spring中的同步执行器 1. SyncTaskExecutor:同步可以用SyncTaskExecutor,但这个可以说不算一个线程池,因为还在原线程执行。这个类没有实现异步调用,只是一个同步操作。 2.也可以用ThreadPoolTaskExecutor结合...
主要介绍了Spring Boot整合FTPClient线程池的实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
下面小编就为大家带来一篇基于Spring中的线程池和定时任务功能解析。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring中的Quartz配置-Spring-定时器-java定时器.doc
1. SpringBoot 自定义线程池以及多线程间的异步调用(@Async、@EnableAsync) 2.Java多线程之定时任务 以及 SpringBoot多线程实现定时任务 3.@EnableScheduling 与 @Scheduled
本篇文章主要介绍了浅谈Spring @Async异步线程池用法总结,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
springAop与spring定时器
NULL 博文链接:https://bijian1013.iteye.com/blog/2368708
Spring定时任务的几种实现,欢迎交流!
主要介绍了Spring Boot使用Spring的异步线程池的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Spring3.x企业应用开发实战 中 Spring 定时任务源码(spring 三种定时任务的实现方式),Spring quartz java 定时器 定时任务 源码
使用spring 实现的两种定时任务,使用eclipse直接导入就可使用
2.通过Spring是JAR-quartz写的一个定时任务 1)普通的定时任务,定时完成指定的任务 2)通过前台动态分配定时任务 可指定多个任务,可同时执行任务,可以精确到时分秒扫描并执行任务 3)可以完成稍微复杂点的任务 ...
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...