Add Ordered interface to all event listeneres

changes requested by cppwfs

Fix Constructors duplication

Add @author

Some cleanup on chunk listener

Updated chunk listener to send message before and after chunk.
Fixed Links for JavaDocs to prevent failure on javadoc build.
Updated tests to support new chunk messages.
This commit is contained in:
Ali Shahbour
2017-03-06 17:06:51 +02:00
committed by Glenn Renfro
parent fc708ac775
commit 450de51d0c
13 changed files with 382 additions and 22 deletions

View File

@@ -27,8 +27,11 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import org.springframework.core.Ordered;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.messaging.Message;
@@ -36,6 +39,7 @@ import static org.junit.Assert.assertEquals;
/**
* @author Glenn Renfro
* @author Ali Shahbour
*/
public class EventListenerTests {
@@ -47,7 +51,7 @@ public class EventListenerTests {
private EventEmittingItemWriteListener eventEmittingItemWriteListener;
private EventEmittingJobExecutionListener eventEmittingJobExecutionListener;
private EventEmittingStepExecutionListener eventEmittingStepExecutionListener;
private EventEmittingChunkListener eventEmittingChunkListener;
@Before
public void beforeTests() {
@@ -58,8 +62,21 @@ public class EventListenerTests {
eventEmittingItemWriteListener = new EventEmittingItemWriteListener(queueChannel);
eventEmittingJobExecutionListener = new EventEmittingJobExecutionListener(queueChannel);
eventEmittingStepExecutionListener = new EventEmittingStepExecutionListener(queueChannel);
eventEmittingChunkListener = new EventEmittingChunkListener(queueChannel,0);
}
@Test
public void testEventListenerOrderProperty() {
assertEquals(eventEmittingSkipListener.getOrder(),Ordered.LOWEST_PRECEDENCE);
assertEquals(eventEmittingItemProcessListener.getOrder(), Ordered.LOWEST_PRECEDENCE);
assertEquals(eventEmittingItemReadListener.getOrder(), Ordered.LOWEST_PRECEDENCE);
assertEquals(eventEmittingItemWriteListener.getOrder(), Ordered.LOWEST_PRECEDENCE);
assertEquals(eventEmittingJobExecutionListener.getOrder(), Ordered.LOWEST_PRECEDENCE);
assertEquals(eventEmittingStepExecutionListener.getOrder(), Ordered.LOWEST_PRECEDENCE);
assertEquals(eventEmittingChunkListener.getOrder(),0);
}
@Test
public void testItemProcessListenerOnProcessorError() {
RuntimeException exeption = new RuntimeException("Test Exception");
@@ -219,6 +236,33 @@ public class EventListenerTests {
stepExecutionEvent.getStepName());
}
@Test
public void EventEmittingChunkExecutionListenerBeforeChunk() {
final String CHUNK_MESSAGE = "Before Chunk Processing";
ChunkContext chunkContext = getChunkContext();
eventEmittingChunkListener.beforeChunk(chunkContext);
assertEquals(1,queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals(CHUNK_MESSAGE,msg.getPayload());
}
@Test
public void EventEmittingChunkExecutionListenerAfterChunk() {
final String CHUNK_MESSAGE = "After Chunk Processing";
ChunkContext chunkContext = getChunkContext();
eventEmittingChunkListener.afterChunk(chunkContext);
assertEquals(1,queueChannel.getQueueSize());
Message msg = queueChannel.receive();
assertEquals(CHUNK_MESSAGE,msg.getPayload());
}
@Test
public void EventEmittingChunkExecutionListenerAfterChunkError() {
ChunkContext chunkContext = getChunkContext();
eventEmittingChunkListener.afterChunkError(chunkContext);
assertEquals(0,queueChannel.getQueueSize());
}
private JobExecution getJobExecution() {
final String JOB_NAME = UUID.randomUUID().toString();
JobInstance jobInstance = new JobInstance(1L, JOB_NAME);
@@ -232,5 +276,12 @@ public class EventListenerTests {
testList.add("foo");
return testList;
}
private ChunkContext getChunkContext() {
JobExecution jobExecution = getJobExecution();
StepExecution stepExecution = new StepExecution("STEP1",jobExecution);
StepContext stepContext = new StepContext(stepExecution);
ChunkContext chunkContext = new ChunkContext(stepContext);
return chunkContext;
}
}

View File

@@ -43,6 +43,7 @@ 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 org.springframework.core.Ordered;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -51,6 +52,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Glenn Renfro.
* @author Ali Shahbour
*/
public class JobExecutionEventTests {
@@ -270,6 +272,29 @@ public class JobExecutionEventTests {
assertEquals(BatchStatus.FAILED, jobExecutionEvent.getStatus());
}
@Test
public void testOrderConfiguration() {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(new Object[]{BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class,
EventJobExecutionConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
TestSupportBinderAutoConfiguration.class},
new String[]{"--spring.cloud.task.closecontext_enable=false",
"--spring.main.web-environment=false",
"--spring.cloud.task.batch.events.chunk-order=5",
"--spring.cloud.task.batch.events.item-process-order=5",
"--spring.cloud.task.batch.events.item-read-order=5",
"--spring.cloud.task.batch.events.item-write-order=5",
"--spring.cloud.task.batch.events.job-execution-order=5",
"--spring.cloud.task.batch.events.skip-order=5",
"--spring.cloud.task.batch.events.step-execution-order=5"
});
for (String beanName : LISTENER_BEAN_NAMES) {
Ordered ordered = (Ordered)applicationContext.getBean(beanName);
assertEquals("Expected order value of 5 for " + beanName,ordered.getOrder(),5);
}
}
public void testDisabledConfiguration(String property, String disabledListener) {
boolean exceptionThrown = false;
String disabledPropertyArg = (property != null) ? "--" + property + "=false" : "";