Before using spring boot, our approach was to define a task pool in the configuration file, and then throw the @Async annotated task into the task pool for execution. So in spring boot, how to implement asynchronous task calls, the method is simpler.
Let's combine the previous one
Spring boot integrated JMS (ActiveMQ implementation)
The code in this blog is implemented.
1. Function description
When a consumer listens to a message in the queue, he or she processes the task receiving the message as an asynchronous task.
2. Code modification
Consumer 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 //This method will be executed asynchronously, which means the main thread will skip the method directly, but use the thread in the thread pool to execute the method public void receiveQueue(String text) { System.out.println(Thread.currentThread().getName()+": The message received by Consumer is: "+text); } }Consumer 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()+": The message received by Consumer2 is: "+text); return "return message"+text; } }Add the following annotation to the test class:
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 // Enable asynchronous tasks to support 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!!!"); } } } 3. Test results
DefaultMessageListenerContainer-1:Consumer2 receives: myname is chhliu!!! The reply message received from the out.queue queue is: return messagemyname is chhliu!!! SimpleAsyncTaskExecutor-45: The message received by Consumer is: myname is chhliu!!! DefaultMessageListenerContainer-1:Consumer2 receives: myname is chhliu!!! The reply message received from the out.queue queue is: return messagemyname is chhliu!!! SimpleAsyncTaskExecutor-46: The message received by Consumer is: myname is chhliu!!! DefaultMessageListenerContainer-1: The message received by Consumer2 is: myname is chhliu!!! The reply message received from the out.queue queue is: return messagemyname is chhliu!!! SimpleAsyncTaskExecutor-47: The message received by Consumer is: myname is chhliu!!! DefaultMessageListenerContainer-1: The message received by Consumer2 is: myname is chhliu!!! The reply message received from the out.queue queue is: return messagemyname is chhliu!!! SimpleAsyncTaskExecutor-48: The message received by Consumer is: myname is chhliu!!! DefaultMessageListenerContainer-1:Consumer2 is: myname is chhliu!!! The reply message received from the out.queue queue is: return messagemyname is chhliu!!! SimpleAsyncTaskExecutor-49: The message received by Consumer is: myname is chhliu!!! DefaultMessageListenerContainer-1: The message received by Consumer2 is: myname is chhliu!!! The reply message received from the out.queue queue is: return messagemyname is chhliu!!! SimpleAsyncTaskExecutor-50: The message received by Consumer is: myname is chhliu!!! DefaultMessageListenerContainer-1: The message received by Consumer2 is: myname is chhliu!!!
From the above test results, we can see that since Consumer 2 does not use the asynchronous task method, the consumer 2 consumes messages are processed by the fixed thread DefaultMessageListenerContainer-1. Consumer 1 uses the asynchronous task method, and each time the received message is processed by a different thread. When the message is received, the task is thrown directly into the task pool for processing, and the main thread continues to run. It can also be inferred from the test results that spring boot uses the newCachedThreadPool thread pool by default.
For specific usage of thread pools, please refer to another blog post from me: //www.VeVB.COM/article/134870.htm
4. Asynchronous tasks have returned
In actual development, we often encounter situations where asynchronous tasks return. So how do we implement it in spring boot?
The following is an example of asynchronous email sending as a description. The example code is as follows:
@Async("taskExecutePool") // The asynchronous task will be submitted to the taskExecutePool task pool to execute public Future<Response> doSendEmail(MailInfo mailInfo) {// The asynchronous task returns, use Future<Response> to asynchronously return log.info(Thread.currentThread().getName()+"The doSendEmail asynchronous method was called!"); SendMailSession session = null; Response res = new Response(); boolean isOK = sendEmail(mailInfo);// The specific method of sending emails if(isOK){ res.setSuccess(true); }else{ res.setSuccess(false); } return new AsyncResult<Response>(res);How to use it after returning? The sample code is as follows:
Future<Response> result = taskJob.doSendEmail(mailInfo); res = result.get(6, TimeUnit.SECONDS);
This way you can get the return of the asynchronous task!
Summarize
The above is the implementation method of spring boot asynchronous (Async) task scheduling introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!