对于最新的稳定版本,请使用 spring-cloud-task 3.3.0! |
开始
如果您刚刚开始使用 Spring Cloud Task,您应该阅读本节。 在这里,我们回答基本的“什么”、“如何”和“为什么”问题。我们从 温和地介绍 Spring Cloud Task。然后我们构建一个 Spring Cloud Task 应用程序, 边走边讨论一些核心原则。
开发您的第一个 Spring Cloud 任务应用程序
一个好的起点是从一个简单的“Hello, World!”应用程序开始,因此我们创建了 Spring Cloud Task 等效,突出框架的特性。大多数 IDE 都有 对 Apache Maven 的良好支持,因此我们将其用作该项目的构建工具。
spring.io 网站包含许多“Getting Started ”
指南使用 Spring Boot 的。如果您需要解决特定问题,请先检查那里。
您可以通过转到 Spring Initializr 并创建一个新项目来快捷执行以下步骤。这样做
自动生成新的项目结构,以便您可以立即开始编码。
我们建议尝试使用 Spring Initializr 来熟悉它。 |
使用 Spring Initializr 创建 Spring 任务项目
现在我们可以创建和测试一个打印Hello, World!
到控制台。
为此,请执行以下作:
-
访问 Spring Initialzr 网站。
-
创建一个组名称为
io.spring.demo
和 Artifact 名称helloworld
. -
在依赖项文本框中,键入
task
,然后选择Task
依赖于Spring Cloud
标签。 -
在依赖项文本框中,键入
h2
,然后选择H2
依赖于SQL
标签。 -
单击“生成项目”按钮
-
-
解压缩 helloworld.zip 文件并将项目导入到您最喜欢的 IDE 中。
编写代码
要完成我们的应用程序,我们需要更新生成的HelloworldApplication
替换为以下内容,以便启动任务。
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableTask
public class HelloworldApplication {
@Bean
public ApplicationRunner applicationRunner() {
return new HelloWorldApplicationRunner();
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
public static class HelloWorldApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("Hello, World!");
}
}
}
虽然它看起来很小,但正在发生相当多的事情。有关 Spring 的更多信息 引导细节,请参阅 Spring Boot 参考文档。
现在我们可以打开application.properties
文件输入src/main/resources
.
我们需要在application.properties
:
-
application.name
:设置应用程序名称(转换为任务名称) -
logging.level
:将 Spring Cloud Task 的日志记录设置为DEBUG
为了 了解正在发生的事情。
以下示例演示了如何同时执行这两项作:
logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloWorld
任务自动配置
当包含 Spring Cloud Task Starter 依赖项时,Task 会自动配置所有 bean 以引导其功能。
此配置的一部分注册了TaskRepository
以及使用它的基础设施。
在我们的演示中,TaskRepository
使用嵌入式 H2 数据库记录结果
任务的。此 H2 嵌入式数据库对于生产环境来说并不是一个实用的解决方案,因为
任务结束后,H2 数据库就会消失。但是,为了快速入门
经验,我们可以在示例中使用它,并将正在更新的内容回显到日志中
在该存储库中。在“配置”部分(后面的
文档),我们将介绍如何自定义由
Spring Cloud 任务。
当我们的示例应用程序运行时,Spring Boot 会启动我们的HelloWorldApplicationRunner
并将我们的“Hello, World!”消息输出到标准输出。这TaskLifecycleListener
在存储库中记录任务的开始和结束。
主要方法
main 方法充当任何 java 应用程序的入口点。我们的主要方法 委托给 Spring Boot 的 SpringApplication 类。
应用程序运行器
Spring 包含许多引导应用程序逻辑的方法。Spring Boot 提供
一种通过其*Runner
接口
(CommandLineRunner
或ApplicationRunner
).一个行为良好的任务可以引导任何
逻辑,使用这两个运行器之一。
任务的生命周期从*Runner#run
方法被执行
等它们全部完成后。Spring Boot 允许应用程序使用多个*Runner
实现,Spring Cloud Task 也是如此。
从除CommandLineRunner 或ApplicationRunner (通过使用InitializingBean#afterPropertiesSet 例如)不是
由 Spring Cloud Task 录制。 |
运行示例
此时,我们的应用程序应该可以工作。由于此应用程序是基于 Spring Boot 的,
我们可以使用$ ./mvnw spring-boot:run
从根源
如以下示例所示(及其输出):
$ mvn clean spring-boot:run
....... . . .
....... . . . (Maven log output here)
....... . . .
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.1)
2024-01-04T10:07:01.102-06:00 INFO 18248 --- [helloWorld] [ main] i.s.d.helloworld.HelloworldApplication : Starting HelloworldApplication using Java 21.0.1 with PID 18248 (/Users/dashaun/fun/dashaun/spring-cloud-task/helloworld/target/classes started by dashaun in /Users/dashaun/fun/dashaun/spring-cloud-task/helloworld)
2024-01-04T10:07:01.103-06:00 INFO 18248 --- [helloWorld] [ main] i.s.d.helloworld.HelloworldApplication : No active profile set, falling back to 1 default profile: "default"
2024-01-04T10:07:01.526-06:00 INFO 18248 --- [helloWorld] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2024-01-04T10:07:01.626-06:00 INFO 18248 --- [helloWorld] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:3ad913f8-59ce-4785-bf8e-d6335dff6856 user=SA
2024-01-04T10:07:01.627-06:00 INFO 18248 --- [helloWorld] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2024-01-04T10:07:01.633-06:00 DEBUG 18248 --- [helloWorld] [ main] o.s.c.t.c.SimpleTaskAutoConfiguration : Using org.springframework.cloud.task.configuration.DefaultTaskConfigurer TaskConfigurer
2024-01-04T10:07:01.633-06:00 DEBUG 18248 --- [helloWorld] [ main] o.s.c.t.c.DefaultTaskConfigurer : No EntityManager was found, using DataSourceTransactionManager
2024-01-04T10:07:01.639-06:00 DEBUG 18248 --- [helloWorld] [ main] o.s.c.t.r.s.TaskRepositoryInitializer : Initializing task schema for h2 database
2024-01-04T10:07:01.772-06:00 DEBUG 18248 --- [helloWorld] [ main] o.s.c.t.r.support.SimpleTaskRepository : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='helloWorld', startTime=2024-01-04T10:07:01.757268, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
2024-01-04T10:07:01.785-06:00 INFO 18248 --- [helloWorld] [ main] i.s.d.helloworld.HelloworldApplication : Started HelloworldApplication in 0.853 seconds (process running for 1.029)
Hello, World!
2024-01-04T10:07:01.794-06:00 DEBUG 18248 --- [helloWorld] [ main] o.s.c.t.r.support.SimpleTaskRepository : Updating: TaskExecution with executionId=1 with the following {exitCode=0, endTime=2024-01-04T10:07:01.787112, exitMessage='null', errorMessage='null'}
2024-01-04T10:07:01.799-06:00 INFO 18248 --- [helloWorld] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2024-01-04T10:07:01.806-06:00 INFO 18248 --- [helloWorld] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
....... . . .
....... . . . (Maven log output here)
....... . . .
前面的输出有三行,我们在这里感兴趣:
-
SimpleTaskRepository
在TaskRepository
. -
执行我们的
ApplicationRunner
,由“Hello, World!” 输出演示。 -
SimpleTaskRepository
将任务的完成记录在TaskRepository
.
可以在 Spring Cloud 的示例模块中找到一个简单的任务应用程序任务项目在这里。 |