Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No TaskBatchExecutionListener without EnableTask #786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import org.springframework.cloud.task.batch.listener.TaskBatchExecutionListener;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
Expand All @@ -39,7 +40,7 @@
* @author Michael Minella
*/
@Configuration
@ConditionalOnBean({ Job.class })
@ConditionalOnBean({Job.class, TaskLifecycleListener.class})
@ConditionalOnProperty(
name = { "spring.cloud.task.batch.listener.enable",
"spring.cloud.task.batch.listener.enabled" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,16 +28,14 @@
import org.junit.jupiter.api.Test;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.SimpleJob;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
Expand All @@ -64,6 +62,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author Michael Minella
Expand Down Expand Up @@ -151,6 +150,16 @@ private void validateContext() {

}

@Test
public void testNoListenerIfTaskNotEnabled() {
this.applicationContext = SpringApplication.run(TaskNotEnabledConfiguration.class, ARGS);
assertThat(applicationContext.getBean(Job.class)).isNotNull();
assertThatThrownBy(() -> applicationContext.getBean(TaskBatchExecutionListenerBeanPostProcessor.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
assertThatThrownBy(() -> applicationContext.getBean(TaskBatchExecutionListener.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}

@Test
public void testMultipleDataSources() {
this.applicationContext = SpringApplication
Expand Down Expand Up @@ -297,18 +306,36 @@ public static class JobConfiguration {
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}).build()).build();
}

}

@EnableBatchProcessing
@TaskBatchTest
@Import(EmbeddedDataSourceConfiguration.class)
public static class TaskNotEnabledConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}).build()).build();
}

}

@EnableBatchProcessing
@TaskBatchTest
@EnableTask
Expand All @@ -325,18 +352,12 @@ public static class JobFactoryBeanConfiguration {
public FactoryBean<Job> job() {
return new FactoryBean<Job>() {
@Override
public Job getObject() throws Exception {
public Job getObject() {
return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
.start(JobFactoryBeanConfiguration.this.stepBuilderFactory
.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(
StepContribution contribution,
ChunkContext chunkContext)
throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
.get("step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}).build())
.build();
}
Expand Down Expand Up @@ -370,13 +391,9 @@ public static class JobConfigurationMultipleDataSources {
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
.start(this.stepBuilderFactory.get("step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}).build()).build();
}

Expand Down Expand Up @@ -422,26 +439,18 @@ public static class MultipleJobConfiguration {
@Bean
public Job job1() {
return this.jobBuilderFactory.get("job1").start(
this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed job1");
return RepeatStatus.FINISHED;
}
this.stepBuilderFactory.get("job1step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed job1");
return RepeatStatus.FINISHED;
}).build()).build();
}

@Bean
public Job job2() {
return this.jobBuilderFactory.get("job2").start(
this.stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed job2");
return RepeatStatus.FINISHED;
}
this.stepBuilderFactory.get("job2step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed job2");
return RepeatStatus.FINISHED;
}).build()).build();
}

Expand Down