我们需要在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)
推荐阅读
- 她救了他,他宠她一生?全新重生甜宠文-这是她第二次成为他的心尖宠
- 日语中的修饰语是什么?日语修饰词是什么意思
- 明朝朱棣母亲是谁?朱棣的亲生母亲到底是谁_1
- 前端开发:Laravel和Vue.JS为什么如此搭?
- 我对于家庭的价值是什么?谈谈你的家庭价值观
- 明朝对官员为什么这么残忍?明朝那些事儿的好处
- 朱元璋送给徐达什么东西害死了他?徐达在的话朱棣会成功吗
- 为什么做的电饭煲蛋糕不蓬松 电饭煲做的蛋糕不蓬松原因是什么
- 曹操为什么没有杀死刘备?谁劝曹操除掉刘备
- 空调扇和空调的区别是什么 空调扇和空调的区别分析