Master Short‑Lived Microservices with Spring Cloud Task: A Hands‑On Guide
This article explains how to build short‑lived microservices using Spring Cloud Task on Spring Boot 3.5.0, covering environment setup, Maven dependencies, quick start code, configuration files, first task implementation, advanced options like custom table prefixes, external and parent task IDs, and task execution listeners with full code examples and screenshots.
Introduction
In a microservice architecture, some services only need to run briefly to perform a specific job and then terminate. Spring Cloud Task enables the creation of such short‑lived microservices, runnable locally, in the cloud, or on Spring Cloud Data Flow.
Prerequisites
Environment: Spring Boot 3.5.0.
Setup
Prepare Environment
<properties>
<spring-cloud.version>2025.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Quick Start
Add @EnableTask to a Spring Boot application and run it.
@SpringBootApplication
@EnableTask
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}Configuration example (application.yml):
spring:
application:
name: short-term-task
logging:
level:
'[org.springframework.cloud.task]': DEBUGFirst Task
Implement a CommandLineRunner (or ApplicationRunner) to define the task logic.
@Component
public class HelloRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.err.println("执行任务...");
}
}Running the application prints the message, creates the default tables (e.g., task_execution) and records the task execution.
Advanced Configuration
Modify Table Prefix
spring:
cloud:
task:
table-prefix: PACK_TASK_If you change the prefix, you must create the corresponding tables yourself. See the DDL scripts in the Spring Cloud Task repository.
https://github.com/spring-cloud/spring-cloud-task/tree/master/spring-cloud-task-core/src/main/resources/org/springframework/cloud/task
External Task ID
Set a specific TaskExecution ID when the task starts.
spring:
cloud:
task:
executionid: 10000If the ID does not exist in TASK_EXECUTION, startup fails (see screenshot).
External Execution ID
spring:
cloud:
task:
external-execution-id: 88888Parent Task ID
spring:
cloud:
task:
parent-execution-id: 99999You can also supply it via a command‑line argument:
java -jar child_task_app.jar --PARENT_TASK_ID=666888Task Execution Listeners
Implement TaskExecutionListener or use the annotations @BeforeTask, @AfterTask, and @FailedTask to react to lifecycle events.
@Component
public class PackTaskListener {
@BeforeTask
public void before(TaskExecution taskExecution) {
System.err.println("%s - 任务准备执行".formatted(taskExecution.getTaskName()));
}
@AfterTask
public void after(TaskExecution taskExecution) {
System.err.println("%s - 任务执行完成".formatted(taskExecution.getTaskName()));
}
@FailedTask
public void failed(TaskExecution taskExecution, Throwable throwable) {
System.err.println("%s - 任务准备失败,错误信息: %s".formatted(taskExecution.getTaskName(), throwable.getMessage()));
}
}Running a task with this listener produces console output like the following:
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
