Recently in the development of a new company , Come across a scene like this , There is a small function of reminder , Customers can make reminders half an hour after ordering , Our logic at that time was that every time our customers urged us , It will send a message to the company's operator's enterprise wechat and return a reminder interval to the front end .
According to the coding habits I just started to enter the company , I will definitely send the message first , Later, store the data in the database , This sequence is executed . For people with project experience , Will certainly use the asynchronous method to improve the performance of the program . Why? ? Because when calling the third-party interface , There may be situations , for example , Message sending failed or timed out due to network reasons , At this time, our asynchronous method is particularly important .
Scenarios for using asynchronous methods : In the project , When accessing other people's interfaces is slow or time-consuming , I don't want the program stuck in time-consuming tasks , I want the program to run in parallel , We can use multithreading to process tasks in parallel , It can also be used spring Asynchronous processing mode provided @Async. for example , Send short message , Send mail ,APP Message push, etc .
Here is an example of an asynchronous method I wrote demo
First of all, you need to annotate the startup class @EnableAsync
package com.xzz.service.sale; import
org.springframework.boot.SpringApplication; import
org.springframework.boot.autoconfigure.SpringBootApplication; import
org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication
@EnableAsync public class ServiceSaleApplication { public static void
main(String[] args) { SpringApplication.run(ServiceSaleApplication.class,
args); } }
Asynchronous method class
package com.xzz.service.sale.mytest; import lombok.extern.slf4j.Slf4j; import
org.springframework.scheduling.annotation.Async; import
org.springframework.stereotype.Component; @Component @Slf4j public class
TestAnysc { @Async public void dealNoReturnTask(){ log.info("Thread {} deal No
Return Task start", Thread.currentThread().getName()); try {
Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }
log.info("Thread {} deal No Return Task end at {}",
Thread.currentThread().getName(), System.currentTimeMillis()); } }
Test class
/** * test @Async asynchronous * */ @Autowired TestAnysc testAnysc; @Test public void
testDealNoReturnTask(){ testAnysc.dealNoReturnTask(); try { log.info("begin to
deal other Task!"); Thread.sleep(10000); } catch (InterruptedException e) {
e.printStackTrace(); } }
Test results
begin to deal other Task! Thread {} deal No Return Task start Thread {} deal
No Return Task end at {}
It turned out that we succeeded .
Now I'll summarize , I'm using it @Async Stepped on the pit
Asynchronous class not used @Component annotation ( Or other notes ) cause spring Unable to scan for asynchronous class ;
Testing asynchronous methods cannot be in the same class as asynchronous methods ;
You need to use the @Autowired or @Resource Automatic annotation injection , You can't do it yourself new object ;
If used SpringBoot The framework must be added to the boot class @EnableAsync annotation .
Technology
Daily Recommendation