Use asynchronous @Async annotation in the SpringBoot project

tags: spring boot  java  spring

The first step is to add @EnableAsync annotations to the startup class (must)
/**
   * starting program
 * 
 */
@SpringBootApplication
@EnableAsync
public class RuoYiApplication
{
    public static void main(String[] args)
    {
        
    }
}

The second step, in the controller, the method T (), remember that the asynchronous method cannot be called in the same type

@GetMapping("/list")
public void test01() throws Exception{

    batchPaymentService.t();
System.out.println ("After executing");
}

In the third step, adding annotations @Async (method Void and Future (waiting for asynchronous execution) in the service method to be asynchronous in the service layer

@Async
public void t() throws InterruptedException {
    System.out.println("1");
    Thread.sleep(10000);
    for (int i = 0; i < 100; i++) {
        System.out.println(i);
    }
    log.info("ttt");
}

The results of the execution can be seen that the asynchronous (first printed the execution, and the asynchronous method is printed)

Intelligent Recommendation

SpringBoot implements asynchronous tasks (@Async annotation)

The project example has been uploaded to GitHub, visiblegithub project address。    Before explaining the asynchronous call, let's first look at the definition of synchronous call; synch...

SpringBoot asynchronous task @Async annotation (four)

SpringBoot asynchronous task @Async annotation (four) To write an interface, first call the proce() method, and then return a string to the browser: Write a business logic. Sleeping for 5 seconds in t...

SpringBoot asynchronous task-using @Async annotation

Common high concurrent plan Asynchronous, peak -cutting valley Caches, the cache is relatively stable high -frequency hotspot data Parallel, shorten the business response time Optimize your business c...

[] SpringBoot use @Async asynchronous operation

Background development process, when a time-consuming operation is often required to open a thread to perform asynchronous operations, providing @Async notes in SpringBoot can make us more quickly usi...

Use @Async annotation processing asynchronous call

inJavaApplications, the vast majority of cases are achieved through synchronous way interactive process; but when dealing with third-party systems to interact, likely to cause slow response to the sit...

More Recommendation

Use @Async annotation to execute asynchronous programs

Directly paste code Custom thread pool Asynchronous service Test program Output result, time is 5010 milliseconds, asynchronous execution is successful...

SpringBoot @Async annotation with @EnableAsync annotation to open asynchronous task

table of Contents Foreword No asynchronous execution, no @Async annotation Execute tasks asynchronously, when adding @Async annotations Foreword @Async is an asynchronous annotation, placed on the met...

Asynchronous SpringBoot project combat the call @Async

Demand: The company projects require the user to fill in basic information, such as when the name and license plate number, he needs to be inserted over the previous data read together. Idea: Because ...

How to support the SpringBoot project (ASYNC asynchronous task)

  Why do you need an asynchronous task? 1. If a request is sent to the server (assuming: send mail or SMS notification or batch message to the batch), due to the large number of baseline users, i...

@Async asynchronous annotation

front end:   Back-end control layer: Background service: Browser: http://localhost:8090/MyLab/rpc/async/asyncTest In the asyncTest method, a for loop in the service will be called. This for loop ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top