Asynchronous tasks

In Java applications, most of the interactive processing is achieved by means of synchronization. However, when dealing with the interaction with third party systems, it is easy to cause slow response. Most of the previous use of multithreading to complete such tasks, in fact, after Spring 3.x, @async has been built to solve this problem perfectly.

Two comments: @enableaysnc, @aysnc

First let’s look at operations without asynchronous interfaces

@Service
public class AsyncService {

    public void hello(a){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("In processing the data..."); }}Copy the code
@RestController
public class AsyncController {

    @Autowired
    AsyncService asyncService;

    @GetMapping("/hello")
    public String hello(a){
        asyncService.hello();
        return "success"; }}Copy the code

Start the server: We can see that the page waits three seconds before executing the desired result



After three seconds:

Here we add the synchronization operation (easy to do with just two annotations) :

@Service
public class AsyncService {

    // Tell Spring that this is an asynchronous method
    @Async
    public void hello(a){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("In processing the data..."); }}Copy the code

Don’t forget to enable asynchronous annotations on the launcher, otherwise asynchronous annotations won’t work

@EnableAsync  // Enable asynchronous annotations
@SpringBootApplication
public class Springboot04TaskApplication {

	public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); }}Copy the code

Here we can see that asynchrony can start successfully

Second, scheduled tasks

There are often scheduled tasks that need to be performed during project development, such as analyzing logs from the previous day at dawn every day. Spring provides us with a way to perform task scheduling asynchronously, providing TaskExecutor and TaskScheduler interfaces.

Two comments: @enablescheduling, @scheduled

Cron expression:



@Service
public class ScheduledService {

    /** * second(second), minute (minute), hour (hour), day of month (day), month (month), * 0 * * * * mon-fri * [0/5 14,18 * *?] * [0 15 10? * 1-6] Every Five minutes at 14:00 and 18:00 every day * [0 15 10? * 1-6] Every Monday to Saturday at 10:15 * [0 0 2? * 6L] Every last Saturday at 2:00 in the morning * [0 2 LW *?] * [0 0 2-4? * 1#1] Is executed at 2:00 am on the last working day of each month from 2:00 am to 4:00 am on the first Monday of each month. * /
   // @Scheduled(cron = "0 * * * * 2")
    // Scheduled @cron = "0,1,2,3,4 * * * * 2"
   // @Scheduled(cron = "0-4 * * * * 2")
    @Scheduled(cron = "0/4 * * * * 2")  // Every 4 seconds on Tuesday
    public void hello(a){
        System.out.println("hello ... "); }}Copy the code

Enable annotation-based scheduled tasks

@EnableAsync  // Enable asynchronous annotations
@EnableScheduling // Enable annotation-based scheduled tasks
@SpringBootApplication
public class Springboot04TaskApplication {

	public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); }}Copy the code

View console print

Source code address: github.com/839022478/s…