当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot异步(Async)任务调度实现方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

     在没有使用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线程池来实现。

关于线程池的具体用法,请参考我的另一篇博文:

四、异步任务有返回

在实际的开发中,我们会经常遇到异步任务有返回的情况,那么在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)任务调度实现方法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网