diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CompositeItemWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CompositeItemWriter.java new file mode 100644 index 000000000..686301f5b --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/CompositeItemWriter.java @@ -0,0 +1,29 @@ +package org.springframework.batch.sample.common; + +import org.springframework.batch.item.ClearFailedException; +import org.springframework.batch.item.FlushFailedException; +import org.springframework.batch.item.ItemWriter; + +public class CompositeItemWriter implements ItemWriter { + + ItemWriter itemWriter; + + public CompositeItemWriter(ItemWriter itemWriter) { + this.itemWriter = itemWriter; + } + + public void write(T item) throws Exception { + + //Add business logic here + + itemWriter.write(item); + } + + public void clear() throws ClearFailedException { + itemWriter.clear(); + } + + public void flush() throws FlushFailedException { + itemWriter.flush(); + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/InfiniteLoopReader.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/InfiniteLoopReader.java new file mode 100644 index 000000000..24f47ed7f --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/InfiniteLoopReader.java @@ -0,0 +1,25 @@ +/** + * + */ +package org.springframework.batch.sample.common; + +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.NoWorkFoundException; +import org.springframework.batch.item.ParseException; +import org.springframework.batch.item.UnexpectedInputException; + +/** + * ItemReader implementation that will continually return a new object. It's generally + * useful for testing interruption. + * + * @author Lucas Ward + * + */ +public class InfiniteLoopReader implements ItemReader { + + public Object read() throws Exception, UnexpectedInputException, + NoWorkFoundException, ParseException { + return new Object(); + } + +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/InfiniteLoopWriter.java similarity index 50% rename from spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java rename to spring-batch-samples/src/main/java/org/springframework/batch/sample/common/InfiniteLoopWriter.java index 4fce60ca4..0c2fcc168 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/tasklet/InfiniteLoopTasklet.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/common/InfiniteLoopWriter.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package org.springframework.batch.sample.tasklet; +package org.springframework.batch.sample.common; + +import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.listener.StepExecutionListenerSupport; -import org.springframework.batch.core.step.tasklet.Tasklet; -import org.springframework.batch.repeat.ExitStatus; +import org.springframework.batch.item.ItemWriter; /** * Simple module implementation that will always return true to indicate that @@ -32,39 +32,29 @@ import org.springframework.batch.repeat.ExitStatus; * @author Lucas Ward * */ -public class InfiniteLoopTasklet extends StepExecutionListenerSupport implements Tasklet { +public class InfiniteLoopWriter extends StepExecutionListenerSupport implements + ItemWriter { private StepExecution stepExecution; private int count = 0; - private static final Log logger = LogFactory.getLog(InfiniteLoopTasklet.class); + private static final Log logger = LogFactory + .getLog(InfiniteLoopWriter.class); /** * */ - public InfiniteLoopTasklet() { + public InfiniteLoopWriter() { super(); } - public ExitStatus execute() throws Exception { - while(!stepExecution.isTerminateOnly()) { - try { - Thread.sleep(500); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new RuntimeException("Job interrupted."); - } - stepExecution.setItemCount(++count); - logger.info("Executing infinite loop, at count="+count); + public void write(List items) throws Exception { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException("Job interrupted."); } - stepExecution.setStatus(BatchStatus.STOPPING); - return ExitStatus.FAILED; + stepExecution.setItemCount(++count); + logger.info("Executing infinite loop, at count=" + count); } - - /* (non-Javadoc) - * @see org.springframework.batch.core.listener.StepListenerSupport#beforeStep(org.springframework.batch.core.domain.StepExecution) - */ - public void beforeStep(StepExecution stepExecution) { - this.stepExecution = stepExecution; - } - } diff --git a/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml b/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml index c83683fdd..034aad0b5 100644 --- a/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/adhocLoopJob.xml @@ -12,10 +12,14 @@ - - + + + + + + + class="org.springframework.batch.sample.common.InfiniteLoopWriter" /> diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java new file mode 100644 index 000000000..11c814409 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.sample; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * Functional test for graceful shutdown. A batch container is started in a new thread, + * then it's stopped using {@link JobExecution#stop()}. + * + * @author Lucas Ward + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration() +public class DatabaseShutdownFunctionalTests extends AbstractBatchLauncherTests { + + @Transactional @Test + public void testLaunchJob() throws Exception { + + final JobParameters jobParameters = new JobParameters(); + + JobExecution jobExecution = launcher.run(getJob(), jobParameters); + + Thread.sleep(1000); + + assertEquals(BatchStatus.STARTED, jobExecution.getStatus()); + assertTrue(jobExecution.isRunning()); + + //jobExecution.stop(); + JdbcTemplate jdbcTemplate = (JdbcTemplate)applicationContext.getBean("jdbcTemplate"); + jdbcTemplate.update("UPDATE BATCH_JOB_EXECUTION set STATUS = ?", new Object[]{BatchStatus.STOPPING.toString()}); + + + int count = 0; + while(jobExecution.isRunning() && count <= 10){ + logger.info("Checking for end time in JobExecution: count="+count); + Thread.sleep(100); + count++; + } + + assertFalse("Timed out waiting for job to end.", jobExecution.isRunning()); + assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); + + } + +} diff --git a/spring-batch-samples/src/test/resources/org/springframework/batch/sample/DatabaseShutdownFunctionalTests-context.xml b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/DatabaseShutdownFunctionalTests-context.xml new file mode 100644 index 000000000..93b921438 --- /dev/null +++ b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/DatabaseShutdownFunctionalTests-context.xml @@ -0,0 +1,10 @@ + + + + + + +