Asynchronous processing with regular execution in Spring Boot

Overview

It is a memorandum because I tried periodic execution and asynchronous processing with Spring Boot.

environment

OpenJDK 11 Spring Boot 2.2.1

Periodic execution

I referred to here. How to periodically execute tasks in Spring Boot

@EnableScheduling
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

@Scheduled(fixedRate = 10000)
public void scheduledTask() {
    this.doTask();
}

Use the Scheduled annotation.

Asynchronous processing

Async annotations can be used for asynchronous processing.

@EnableAsync
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
@Async
public void doTask() {
    NormalTask normalTask = new NormalTask();
    normalTask.do();
}

When the doTask method is called, the process is executed in another thread.

Do it at the same time

I thought that this combination would allow periodic execution → asynchronous processing, but the combination of Scheduled and Async does not result in asynchronous processing.

@Async
public void doTask() {
    NormalTask normalTask = new NormalTask();
    normalTask.do();
}

@Scheduled(fixedRate = 10000)
public void scheduled() {
    this.doTask();
}

In the above example, Async doesn't work and normalTask.do () is blocked.

The method with Scheduled annotation creates Runnable and processes it in another thread, but Async does not work at this time. ScheduledThreadPoolExecutor, which manages periodic execution, seems to block until the end of Runnable's run () (to calculate the next periodic execution time from the end).

Therefore, Scheduled and Async annotation cannot be used together.

solution

It was solved by creating another thread in Runnable created by Scheduled and executing the process in it. I think that there is no problem because Runnable created by Scheduled ends as soon as another thread for asynchronous processing is created.

AsyncTask.java


public class AsyncTask implements Runnable {

    @Override
    public void run() {
        // Do some task
    }
}

public void doTask() {
    AsyncTask asyncTask = new AsyncTask();
    Thread newThread = new Thread(asyncTask);
    newThread.start();
}

@Scheduled(fixedRate = 10000)
public void scheduled() {
    this.doTask();
}

If you have any suggestions, please feel free to contact us.

reference

How to periodically execute tasks in Spring Boot using @Scheduled and @Async together?

Recommended Posts

Asynchronous processing with regular execution in Spring Boot
Asynchronous processing with Spring Boot using @Async
Spring with Kotorin --6 Asynchronous processing
I checked asynchronous execution of queries in Spring Boot 1.5.9
Processing at application startup with Spring Boot
Send regular notifications with LineNotify + Spring Boot
Download with Spring Boot
Include external jar in package with Spring boot2 + Maven3
Execute arbitrary processing after Basic authentication with Spring boot.
SSO with GitHub OAuth in Spring Boot 1.5.x environment
Until you start development with Spring Boot in eclipse 1
Until you start development with Spring Boot in eclipse 2
Database environment construction with Docker in Spring boot (IntellJ)
Set context-param in Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Asynchronous processing with Shoryuken + SQS
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Major changes in Spring Boot 1.5
Add module with Spring Boot
Getting Started with Spring Boot
NoHttpResponseException in Spring Boot + WireMock
Create microservices with Spring Boot
Send email with spring boot
Execution of initial processing using Spring Boot Command Line Runner
Use thymeleaf3 with parent without specifying spring-boot-starter-parent in Spring Boot
Use Basic Authentication with Spring Boot
Spring Boot application development in Eclipse
Try implementing asynchronous processing in Azure
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Write test code in Spring Boot
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
Implementation of asynchronous processing in Tomcat
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
Google Cloud Platform with Spring Boot 2.0.0
Event processing is performed in Spring.
Check date correlation with Spring Boot
Implement Spring Boot application in Gradle
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI