spring boot异步(Async)任务调度实现方法

所属分类: 软件编程 / java 阅读数: 30
收藏 0 赞 0 分享

     在没有使用spring boot之前,我们的做法是在配置文件中定义一个任务池,然后将@Async注解的任务丢到任务池中去执行,那么在spring boot中,怎么来实现异步任务的调用了,方法更简单。

我们还是结合前面

spring boot整合JMS(ActiveMQ实现)

这篇博客里面的代码来实现。

一、功能说明

消费者在监听到队列里面的消息时,将接收消息的任务作为异步任务处理。

二、代码修改

消费者1:

package com.chhliu.springboot.jms; 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.scheduling.annotation.Async; 
import org.springframework.stereotype.Component; 
@Component 
public class Consumer { 
 @JmsListener(destination = "mytest.queue") 
 @Async //该方法会异步执行,也就是说主线程会直接跳过该方法,而是使用线程池中的线程来执行该方法 
 public void receiveQueue(String text) { 
  System.out.println(Thread.currentThread().getName()+":Consumer收到的报文为:"+text); 
 } 
} 

消费者2:

package com.chhliu.springboot.jms; 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.messaging.handler.annotation.SendTo; 
import org.springframework.stereotype.Component; 
@Component 
public class Consumer2 { 
 @JmsListener(destination = "mytest.queue") 
 @SendTo("out.queue") 
 public String receiveQueue(String text) { 
  System.out.println(Thread.currentThread().getName()+":Consumer2收到的报文为:"+text); 
  return "return message"+text; 
 } 
} 

在测试类上添加如下注解:

package com.chhliu.springboot.jms; 
import javax.jms.Destination; 
import org.apache.activemq.command.ActiveMQQueue; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.scheduling.annotation.EnableAsync; 
import org.springframework.test.context.junit4.SpringRunner; 
@RunWith(SpringRunner.class) 
@SpringBootTest 
@EnableAsync // 开启异步任务支持 
public class SpringbootJmsApplicationTests { 
 @Autowired 
 private Producer producer; 
 @Test 
 public void contextLoads() throws InterruptedException { 
  Destination destination = new ActiveMQQueue("mytest.queue"); 
  for(int i=0; i<100; i++){ 
   producer.sendMessage(destination, "myname is chhliu!!!"); 
  } 
 } 
} 

三、测试结果 

DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
SimpleAsyncTaskExecutor-45:Consumer收到的报文为:myname is chhliu!!! 
DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
SimpleAsyncTaskExecutor-46:Consumer收到的报文为:myname is chhliu!!! 
DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
SimpleAsyncTaskExecutor-47:Consumer收到的报文为:myname is chhliu!!! 
DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
SimpleAsyncTaskExecutor-48:Consumer收到的报文为:myname is chhliu!!! 
DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
SimpleAsyncTaskExecutor-49:Consumer收到的报文为:myname is chhliu!!! 
DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
SimpleAsyncTaskExecutor-50:Consumer收到的报文为:myname is chhliu!!! 
DefaultMessageListenerContainer-1:Consumer2收到的报文为:myname is chhliu!!! 

从上面的测试结果可以看出,由于消费者2没有使用异步任务方式,所以消费者2消费消息都是由固定的线程DefaultMessageListenerContainer-1这个线程来处理的,而消费者1由于使用了异步任务的方式,每次处理接收到的消息都是由不同的线程来处理的,当接收到消息时,直接将任务丢到任务池中去处理,而主线程则继续跑,从测试结果中还可以推断出,spring boot默认使用了newCachedThreadPool线程池来实现。

关于线程池的具体用法,请参考我的另一篇博文:https://www.jb51.net/article/134870.htm

四、异步任务有返回

在实际的开发中,我们会经常遇到异步任务有返回的情况,那么在spring boot中,怎么来实现了?

下面以异步发邮件为例,来进行说明,示例代码如下:

@Async("taskExecutePool") // 异步任务会提交到taskExecutePool任务池中执行 
 public Future<Response> doSendEmail(MailInfo mailInfo) {// 异步任务返回,使用Future<Response>来异步返回 
  log.info(Thread.currentThread().getName()+"调用了doSendEmail异步方法!"); 
  SendMailSession session = null; 
  Response res = new Response(); 
  boolean isOK = sendEmail(mailInfo);// 具体发邮件的方法 
 if(isOK){   
  res.setSuccess(true);  
 }else{ 
  res.setSuccess(false); 
 } 
 return new AsyncResult<Response>(res); 

返回之后怎么使用?示例代码如下:

Future<Response> result = taskJob.doSendEmail(mailInfo); 
   res = result.get(6, TimeUnit.SECONDS); 

这样就可以获取到异步任务的返回了!

以上所述是小编给大家介绍的spring boot异步(Async)任务调度实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

java实现背单词程序

这篇文章主要为大家详细介绍了java实现背单词程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java实现单词查询小程序

这篇文章主要为大家详细介绍了java实现单词查询小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java程序开发环境配置图文教程

这篇文章主要为大家详细介绍了Java程序开发环境配置图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解ssh框架原理及流程

在本文中小编给大家整理的是关于ssh框架原理及流程的相关知识点内容,有此需要的朋友们可以学习下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解springmvc常用5种注解

在本篇里我们给大家总结了关于springmvc常用5种注解相关知识点以及实例代码,需要的朋友们参考下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作(2)

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot假死诊断实战记录

这篇文章主要给大家介绍了关于Spring Boot假死诊断的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java计时新姿势StopWatch详解

这篇文章主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现点击按钮弹出新窗体功能

这篇文章主要为大家详细介绍了java实现点击按钮弹出新窗体功能,旧窗体不进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多