3838import org .springframework .batch .core .step .tasklet .Tasklet ;
3939import org .springframework .batch .repeat .RepeatStatus ;
4040import org .springframework .beans .factory .FactoryBean ;
41+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
4142import org .springframework .beans .factory .annotation .Autowired ;
4243import org .springframework .boot .SpringApplication ;
4344import org .springframework .boot .autoconfigure .batch .BatchAutoConfiguration ;
6465
6566import static org .assertj .core .api .Assertions .assertThat ;
6667import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
68+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
6769
6870/**
6971 * @author Michael Minella
@@ -151,6 +153,16 @@ private void validateContext() {
151153
152154 }
153155
156+ @ Test
157+ public void testNoListenerIfTaskNotEnabled () {
158+ this .applicationContext = SpringApplication .run (TaskNotEnabledConfiguration .class , ARGS );
159+ assertThat (applicationContext .getBean (Job .class )).isNotNull ();
160+ assertThatThrownBy (() -> applicationContext .getBean (TaskBatchExecutionListenerBeanPostProcessor .class ))
161+ .isInstanceOf (NoSuchBeanDefinitionException .class );
162+ assertThatThrownBy (() -> applicationContext .getBean (TaskBatchExecutionListener .class ))
163+ .isInstanceOf (NoSuchBeanDefinitionException .class );
164+ }
165+
154166 @ Test
155167 public void testMultipleDataSources () {
156168 this .applicationContext = SpringApplication
@@ -297,18 +309,36 @@ public static class JobConfiguration {
297309 @ Bean
298310 public Job job () {
299311 return this .jobBuilderFactory .get ("job" )
300- .start (this .stepBuilderFactory .get ("step1" ).tasklet (new Tasklet () {
301- @ Override
302- public RepeatStatus execute (StepContribution contribution ,
303- ChunkContext chunkContext ) throws Exception {
304- System .out .println ("Executed" );
305- return RepeatStatus .FINISHED ;
306- }
312+ .start (this .stepBuilderFactory .get ("step1" ).tasklet ((contribution , chunkContext ) -> {
313+ System .out .println ("Executed" );
314+ return RepeatStatus .FINISHED ;
307315 }).build ()).build ();
308316 }
309317
310318 }
311319
320+ @ EnableBatchProcessing
321+ @ TaskBatchTest
322+ @ Import (EmbeddedDataSourceConfiguration .class )
323+ public static class TaskNotEnabledConfiguration {
324+
325+ @ Autowired
326+ private JobBuilderFactory jobBuilderFactory ;
327+
328+ @ Autowired
329+ private StepBuilderFactory stepBuilderFactory ;
330+
331+ @ Bean
332+ public Job job () {
333+ return this .jobBuilderFactory .get ("job" )
334+ .start (this .stepBuilderFactory .get ("step1" ).tasklet ((contribution , chunkContext ) -> {
335+ System .out .println ("Executed" );
336+ return RepeatStatus .FINISHED ;
337+ }).build ()).build ();
338+ }
339+
340+ }
341+
312342 @ EnableBatchProcessing
313343 @ TaskBatchTest
314344 @ EnableTask
@@ -325,18 +355,12 @@ public static class JobFactoryBeanConfiguration {
325355 public FactoryBean <Job > job () {
326356 return new FactoryBean <Job >() {
327357 @ Override
328- public Job getObject () throws Exception {
358+ public Job getObject () {
329359 return JobFactoryBeanConfiguration .this .jobBuilderFactory .get ("job" )
330360 .start (JobFactoryBeanConfiguration .this .stepBuilderFactory
331- .get ("step1" ).tasklet (new Tasklet () {
332- @ Override
333- public RepeatStatus execute (
334- StepContribution contribution ,
335- ChunkContext chunkContext )
336- throws Exception {
337- System .out .println ("Executed" );
338- return RepeatStatus .FINISHED ;
339- }
361+ .get ("step1" ).tasklet ((contribution , chunkContext ) -> {
362+ System .out .println ("Executed" );
363+ return RepeatStatus .FINISHED ;
340364 }).build ())
341365 .build ();
342366 }
@@ -417,26 +441,18 @@ public static class MultipleJobConfiguration {
417441 @ Bean
418442 public Job job1 () {
419443 return this .jobBuilderFactory .get ("job1" ).start (
420- this .stepBuilderFactory .get ("job1step1" ).tasklet (new Tasklet () {
421- @ Override
422- public RepeatStatus execute (StepContribution contribution ,
423- ChunkContext chunkContext ) throws Exception {
424- System .out .println ("Executed job1" );
425- return RepeatStatus .FINISHED ;
426- }
444+ this .stepBuilderFactory .get ("job1step1" ).tasklet ((contribution , chunkContext ) -> {
445+ System .out .println ("Executed job1" );
446+ return RepeatStatus .FINISHED ;
427447 }).build ()).build ();
428448 }
429449
430450 @ Bean
431451 public Job job2 () {
432452 return this .jobBuilderFactory .get ("job2" ).start (
433- this .stepBuilderFactory .get ("job2step1" ).tasklet (new Tasklet () {
434- @ Override
435- public RepeatStatus execute (StepContribution contribution ,
436- ChunkContext chunkContext ) throws Exception {
437- System .out .println ("Executed job2" );
438- return RepeatStatus .FINISHED ;
439- }
453+ this .stepBuilderFactory .get ("job2step1" ).tasklet ((contribution , chunkContext ) -> {
454+ System .out .println ("Executed job2" );
455+ return RepeatStatus .FINISHED ;
440456 }).build ()).build ();
441457 }
442458
0 commit comments