spring多线程与定时任务
本篇主要描述一下spring的多线程的使用与定时任务的使用.
1.spring多线程任务的使用
spring通过任务执行器TaskExecutor来实现多线程与并发编程。通常使用ThreadPoolTaskExecutor来实现一个基于线程池的TaskExecutor.
首先你要实现AsyncConfigurer 这个接口,目的是开启一个线程池
代码如下:
package com.foreveross.service.weixin.test.thread; import java.util.concurrent.Executor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; /**
* 注入一个线程池
* @author mingge
*
*/ @Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer { @Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
} @Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
} }
然后注入一个类,实现你的业务,并在你的Bean的方法中使用@Async注解来声明其是一个异步任务
代码如下:
package com.foreveross.service.weixin.test.thread; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; /**
* 线程池任务
* @author mingge
*
*/
@Service
public class TaskService { @Async
public void executeAsyncTask(int i){
System.out.println("执行异步任务:"+i);
} @Async
public void executeAsyncTask1(int i){
System.out.println("执行异步任务1:"+(i+i));
}
}
最后通过测试,可以看到你的实现是异步执行了.
package com.foreveross.service.weixin.test.thread; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
*
* @author mingge
*
*/
public class Test { public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
TaskService taskService=context.getBean(TaskService.class);
for(int i=0;i<20;i++){
taskService.executeAsyncTask(i);
taskService.executeAsyncTask1(i);
}
//最后可以根据结果可以看出结果是并发执行而不是顺序执行的呢
context.close();
}
}
1.spring定时任务的使用
在java原生态中,我们使用timer就可以了,这里小编说一些在Spring中的定时任务的使用
package com.foreveross.service.weixin.test.thread; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling; @Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableScheduling//开启对定时器的支持
public class TaskSchedulerConfig { }
package com.foreveross.service.weixin.test.thread; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; @Service
public class TimerTaskJob { @Scheduled(fixedRate=2000)
public void test(){
System.out.println("我是定时任务:"+new Date().getSeconds());
}
}
package com.foreveross.service.weixin.test.thread;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestTimer {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
//context.close();
}
}
记一下总结,或许以后有用...
每天进步一点点...
spring多线程与定时任务的更多相关文章
- Spring Boot (29) 定时任务
使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...
- spring boot 创建定时任务
@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...
- spring 多线程 注入 服务层 问题
在用多线程的时候,里面要用到Spring注入服务层,或者是逻辑层的时候,一般是注入不进去的.具体原因应该是线程启动时没有用到Spring实例不池.所以注入的变量值都为null. 详细:http://h ...
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- Spring Boot配置定时任务
在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...
- Spring+Quartz 实现定时任务的配置方法
Spring+Quartz 实现定时任务的配置方法 整体介绍 一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是 ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
- spring多线程初探
6月14号 晴 最高温度37 今天很热的一天啊,开发的任务现在正在测试阶段,手头没有什么工作任务,忙里偷闲,丰富一下我的blog. 前两天有个需求:调用第三方接口,这个接口的响应时间有点长,需 ...
- spring多个定时任务quartz配置
spring多个定时任务quartz配置 <?xml version=”1.0″ encoding=”UTF-8″?> <beans xmlns=”http://www.spring ...
随机推荐
- mysql安装及卸载
一.关于mysql MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...
- LaTex Remove Left Margin 去除左边空间
LaTex中使用itemize的时候,默认的每一项左边都有一小段距离,并不是紧贴着边缘的,那么如果我们想去掉这段距离,我们可以使用下面的命令: \usepackage{enumitem} \setli ...
- css中margin-left与left的区别
研究下拉菜单和弹出菜单时比较所得: 1.直接在css中设置left生效的前提是必须设置父容器position:absolute或relative,如果不设置则会显示为最近一个定位的父对象左边相关的位置 ...
- oracle中复制表和数据 && 多表插入语句
创建测试表和测试数据 create table test (id number,name varchar(10)); insert into test values(1,'liufang'); ...
- 屏蔽Enter触发的事件
无论是 <button type="button" onclick="console.log('123');">123</button> ...
- vim的常用命令
平常最多是用vim来编辑单个文件,看看源码.就是写几k行代码时也没有用一些其他的插件,只是设置了高亮等一些自带的属性.这样的好处是,换到任何一台新机上都能立马使用. 网上流传了大量的“vim命令合集” ...
- Apache Spark技术实战之3 -- Spark Cassandra Connector的安装和使用
欢迎转载,转载请注明出处,徽沪一郎. 概要 前提 假设当前已经安装好如下软件 jdk sbt git scala 安装cassandra 以archlinux为例,使用如下指令来安装cassandra ...
- jdk安装配置具体分析
JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹中.(不能都安装在java文件夹的根目录下,j ...
- mvc 扩展htmlhelper
using System.Web.Mvc; namespace System.Web.Mvc{ public static class HtmlExtend { public ...
- phpcms v9的url优化
nginx配置重定向 # nginx rewrite rule rewrite ^/show-(.+)-(.+)-(.+).html$ /index.php?m=content&c=index ...