开始
如果你刚开始使用春季云任务,应该阅读这一部分。 在这里,我们回答了基本的“什么?”、“怎么做?”和“为什么?”问题。我们从一个 温和的春云任务介绍。然后我们构建了一个 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 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的示例模块中可以找到一个简单的任务应用 任务项目在此。 |