Şuayb's BlogŞuayb's Blog
Home
Categories
Games
MediumAboutContact
Language
Theme
    1. Blog
    2. Programming
    3. Spring Boot Async Tasks

Spring Boot Async Tasks

PublishedFebruary 20, 2025
UpdatedFebruary 21, 2025
Reading time3 min read
JavaKotlinSpring BootAsync Tasks
XLinkedInFacebook
Spring Boot Async Tasks

Loading likes...

Spring Boot provides an easy way to run asynchronous tasks using the @Async annotation. This is useful for executing tasks in the background, improving performance, and avoiding blocking the main thread.


Last updatedFebruary 21, 2025

Total viewsLoading hits...

Previous articleSpring Boot Scheduled TasksNext articleSpring Boot Kubernetes Integration
Şuayb Şimşek

Written by

Şuayb Şimşek

Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.

Expertise

  • Spring Boot
  • Go
  • Microservices
  • Next.js
  • Cloud Native

Connect

GitHubLinkedInMedium

Related posts

Spring Boot Async Tasks with Virtual Thread
Programming

Spring Boot Async Tasks with Virtual Thread

Learn how to run asynchronous tasks with `@Async` on JDK 21 virtual threads in Spring Boot.

April 26, 20253 min read
JavaKotlinSpring BootAsync TasksVirtual Thread
Spring Boot Configuration Properties
Programming

Spring Boot Configuration Properties

Learn how to use @ConfigurationProperties for type-safe configuration, validate settings with @Validated, and manage environment-specific values with profile-specific application-{profile}.yml files.

February 4, 20263 min read
JavaKotlinSpring BootConfiguration
Spring Boot GraphQL JWE Authentication
Programming

Spring Boot GraphQL JWE Authentication

Learn how to secure your Spring Boot GraphQL APIs with stateless encrypted JWTs (JWE) while persisting user identities and roles in a JPA-backed database.

May 17, 20256 min read
JavaKotlinSpring BootSecurityJWTJWEGraphQL

About

Articles on Spring Boot, microservices, security, and more.

ContactStart here

Latest posts

  • Captain Tsubasa 2: World Fighters
  • Captain Tsubasa: Rise of New Champions
  • Spring Boot Configuration Properties
  • Spring Boot GraphQL JWE Authentication
  • Spring Boot JWE Authentication with JPA

Top topics

JavaKotlinSpring BootJWEJWTMicroservice

Subscribe

Get practical backend + fullstack notes when new articles are published.

Social

© 2024-2026 Şuayb's Blog. All rights reserved.

🌟 Why Use @Async in Spring Boot?

In this section, we clarify Why Use @Async in Spring Boot? and summarize the key points you will apply in implementation.

  • Non-Blocking Execution: Runs tasks asynchronously without blocking the main thread.
  • Improved Performance: Executes independent tasks in parallel.
  • Better Scalability: Frees up resources for other processes.
  • Seamless Integration: Works with Spring Boot’s dependency injection and lifecycle management.

📋 Prerequisites

Ensure you have the following:

  • ☕ Java Development Kit (JDK) 17+
  • 📦 Maven or Gradle installed
  • 🔤 A Java IDE (e.g., IntelliJ IDEA, Eclipse)

🛠️ Step 1: Add Dependencies

To enable async processing, include spring-boot-starter-web in your pom.xml or build.gradle file.

Maven:

XMLpom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Gradle:

GROOVYbuild.gradle
implementation 'org.springframework.boot:spring-boot-starter-web'

🛠️ Step 2: Enable Async in Your Application

Annotate your main application class with @EnableAsync to enable asynchronous execution.


🛠️ Step 3: Create an Async Task

Define an asynchronous method using @Async.


🛠️ Step 4: Create a Controller to Trigger Async Tasks

Create a REST controller to trigger the asynchronous task.


▶️ Running the Application

Run the Spring Boot application:

BASH
./mvnw spring-boot:run

Or using Gradle:

BASH
gradle bootRun

🧪 Testing the Async Task

In this section, we clarify Testing the Async Task and summarize the key points you will apply in implementation.

Trigger Async Task:

Call this endpoint to trigger the async workflow and observe execution flow in logs.

BASH
curl -X GET http://localhost:8080/async/run

Expected Console Output:

Compare your console logs with this output to quickly confirm the behavior is correct.

PLAINTEXTsnippet.txt
Async task executed at: 12:00:01

🏁 Conclusion

You now have a practical Spring Boot Async Tasks implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.

JAVAAsyncApplication.java
package com.example.async;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class AsyncApplication {
    public static void main(String[] args) {
        SpringApplication.run(AsyncApplication.class, args);
    }
}
KOTLINAsyncApplication.kt
package com.example.async

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableAsync

@SpringBootApplication
@EnableAsync
class AsyncApplication

fun main(args: Array<String>) {
    runApplication<AsyncApplication>(*args)
}
JAVAAsyncTask.java
package com.example.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.time.LocalTime;

@Service
public class AsyncTask {

    @Async
    public void runTask() {
        System.out.println("Async task executed at: " + LocalTime.now());
    }
}
KOTLINAsyncTask.kt
package com.example.async

import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
import java.time.LocalTime

@Service
class AsyncTask {

    @Async
    fun runTask() {
        println("Async task executed at: ${LocalTime.now()}")
    }
}
JAVAAsyncController.java
package com.example.async;

import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/async")
@RequiredArgsConstructor
public class AsyncController {

    private final AsyncTask asyncTask;

    @GetMapping("/run")
    public String triggerAsyncTask() {
        asyncTask.runTask();
        return "Async task triggered!";
    }
}
KOTLINAsyncController.kt
package com.example.async

import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/async")
class AsyncController(
    private val asyncTask: AsyncTask
) {

    @GetMapping("/run")
    fun triggerAsyncTask(): String {
        asyncTask.runTask()
        return "Async task triggered!"
    }
}