什么是Spring Cloud Task啊?简单示例下( 二 )

我们需要在TaskDemo类中声明实现类的bean :
@Beanpublic TaskListener taskListener() {return new TaskListener();}运行TaskDemo类,在控制台可看到任务被执行:
14:23:29.974 [main] INFOo.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup14:23:29.978 [main] INFOo.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 014:23:30.103 [main] INFOc.p.spring.cloud.task.TaskListener - Task Startup14:23:30.109 [main] INFOc.p.spring.cloud.task.TaskDemo - Hello World from Spring Cloud Task!14:23:30.113 [main] INFOc.p.spring.cloud.task.TaskListener - End of Task14:23:30.126 [main] INFOo.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a798d51: startup date [Fri Oct 12 14:23:28 CST 2018]; root of context hierarchy14:23:30.127 [main] INFOo.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase 014:23:30.128 [main] INFOo.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown14:23:30.128 [main] INFOo.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'14:23:30.129 [main] INFOo.h.tool.hbm2ddl.SchemaExport - HHH000227: Running hbm2ddl schema export14:23:30.129 [main] INFOo.h.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete14:23:30.132 [main] INFOc.p.spring.cloud.task.TaskDemo - Started TaskDemo in 2.405 seconds (JVM running for 2.771)4. 与Spring Batch集成我们可以将Spring Batch Job作为Task执行,并使用Spring Cloud Task记录Job执行的事件 。要启用此功能,我们需要添加与Boot和Cloud相关的Batch依赖项:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-task-batch</artifactId></dependency>要将作业配置为任务,我们需要在JobConfiguration类中注册Job Bean :
@Beanpublic Job job2() {return jobBuilderFactory.get("job2").start(stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {@Overridepublic RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {LOGGER.info("This job is from PeterWanghao");return RepeatStatus.FINISHED;}}).build()).build();}我们需要使用@EnableBatchProcessing注解来装饰TaskDemo类:
@SpringBootApplication@EnableTask@EnableBatchProcessingpublic class TaskDemo {// ...}该@EnableBatchProcessing注解集成了Spring Batch的功能,并设置批处理作业所需的基本配置 。
现在,如果我们运行应用程序,@ EnableBatchProcessing注释将触发Spring Batch Job执行,Spring Cloud Task将使用springcloud数据库记录所有批处理作业的执行事件 。
运行TaskDemo类,在控制台可看到任务被执行:
14:30:26.003 [main] INFOo.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup14:30:26.008 [main] INFOo.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 014:30:26.047 [main] INFOc.p.spring.cloud.task.TaskListener - Task Startup14:30:26.053 [main] INFOc.p.spring.cloud.task.TaskDemo - Hello World from Spring Cloud Task!14:30:26.054 [main] INFOo.s.b.a.b.JobLauncherCommandLineRunner - Running default command line with: []14:30:26.257 [main] INFOo.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=job1]] launched with the following parameters: [{}]14:30:26.266 [main] INFOo.s.c.t.b.l.TaskBatchExecutionListener - The job execution id 1 was run within the task execution 414:30:26.292 [main] INFOo.s.batch.core.job.SimpleStepHandler - Executing step: [job1step1]14:30:26.312 [main] INFOc.p.s.cloud.task.JobConfiguration - Tasklet has run14:30:26.342 [main] INFOo.s.batch.core.job.SimpleStepHandler - Executing step: [job1step2]14:30:26.353 [main] INFOc.p.s.cloud.task.JobConfiguration - Processing of chunks14:30:26.353 [main] INFOc.p.s.cloud.task.JobConfiguration - Processing of chunks14:30:26.353 [main] INFOc.p.s.cloud.task.JobConfiguration - Processing of chunks14:30:26.354 [main] INFOc.p.s.cloud.task.JobConfiguration - >> -714:30:26.354 [main] INFOc.p.s.cloud.task.JobConfiguration - >> -214:30:26.354 [main] INFOc.p.s.cloud.task.JobConfiguration - >> -314:30:26.359 [main] INFOc.p.s.cloud.task.JobConfiguration - Processing of chunks14:30:26.359 [main] INFOc.p.s.cloud.task.JobConfiguration - Processing of chunks14:30:26.359 [main] INFOc.p.s.cloud.task.JobConfiguration - Processing of chunks14:30:26.359 [main] INFOc.p.s.cloud.task.JobConfiguration - >> -1014:30:26.359 [main] INFOc.p.s.cloud.task.JobConfiguration - >> -514:30:26.359 [main] INFOc.p.s.cloud.task.JobConfiguration - >> -614:30:26.381 [main] INFOo.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED]14:30:26.398 [main] INFOo.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=job2]] launched with the following parameters: [{}]14:30:26.404 [main] INFOo.s.c.t.b.l.TaskBatchExecutionListener - The job execution id 2 was run within the task execution 414:30:26.420 [main] INFOo.s.batch.core.job.SimpleStepHandler - Executing step: [job2step1]14:30:26.428 [main] INFOc.p.s.cloud.task.JobConfiguration - This job is from PeterWanghao14:30:26.441 [main] INFOo.s.b.c.l.support.SimpleJobLauncher - Job: [SimpleJob: [name=job2]] completed with the following parameters: [{}] and the following status: [COMPLETED]14:30:26.442 [main] INFOc.p.spring.cloud.task.TaskListener - End of Task14:30:26.448 [main] INFOo.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@399f45b1: startup date [Fri Oct 12 14:30:23 CST 2018]; root of context hierarchy14:30:26.450 [main] INFOo.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase 014:30:26.450 [main] INFOo.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown14:30:26.451 [main] INFOo.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'14:30:26.451 [main] INFOo.h.tool.hbm2ddl.SchemaExport - HHH000227: Running hbm2ddl schema export14:30:26.451 [main] INFOo.h.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete14:30:26.455 [main] INFOc.p.spring.cloud.task.TaskDemo - Started TaskDemo in 3.746 seconds (JVM running for 4.093)


推荐阅读