spring-cloud-task(1)简单使用

Spring Cloud Task使用关系数据库来存储执行任务的结果。 虽然可以在没有数据库的情况下开始开发任务(任务的状态会记录为任务存储库更新的一部分),但对于生产环境,您将需要使用受支持的数据库。 以下是目前支持的列表:

  • DB2
  • H2
  • HSQLDB
  • MySql
  • Oracle
  • Postgres
  • SqlServer
注解@EnableTask 告诉Spring Cloud Task引导它的功能。这是通过默认导入其他配置类SimpleTaskConfiguration来实现的。这个额外的配置注册了TaskRepository及其使用的基础设施。
TaskRepository将使用内存Map来记录任务的结果。显然这对于​​生产环境来说不是一个实际的解决方案,因为一旦任务结束,Map就会消失。但是,对于快速入门体验,我们使用这个作为默认值,并向日志回应该存储库中正在更新的内容。
TaskLifecyceListener将记录任务的开始和任务的结束。

在debug日志中:

2018-01**SimpleTaskRepository   : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='helloWorld', startTime=Sat Jan 06 00:18:44 CST 2018, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
Hello World!
2018-01-**SimpleTaskRepository   : Updating: TaskExecution with executionId=0 with the following {exitCode=0, endTime=Sat Jan 06 00:18:44 CST 2018, exitMessage='null', errorMessage='null'}
SimpleTaskRepository 创建了TaskRepository。
执行结束后
SimpleTaskRepository记录TaskRepository中任务的完成情况。

测试用例:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = {"spring.cloud.task.closecontext_enabled=false"})
public class DemoApplicationTests {
	@Test
	public void contextLoads() {
	//your test here
	}
}

如果应用里面有数据库链接,默认自动创建表,通过:

spring.cloud.task.initialize.enable=


你可能感兴趣的:(Spring-cloud系列)