在沒有使用spring boot之前,我們的做法是在配置文件中定義一個任務池,然后將@Async注解的任務丟到任務池中去執行,那么在spring boot中,怎么來實現異步任務的調用了,方法更簡單。
我們還是結合前面
spring boot整合JMS(ActiveMQ實現)
這篇博客里面的代碼來實現。
一、功能說明
消費者在監聽到隊列里面的消息時,將接收消息的任務作為異步任務處理。
二、代碼修改
消費者1:
1
2
3
4
5
6
7
8
9
10
11
12
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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; } } |
在測試類上添加如下注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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!!!" ); } } } |
三、測試結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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線程池來實現。
關于線程池的具體用法,請參考我的另一篇博文:http://m.ythuaji.com.cn/article/153012.html
四、異步任務有返回
在實際的開發中,我們會經常遇到異步任務有返回的情況,那么在spring boot中,怎么來實現了?
下面以異步發郵件為例,來進行說明,示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@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); |
返回之后怎么使用?示例代碼如下:
1
2
|
Future<Response> result = taskJob.doSendEmail(mailInfo); res = result.get( 6 , TimeUnit.SECONDS); |
這樣就可以獲取到異步任務的返回了!
總結
以上所述是小編給大家介紹的spring boot異步(Async)任務調度實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/54605697