SpringBoot Async asynchronous call

Keywords: SpringBoot

SpringBoot Async asynchronous call

Asynchronous methods are sometimes needed in development. SpringBoot provides an asynchronous method that can be easily used with only two annotations, @Async and@EnableAsync annotations.

SpringBoot version 2.x.

1. Non-asynchronous examples.

2. Asynchronous examples.

1. Non-asynchronous examples.

Create a simple web project to ensure it works. The requirements of the project are as follows.

1. Create an instance class

public class User {
    private Integer id;
    private String name;
    private String gender;

    //Leave out get,set, toString methods.

    public User(Integer id, String name, String gender) {
        this.id = id;
        this.name = name;
        this.gender = gender;
    }

    public User() {
    }
}

2. Create a Service class.

@Service
public class AsyncService {

    public User Hello(){
        User user=new User(10010,"Jerry","male");

        System.out.println("Hello, everyone. I am"+user.getName());
        return user;

    }
}

3. Create a Controller class.

@RestController
public class AsyncController {
    
    @Autowired
    AsyncService asyncService;
    
    @RequestMapping("/a")
    public User async(){

        System.out.println("Start execution Hello Method...");
        User user = asyncService.Hello();
        return user ;
    }
}

4. Configure your own port and path, open the browser, access the controller path.

At this point, the console prints as follows

Start executing the Hello method...
Hello, everyone. I'm Jerry.

The browser is as follows

{"id":10010,"name":"Jerry","gender":"male"}

Access is delayed at this time.

5. Modify the Controller class and add Thread.sleep(5000); to make the current thread sleep for a while.

@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/a")
    public User async() throws InterruptedException {

        System.out.println("Start execution Hello Method...");
        
        Thread.sleep(5000);
        User user = asyncService.Hello();

        return user ;
    }

}

6. Restart the project and access the controller.

At this point, the console will only appear the following. Browser is refreshing, no content.

Start executing the Hello method...

Wait 5 seconds. The following contents appear in the console and data appears in the browser.

Hello, everyone. I'm Jerry.

2. Asynchronous examples

SpringBoot's method of opening asynchrony is extremely simple. Simply add the annotation @Async to the method that needs to open asynchrony and @EnableAsync to the entry class to open the annotation.

1. Modify the Service class based on the first four steps of the above example, adding thread sleep and asynchronous annotations.

@Async
    public User Hello() throws InterruptedException {
        User user=new User(10010,"Jerry","male");
        Thread.sleep(5000);
        System.out.println("Hello, everyone. I am"+user.getName());
        return user;

    }

2. Modify the contents of methods in the Controller class.

 @RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/a")
    public User async() throws InterruptedException {

        System.out.println("Start execution Hello Method...");

        User user=asyncService.Hello();

        System.out.println("Hello Method execution ends...");
        return user ;
    }

}

3. Add the annotation @EnableAsync to the startup class.

4. Access path. The following data are obtained

Start executing the Hello method...
Hello method execution ends...
Hello, everyone. I'm Jerry.

You can see that the method in service is executed asynchronously.

Asynchronous methods do not become blocking

Posted by bjdouros on Sat, 07 Sep 2019 01:46:21 -0700