Added ConditionalOnProperty for batch event listeners

Resolves spring-cloud/spring-cloud-task#130
This commit is contained in:
Glenn Renfro
2016-05-02 14:29:54 -04:00
parent 5c0a83dd4f
commit fc3dcffe4b
2 changed files with 108 additions and 9 deletions

View File

@@ -82,17 +82,20 @@ public class BatchEventAutoConfiguration {
@Bean @Bean
@Lazy @Lazy
@ConditionalOnProperty(prefix = "spring.cloud.task.events.job.execution.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public JobExecutionListener jobExecutionEventsListener() { public JobExecutionListener jobExecutionEventsListener() {
return new EventEmittingJobExecutionListener(listenerChannels.jobExecutionEvents()); return new EventEmittingJobExecutionListener(listenerChannels.jobExecutionEvents());
} }
@Bean @Bean
@ConditionalOnProperty(prefix = "spring.cloud.task.events.step.execution.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public StepExecutionListener stepExecutionEventsListener() { public StepExecutionListener stepExecutionEventsListener() {
return new EventEmittingStepExecutionListener(listenerChannels.stepExecutionEvents()); return new EventEmittingStepExecutionListener(listenerChannels.stepExecutionEvents());
} }
@Bean @Bean
@Lazy @Lazy
@ConditionalOnProperty(prefix = "spring.cloud.task.events.chunk.events.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public GatewayProxyFactoryBean chunkEventsListener() { public GatewayProxyFactoryBean chunkEventsListener() {
GatewayProxyFactoryBean factoryBean = GatewayProxyFactoryBean factoryBean =
new GatewayProxyFactoryBean(ChunkListener.class); new GatewayProxyFactoryBean(ChunkListener.class);
@@ -103,21 +106,25 @@ public class BatchEventAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnProperty(prefix = "spring.cloud.task.events.item.read.events.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public ItemReadListener itemReadEventsListener() { public ItemReadListener itemReadEventsListener() {
return new EventEmittingItemReadListener(listenerChannels.itemReadEvents()); return new EventEmittingItemReadListener(listenerChannels.itemReadEvents());
} }
@Bean @Bean
@ConditionalOnProperty(prefix = "spring.cloud.task.events.item.write.events.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public ItemWriteListener itemWriteEventsListener() { public ItemWriteListener itemWriteEventsListener() {
return new EventEmittingItemWriteListener(listenerChannels.itemWriteEvents()); return new EventEmittingItemWriteListener(listenerChannels.itemWriteEvents());
} }
@Bean @Bean
@ConditionalOnProperty(prefix = "spring.cloud.task.events.item.process.events.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public ItemProcessListener itemProcessEventsListener() { public ItemProcessListener itemProcessEventsListener() {
return new EventEmittingItemProcessListener(listenerChannels.itemProcessEvents()); return new EventEmittingItemProcessListener(listenerChannels.itemProcessEvents());
} }
@Bean @Bean
@ConditionalOnProperty(prefix = "spring.cloud.task.events.skip.events.listener", name = "enabled", havingValue = "true", matchIfMissing = true)
public SkipListener skipEventsListener() { public SkipListener skipEventsListener() {
return new EventEmittingSkipListener(listenerChannels.skipEvents()); return new EventEmittingSkipListener(listenerChannels.skipEvents());
} }

View File

@@ -31,11 +31,18 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecution;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent; import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent; import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/** /**
* @author Glenn Renfro. * @author Glenn Renfro.
@@ -46,6 +53,10 @@ public class EventJobExecutionTests {
private static final Long JOB_INSTANCE_ID = 1l; private static final Long JOB_INSTANCE_ID = 1l;
private static final Long JOB_EXECUTION_ID = 2l; private static final Long JOB_EXECUTION_ID = 2l;
private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG"; private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG";
private static final String[] LISTENER_BEAN_NAMES = {BatchEventAutoConfiguration.JOB_EXECUTION_EVENTS_LISTENER,
BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER, BatchEventAutoConfiguration.CHUNK_EVENTS_LISTENER,
BatchEventAutoConfiguration.ITEM_READ_EVENTS_LISTENER, BatchEventAutoConfiguration.ITEM_WRITE_EVENTS_LISTENER,
BatchEventAutoConfiguration.ITEM_PROCESS_EVENTS_LISTENER, BatchEventAutoConfiguration.SKIP_EVENTS_LISTENER};
private JobParameters jobParameters; private JobParameters jobParameters;
private JobInstance jobInstance; private JobInstance jobInstance;
@@ -112,6 +123,87 @@ public class EventJobExecutionTests {
assertEquals("foo stepExecution is not present", "foo", iter.next().getStepName()); assertEquals("foo stepExecution is not present", "foo", iter.next().getStepName());
assertEquals("bar stepExecution is not present", "bar", iter.next().getStepName()); assertEquals("bar stepExecution is not present", "bar", iter.next().getStepName());
assertEquals("baz stepExecution is not present", "baz", iter.next().getStepName()); assertEquals("baz stepExecution is not present", "baz", iter.next().getStepName());
}
@Test
public void testDefaultConfiguration() {
testDisabledConfiguration(null, null);
}
@Test
public void testDisabledJobExecutionListener() {
testDisabledConfiguration("spring.cloud.task.events.job.execution.listener.enabled",
BatchEventAutoConfiguration.JOB_EXECUTION_EVENTS_LISTENER);
}
@Test
public void testDisabledStepExecutionListener() {
testDisabledConfiguration("spring.cloud.task.events.step.execution.listener.enabled",
BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER);
}
@Test
public void testDisabledChunkListener() {
testDisabledConfiguration("spring.cloud.task.events.chunk.events.listener.enabled",
BatchEventAutoConfiguration.CHUNK_EVENTS_LISTENER);
}
@Test
public void testDisabledItemReadListener() {
testDisabledConfiguration("spring.cloud.task.events.item.read.events.listener.enabled",
BatchEventAutoConfiguration.ITEM_READ_EVENTS_LISTENER);
}
@Test
public void testDisabledItemWriteListener() {
testDisabledConfiguration("spring.cloud.task.events.item.write.events.listener.enabled",
BatchEventAutoConfiguration.ITEM_WRITE_EVENTS_LISTENER);
}
@Test
public void testDisabledItemProcessListener() {
testDisabledConfiguration("spring.cloud.task.events.item.process.events.listener.enabled",
BatchEventAutoConfiguration.ITEM_PROCESS_EVENTS_LISTENER);
}
@Test
public void testDisabledSkipEventListener() {
testDisabledConfiguration("spring.cloud.task.events.skip.events.listener.enabled",
BatchEventAutoConfiguration.SKIP_EVENTS_LISTENER);
}
public void testDisabledConfiguration(String property, String disabledListener) {
boolean exceptionThrown = false;
String disabledPropertyArg = (property != null) ? "--" + property + "=false" : "";
ConfigurableApplicationContext applicationContext =
SpringApplication.run(new Object[]{BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class,
EventJobExecutionConfiguration.class,
PropertyPlaceholderAutoConfiguration.class},
new String[]{"--spring.cloud.task.closecontext.enable=false",
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=test",
disabledPropertyArg});
for (String beanName : LISTENER_BEAN_NAMES) {
if (disabledListener != null && disabledListener.equals(beanName)) {
try {
applicationContext.getBean(disabledListener);
}
catch (NoSuchBeanDefinitionException nsbde) {
exceptionThrown = true;
}
assertTrue(String.format("Did not expect %s bean in context", beanName), exceptionThrown);
}
else {
applicationContext.getBean(beanName);
}
}
applicationContext.getBean(BatchEventAutoConfiguration.BatchEventsChannels.class);
}
@Configuration
@EnableTask
public static class EventJobExecutionConfiguration {
} }
} }