|
该版本仍在开发中,尚未被视为稳定。对于最新稳定版本,请使用 spring-cloud-task 5.0.0! |
开始
如果你刚开始使用春季云任务,应该阅读这一部分。 在这里,我们回答了基本的“什么?”、“怎么做?”和“为什么?”问题。我们从一个 温和的春云任务介绍。然后我们构建了一个 Spring Cloud 任务应用程序, 我们边讨论一些核心原则。
开发你的第一个春季云任务应用
一个好的起点是创建一个简单的“Hello, World!”应用程序,因此我们创建了 Spring Cloud Task相当于突出框架的功能。大多数IDE都包含 Apache Maven 支持良好,因此我们用它作为该项目的构建工具。
spring.io 网站上包含许多内容“开始”
指南使用Spring Boot的。如果你需要解决具体问题,先去那里看看。
你可以通过进入 Spring Initializr 创建新项目来快捷完成以下步骤。正在这样做
它会自动生成新的项目结构,让你可以立即开始编码。
我们建议你先尝试一下Spring Initializr,熟悉它。 |
使用 Spring Initializr 创建 Spring 任务项目
现在我们可以创建并测试一个打印世界您好!去控制台。
具体做法:
-
访问Spring Initialzr网站。
-
创建一个新的 Maven 项目,组名为
io.spring.demo以及一个名为HelloWorld. -
在“依赖”文本框中,输入
任务然后选择任务与春云标签。 -
在“依赖”文本框中,输入
H2然后选择H2与SQL标签。 -
点击“生成项目”按钮
-
-
解压helloworld.zip文件,把项目导入你喜欢的IDE。
编写代码
为了完成我们的应用,我们需要更新生成的Helloworld应用以下列内容启动任务。
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/主/资源. 我们需要配置两个属性application.properties:
-
application.name: 以设置应用程序名称(转换为任务名称) -
logging.level: 将 Spring Cloud 任务的日志设置为调试为了了解发生了什么。
以下示例展示了如何同时实现这两种情况:
logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloWorld
任务自动配置
当包含 Spring Cloud Task Starter 依赖时,任务会自动配置所有 BEANS 以启动其功能。该配置的一部分会注册任务仓库以及其使用的基础设施。
在我们的演示中,任务仓库使用嵌入式的H2数据库来记录任务的结果。这个H2嵌入式数据库并不适合生产环境,因为任务结束后,H2数据库就会消失。不过,为了快速开始体验,我们可以在示例中使用它,同时向日志中回应正在更新的内容在该仓库中。在配置部分(本文档后面)中,我们介绍了如何自定义由Spring Cloud Task提供的部分配置。
当我们的示例应用运行时,Spring Boot 会启动HelloWorldApplicationRunner并输出我们的“Hello, World!”消息以实现标准输出。 这任务生命周期监听器记录任务在仓库中的开始和结束。
主要方法
主方法作为任何 Java 应用的入口。我们的主方法委托给 Spring Boot 的 SpringApplication 类。
运行示例
此时,我们的应用程序应该可以运行了。由于这个应用程序基于 Spring Boot,我们可以通过以下方式从命令行运行它$ ./mvnw Spring靴:run从我们应用的根节点出发,如下示例所示(及其输出):
$ mvn clean spring-boot:run
....... . . .
....... . . . (Maven log output here)
....... . . .
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.0)
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)
....... . . .
前面的输出有三条我们感兴趣的行:
-
简单任务仓库记录了该条目的创建过程任务仓库. -
我们的执行
应用运行者,通过“Hello, World!”的输出得到了展示。 -
简单任务仓库记录任务的完成情况任务仓库.
| 一个简单的任务应用可以在Spring Cloud的示例模块中找到。任务项目在这里。 |