Spring异步调用注解@Async的使用
1.pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
2.编写异步方法
package com.yun.base.custom.event; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; @Component
public class AsyncThread { private static final Logger LOGGER = LoggerFactory.getLogger(AsyncThread.class); @Async
public void runMethodAsync() {
Thread t = Thread.currentThread();
try {
LOGGER.debug("异步方法1执行中");
t.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Async
public void runMethodAsync2() {
Thread t = Thread.currentThread();
try {
LOGGER.debug("异步方法2执行中");
t.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Async
public void runMethodAsync3() {
Thread t = Thread.currentThread();
try {
LOGGER.debug("异步方法3执行中");
t.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.配置线程池及开启注解
<?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:aop="http://www.springframework.org/schema/aop" 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/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true"> <context:component-scan base-package="com.yun.base.custom.event" /> <task:executor id="eventExecutor" pool-size="2" />
<task:annotation-driven executor="eventExecutor"/> </beans>
3.测试
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
package test.war; import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.yun.base.custom.event.AsyncThread; @RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:conf/async.xml" } )
public class JunitTest { private static final Logger LOGGER = LoggerFactory.getLogger(JunitTest.class); @Autowired
private ApplicationContext applicationContext; @Test
public void asyncTest() {
AsyncThread asyncThread = (AsyncThread) applicationContext.getBean("asyncThread"); LOGGER.debug("开始调用");
asyncThread.runMethodAsync();
asyncThread.runMethodAsync2();
asyncThread.runMethodAsync3();
LOGGER.debug("结束调用"); try {
Thread.currentThread().sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
} } }
4.结果分析
2017-09-11 15:05:34.323 [main] DEBUG test.war.JunitTest - 开始调用
2017-09-11 15:05:34.338 [main] DEBUG test.war.JunitTest - 结束调用
2017-09-11 15:05:34.370 [eventExecutor-1] DEBUG com.yun.base.custom.event.AsyncThread - 异步方法1执行中
2017-09-11 15:05:34.370 [eventExecutor-2] DEBUG com.yun.base.custom.event.AsyncThread - 异步方法2执行中
2017-09-11 15:05:44.378 [eventExecutor-2] DEBUG com.yun.base.custom.event.AsyncThread - 异步方法3执行中
可以看到在主线程 main 下执行完成之后,另开启了2个线程执行异步方法1,2,然后10秒之后线程2继续执行异步方法3。
这里我设置的线程数为2,所以第一论执行的时候,同时只能有2个线程来执行异步方法,异步方法3等待,当有线程释放的时候继续执行异步方法3。
Spring异步调用注解@Async的使用的更多相关文章
- 如何在项目中使用Spring异步调用注解@Async
本文主要介绍如何使用Spring框架提供的异步调用注解@Async,异步线程池配置.异常捕获处理. 开启@Async注解支持 使用@Async注解的之前,必须在项目中启动时调用@EnableAsync ...
- Spring异步调用原理及SpringAop拦截器链原理
一.Spring异步调用底层原理 开启异步调用只需一个注解@EnableAsync @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTI ...
- Spring Boot中实现异步调用之@Async
一.什么是异步调用 “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用 的语句返回结果 ...
- 161107、spring异步调用,完美解决!
项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步的方式 ...
- 161021、spring异步调用,完美解决!
前言 项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步 ...
- 如何在Spring异步调用中传递上下文
以下文章来源于aoho求索 ,作者aoho 1. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步 ...
- Spring异步任务处理,@Async的配置和使用
本文转自http://blog.csdn.net/clementad/article/details/47403185 感谢作者 这个注解用于标注某个方法或某个类里面的所有方法都是需要异步处理的.被注 ...
- spring boot 学习(十一)使用@Async实现异步调用
使用@Async实现异步调用 什么是”异步调用”与”同步调用” “同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行:”异步调用”则是只要上一行代码执行,无需等 ...
- SpringBoot | 第二十一章:异步开发之异步调用
前言 上一章节,我们知道了如何进行异步请求的处理.除了异步请求,一般上我们用的比较多的应该是异步调用.通常在开发过程中,会遇到一个方法是和实际业务无关的,没有紧密性的.比如记录日志信息等业务.这个时候 ...
随机推荐
- 一个项目中既有移动端,同时也有PC端的代码,并且 他们的代码分开写的,那么如何实现在手机跳转手机页面,pc点击跳转pc页面
将以下代码放入pc首页即可 <script type="text/javascript"> function mobile_device_detect(url) { v ...
- FileZilla 客户端连接 FlieZilla 服务器 连接成功读取目录列表却失败的解决办法
解决过程: 第一步: 第二步:
- 第二阶段——个人工作总结DAY06
1.昨天做了什么:昨天做完了修改密码的界面.(有点丑) 2.今天打算做什么:今天制作时间轴. 3.遇到的困难:无.
- Django之转发和重定向
https://blog.csdn.net/gscsd_t/article/details/79389167 转发和重定向: 转发:一次请求和响应,请求的地址没有发生变化,如果此时刷新页面,就会出现重 ...
- WDA基础九:BusinessGraphics
好像很少有人用这玩意...好难玩,好废...和ABAP的那个图一样废.... 很多报表都是用BO,BI什么做的,不仅废,而且很多BO顾问不懂代码,写出来的报表挫的要死.... WDA的网页图形报表分析 ...
- Day2----Jmeter 压测
一.jmeter 压测1.一般压测时间为10-15分钟就行,设置时间在调度器配置--持续时间中设置,例如:想压10分钟,则持续时间输入:600 1.线程数:发送请求的用户数,即并发数 2.Ram-up ...
- 详谈Oracle12c新特点容器数据库&可插拔数据库(CDB&PDB)
一般信息 数据字典 CDB_FILE$ DBA_PDBS PDB$SEED CDB_LOCAL_ADMINAUTH$ DBA_PDB_HISTORY PDB_HISTORY$ CDB_PDB_SAVE ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- Qt Widgets——主窗口及其主要组成部分
Main Window and Related Classes QAction 动作类,用于当做一个菜单项或工具项插入菜单或工具栏 QActionGroup 动作组,用于管理多个动作,设置它们之间的互 ...
- 窗体应用程序防腾讯QQ源码
窗体应用程序防腾讯QQ源码 using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...