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 ...
随机推荐
- Linux_vi编辑器
一.vi/vim编辑器的三种模式 1. 命令模式 2. 插入模式 3. 最后行模式 eRrsr@hadoop09-linux tmp]$ vi vi.txt # 命令行 start to write ...
- mysql主从切换摘要
1.需要提升为主的从库,停止io线程等待slave数据全部更新完毕 stop slave IO_THREAD #show processlist的输出,直到看到状态是Slave has read al ...
- Logistic Regression Vs Decision Trees Vs SVM: Part I
Classification is one of the major problems that we solve while working on standard business problem ...
- c#面向对象基础 重写、虚方法、抽象类
虚方法 抽象类与抽象方法 1.书写规范: 在类前面加上abstract关键字,就成为了抽象类:在一个方法前面加上abstract关键字,就成为了抽象方法(抽象方法不能有实现方法,直接在后面加分号) 例 ...
- Jquery各个版本的区别
一: 一般原则是越新越好,jQuery版本是在不断进步和发展的,最新版是当时最高技术水平,也是最先进的技术理念. 但个人的角度来看.是最新版本x.x.0的上一版本最好.比如说1.10.0版,上一版本是 ...
- ZedBoard 引脚约束参考
从ISE转换到Vivado时,UCF转XDC的几种方法: (1)软件自动转换 参考网址:Youtube 用ISE->EDK->PlanAhead打开所需转换的工程文件*.xise,并打开b ...
- CMS .NET 程序框架 从2.0/3.5升级到4.0 版本后 需要调整的地方
问题一: document.forms1.action 不可使用 需要修改程 document.forms[0] .NET 程序框架 从2.0/3.5升级到4.0 版本后,document.forms ...
- 大量查询SQL语句 实例
1.查看表结构语句:DESC 表名 2.查询所有列:select * from 表名 3.查询指定列:select 字段名 form 表名 4.查询指定行:SELECT * ...
- IOS第11天(1:UIPickerView点餐)
UIPickerView #import "ViewController.h" @interface ViewController ()<UIPickerViewDataSo ...
- 【iCore3双核心板】【4.3寸液晶驱动板爆照!】
[源代码完全开源,过几天连同硬件一起发布] 花了好久的时间,我们的fpga工程师才完成这液晶模块的驱动代码,其核心价值如下: 1.完全基于fpga驱动,sdram当做缓存: 2.内建双缓冲机制:方便 ...