IN PROGRESS - issue BATCH-453: Killed batches cannot be restarted
http://jira.springframework.org/browse/BATCH-453 Added a new sample job: DatabaseShutdownFunctionalTests that uses the database to detect a stopped job.
This commit is contained in:
@@ -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<T> implements ItemWriter<T> {
|
||||
|
||||
ItemWriter<T> itemWriter;
|
||||
|
||||
public CompositeItemWriter(ItemWriter<T> 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();
|
||||
}
|
||||
}
|
||||
@@ -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<Object> {
|
||||
|
||||
public Object read() throws Exception, UnexpectedInputException,
|
||||
NoWorkFoundException, ParseException {
|
||||
return new Object();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Object> {
|
||||
|
||||
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<? extends Object> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,10 +12,14 @@
|
||||
|
||||
<bean id="loopJob" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<bean id="step1" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean id="step1" parent="simpleStep">
|
||||
<property name="commitInterval" value="3" />
|
||||
<property name="itemReader">
|
||||
<bean class="org.springframework.batch.sample.common.InfiniteLoopReader" />
|
||||
</property>
|
||||
<property name="itemWriter">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.tasklet.InfiniteLoopTasklet" />
|
||||
class="org.springframework.batch.sample.common.InfiniteLoopWriter" />
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<import resource="classpath:/jobs/infiniteLoopJob.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user