您的位置:首页 > 编程语言 > Java开发

Spring异步任务处理,@Async的配置和使用

2017-10-17 16:28 453 查看

目录

目录
实现原理

开启Async注解

实现For example
同步调用

使用Async异步调用

有返回值的异步调用

下面通过AsyncResult来获得异步调用的返回值

在java编程中方法调用有三种方式

同步调用

异步调用

回调

在使用spring框架的时候我们通常会使用异步调用来提高程序执行的效率,这时就会使用到@Async注解,


实现原理

Spring为任务调度与异步方法执行提供了注解支持。通过在方法上设置@Async注解,可使得方法被异步调用。调用者会在调用时立即返回,而被调用方法的实际执行是交给Spring的TaskExecutor来完成的。

所以被注解的方法被调用的时候,会在新的线程中执行,而调用它的方法会在原线程中执行,这样可以避免阻塞,以及保证任务的实时性。适用于处理log、发送邮件、短信等。

注解应用范围:

类:表示这个类汇总的所有方法都是异步的

方法:表示这个方法是异步的,如果类也注释了,则以这个方法为基准

开启@Async注解

<task:annotation-driven executor="annotationExecutor" />
<!-- 支持 @Async 注解 -->
<task:executor id="annotationExecutor" pool-size="20"/>


同时加入扫描注解
<context:component-scan/>


实现For example:

同步调用:

@Component
public class TestAsyncBean {
public void sayHello4() throws InterruptedException {
Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
System.out.println("我爱你啊!");
}


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Test
public void test_sayHello4() throws InterruptedException, ExecutionException {
System.out.println("你不爱我了么?");
testAsyncBean.sayHello4();
System.out.println("回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
}
}


输出结果:

你不爱我了么?

我爱你啊!

回的这么慢,你肯定不爱我了,我们还是分手吧。。。

同步代码会按照瀑布流的形式依次执行,如果中间哪步调用需要很长时间,那么程序会阻塞在哪里,等待调用执行完成。

使用@Async异步调用

异步调用,通过开启新的线程来执行调用的方法,不影响主线程。一步方法实际的执行交给了Spring的TaskExecutor来完成。

@Component
public class TestAsyncBean {
@Async
public void sayHello3() throws InterruptedException {
Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
System.out.println("我爱你啊!");
}
}


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Autowired
private TestAsyncBean testAsyncBean;
@Test
public void test_sayHello3() throws InterruptedException, ExecutionException {
System.out.println("你不爱我了么?");
testAsyncBean.sayHello3();
System.out.println("你竟无话可说, 我们分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
}
}


输出结果:

你不爱我了么?

你竟无话可说,我们分手吧。。。

我爱你啊!

异步调用,通过开启新的线程调用的方法,不影响主线程。异步方法实际的执行交给了Spring的TaskExecutor来完成。

上面这种方法是没有返回值的,下面尝试有返回值的异步调用:

有返回值的异步调用

@Component
public class TestAsyncBean {
@Async
public String sayHello2() throws InterruptedException {
Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
return "我爱你啊!";// 调用方调用后会立即返回,所以返回null
}
}


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Autowired
private TestAsyncBean testAsyncBean;
@Test
public void test_sayHello2() throws InterruptedException, ExecutionException {
System.out.println("你不爱我了么?");
System.out.println(testAsyncBean.sayHello2());
System.out.println("你说的啥? 我们还是分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
}
}


输出结果:

你不爱我了么?

null

你说的啥?我们还是分手吧。。。

通过直接获取返回值的方法是不行的,这就需要用到异步回调,异步方法返回值必须为Future<>,就像Callable与Future.

下面通过AsyncResult<>来获得异步调用的返回值

@Component
public class TestAsyncBean {
@Async
public Future<String> sayHello1() throws InterruptedException {
int thinking = 2;
Thread.sleep(thinking * 1000);//网络连接中 。。。消息发送中。。。
System.out.println("我爱你啊!");
return new AsyncResult<String>("发送消息用了"+thinking+"秒");
}
}


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Autowired
private TestAsyncBean testAsyncBean;
@Test
public void test_sayHello1() throws InterruptedException, ExecutionException {
Future<String> future = null;
System.out.println("你不爱我了么?");
future = testAsyncBean.sayHello1();
System.out.println("你竟无话可说, 我们分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
System.out.println(future.get());
}
}


输出结果:

你不爱我了么?

你竟无话可说,我们分手吧。。。

我爱你啊!

发送消息用了2秒

文章参考自作者 无信不立 感谢作者的无私分享。

关于@Async配置深入讲解博客 Clement-Xu

关于使用@Async配置的事物处理问题被注解方法不受事物管理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: