OPEN - issue BATCH-385: Merge user attributes in StepContext with ExecutionContext
http://jira.springframework.org/browse/BATCH-385 Refactored existing StepContextAware to StepListener implementations. The BatchResourceFactoryBean is now a ProxyResource with a hint in docos and exception messages about registering with a step.
This commit is contained in:
@@ -17,18 +17,20 @@
|
||||
package org.springframework.batch.execution.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepListener;
|
||||
import org.springframework.batch.core.listener.StepListenerSupport;
|
||||
import org.springframework.batch.core.runtime.JobParametersFactory;
|
||||
import org.springframework.batch.execution.bootstrap.support.DefaultJobParametersFactory;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
import org.springframework.batch.execution.scope.StepContextAware;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.io.FileSystemResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -38,10 +40,10 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Strategy for locating different resources on the file system. For each unique
|
||||
* step, the same file handle will be returned. A unique step is defined as
|
||||
* having the same job identifier and step name. An external file mover (such as
|
||||
* an EAI solution) should rename and move any input files to conform to the
|
||||
* patter defined by the file pattern.<br/>
|
||||
* step execution, the same file handle will be returned. A unique step is
|
||||
* defined as having the same job instance and step name. An external file mover
|
||||
* (such as an EAI solution) should rename and move any input files to conform
|
||||
* to the pattern defined here.<br/>
|
||||
*
|
||||
* If no pattern is passed in, then following default is used:
|
||||
*
|
||||
@@ -61,17 +63,19 @@ import org.springframework.util.StringUtils;
|
||||
* implementation of the Spring Core Resource abstractions, it would need to
|
||||
* start with a double forward slash "//" to resolve to an absolute directory.<br/>
|
||||
*
|
||||
* It doesn't make much sense to use this factory unless it is step scoped, but
|
||||
* note that it is thread safe only if it is step scoped and its mutators are
|
||||
* not used except for configuration.
|
||||
* To use this resource it must be initialised with a {@link StepExecution}.
|
||||
* The best way to do that is to register it as a listener in the step that is
|
||||
* going to need it. It is to enable this usage that the resource implements
|
||||
* {@link StepListener}.
|
||||
*
|
||||
* @author Tomas Slanina
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @see FactoryBean
|
||||
* @see Resource
|
||||
*/
|
||||
public class BatchResourceFactoryBean extends AbstractFactoryBean implements ResourceLoaderAware, StepContextAware {
|
||||
public class StepExecutionProxyResource extends StepListenerSupport implements Resource, ResourceLoaderAware,
|
||||
StepListener {
|
||||
|
||||
private static final String JOB_NAME_PATTERN = "%JOB_NAME%";
|
||||
|
||||
@@ -81,15 +85,107 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
|
||||
private String filePattern = DEFAULT_PATTERN;
|
||||
|
||||
private String jobName = null;
|
||||
|
||||
private String stepName = "";
|
||||
|
||||
private JobParametersFactory jobParametersFactory = new DefaultJobParametersFactory();
|
||||
|
||||
private ResourceLoader resourceLoader = new FileSystemResourceLoader();
|
||||
|
||||
private Properties properties;
|
||||
private Resource delegate;
|
||||
|
||||
/**
|
||||
* @param relativePath
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#createRelative(java.lang.String)
|
||||
*/
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.createRelative(relativePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @see org.springframework.core.io.Resource#exists()
|
||||
*/
|
||||
public boolean exists() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @see org.springframework.core.io.Resource#getDescription()
|
||||
*/
|
||||
public String getDescription() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getDescription();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#getFile()
|
||||
*/
|
||||
public File getFile() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @see org.springframework.core.io.Resource#getFilename()
|
||||
*/
|
||||
public String getFilename() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getFilename();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.InputStreamSource#getInputStream()
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getInputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#getURI()
|
||||
*/
|
||||
public URI getURI() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getURI();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @see org.springframework.core.io.Resource#getURL()
|
||||
*/
|
||||
public URL getURL() throws IOException {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.getURL();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @see org.springframework.core.io.Resource#isOpen()
|
||||
*/
|
||||
public boolean isOpen() {
|
||||
Assert.state(delegate != null, "The delegate resource has not been initialised. "
|
||||
+ "Remember to register this object as a StepListener.");
|
||||
return delegate.isOpen();
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobParametersFactory} used to translate
|
||||
@@ -119,34 +215,6 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect the properties of the enclosing {@link StepExecution} that will
|
||||
* be needed to create a file name.
|
||||
*
|
||||
* @see org.springframework.batch.execution.scope.StepContextAware#setStepScopeContext(org.springframework.core.AttributeAccessor)
|
||||
*/
|
||||
public void setStepContext(StepContext context) {
|
||||
Assert.state(context.getStepExecution() != null, "The StepContext does not have an execution.");
|
||||
StepExecution execution = context.getStepExecution();
|
||||
stepName = execution.getStepName();
|
||||
jobName = execution.getJobExecution().getJobInstance().getJobName();
|
||||
properties = jobParametersFactory.getProperties(execution.getJobExecution().getJobInstance().getJobParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Resource representing the file defined by the file pattern.
|
||||
*
|
||||
* @see FactoryBean#getObject()
|
||||
* @return a resource representing the file on the file system.
|
||||
*/
|
||||
protected Object createInstance() {
|
||||
return resourceLoader.getResource(createFileName());
|
||||
}
|
||||
|
||||
public Class getObjectType() {
|
||||
return Resource.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* helper method for <code>createFileName()</code>
|
||||
*/
|
||||
@@ -168,8 +236,11 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
*
|
||||
* Deliberate package access, so that the method can be accessed by unit
|
||||
* tests
|
||||
* @param jobName
|
||||
* @param stepName
|
||||
* @param properties
|
||||
*/
|
||||
private String createFileName() {
|
||||
private String createFileName(String jobName, String stepName, Properties properties) {
|
||||
Assert.notNull(filePattern, "filename pattern is null");
|
||||
|
||||
String fileName = filePattern;
|
||||
@@ -192,4 +263,18 @@ public class BatchResourceFactoryBean extends AbstractFactoryBean implements Res
|
||||
this.filePattern = replacePattern(filePattern, "\\", File.separator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect the properties of the enclosing {@link StepExecution} that will
|
||||
* be needed to create a file name.
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.StepListener#beforeStep(org.springframework.batch.core.domain.StepExecution)
|
||||
*/
|
||||
public void beforeStep(StepExecution execution) {
|
||||
String stepName = execution.getStepName();
|
||||
String jobName = execution.getJobExecution().getJobInstance().getJobName();
|
||||
Properties properties = jobParametersFactory.getProperties(execution.getJobExecution().getJobInstance()
|
||||
.getJobParameters());
|
||||
delegate = resourceLoader.getResource(createFileName(jobName, stepName, properties));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -268,8 +268,8 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean {
|
||||
// Execute step level listeners *after* the execution context is
|
||||
// fixed in the step. E.g. ItemStream instances need the the same
|
||||
// reference to the ExecutionContext as the step execution.
|
||||
listener.open(stepExecution.getExecutionContext());
|
||||
listener.beforeStep(stepExecution);
|
||||
listener.open(stepExecution.getExecutionContext());
|
||||
|
||||
status = stepOperations.iterate(new RepeatCallback() {
|
||||
|
||||
|
||||
@@ -26,26 +26,26 @@ import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.JobParametersBuilder;
|
||||
import org.springframework.batch.core.domain.Step;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.execution.job.JobSupport;
|
||||
import org.springframework.batch.execution.scope.SimpleStepContext;
|
||||
import org.springframework.batch.execution.step.StepSupport;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link BatchResourceFactoryBean}
|
||||
* Unit tests for {@link StepExecutionProxyResource}
|
||||
*
|
||||
* @author robert.kasanicky
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
public class StepExecutionProxyResourceTests extends TestCase {
|
||||
|
||||
/**
|
||||
* Object under test
|
||||
*/
|
||||
private BatchResourceFactoryBean resourceFactory = new BatchResourceFactoryBean();
|
||||
private StepExecutionProxyResource resource = new StepExecutionProxyResource();
|
||||
|
||||
private char pathsep = File.separatorChar;
|
||||
|
||||
@@ -53,7 +53,7 @@ public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
|
||||
private JobInstance jobInstance;
|
||||
|
||||
private Step step;
|
||||
private StepExecution stepExecution;
|
||||
|
||||
/**
|
||||
* mock step context
|
||||
@@ -63,10 +63,9 @@ public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
|
||||
jobInstance = new JobInstance(new Long(0), new JobParameters(), new JobSupport("testJob"));
|
||||
JobExecution jobExecution = jobInstance.createJobExecution();
|
||||
step = new StepSupport("bar");
|
||||
resourceFactory.setStepContext(new SimpleStepContext(jobExecution.createStepExecution(step)));
|
||||
|
||||
resourceFactory.afterPropertiesSet();
|
||||
Step step = new StepSupport("bar");
|
||||
stepExecution = jobExecution.createStepExecution(step);
|
||||
resource.beforeStep(stepExecution);
|
||||
|
||||
}
|
||||
|
||||
@@ -77,15 +76,10 @@ public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
doTestPathName("bar.txt", path);
|
||||
}
|
||||
|
||||
public void testObjectType() throws Exception {
|
||||
assertEquals(Resource.class, resourceFactory.getObjectType());
|
||||
}
|
||||
|
||||
public void testNullFilePattern() throws Exception {
|
||||
resourceFactory = new BatchResourceFactoryBean();
|
||||
resourceFactory.setFilePattern(null);
|
||||
resource.setFilePattern(null);
|
||||
try {
|
||||
resourceFactory.getObject();
|
||||
resource.beforeStep(stepExecution);
|
||||
fail("Expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
@@ -94,7 +88,8 @@ public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testNonStandardFilePattern() throws Exception {
|
||||
resourceFactory.setFilePattern("foo/data/%JOB_NAME%/" + "%STEP_NAME%-job");
|
||||
resource.setFilePattern("foo/data/%JOB_NAME%/" + "%STEP_NAME%-job");
|
||||
resource.beforeStep(stepExecution);
|
||||
doTestPathName("bar-job", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
@@ -102,27 +97,24 @@ public class BatchResourceFactoryBeanTests extends TestCase {
|
||||
jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addString("job.key", "spam")
|
||||
.toJobParameters(), new JobSupport("testJob"));
|
||||
JobExecution jobExecution = jobInstance.createJobExecution();
|
||||
step = new StepSupport("bar");
|
||||
resourceFactory.setStepContext(new SimpleStepContext(jobExecution.createStepExecution(step)));
|
||||
resourceFactory.setFilePattern("foo/data/%JOB_NAME%/%job.key%-foo");
|
||||
Step step = new StepSupport("bar");
|
||||
resource.setFilePattern("foo/data/%JOB_NAME%/%job.key%-foo");
|
||||
resource.beforeStep(jobExecution.createStepExecution(step));
|
||||
doTestPathName("spam-foo", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
public void testResoureLoaderAware() throws Exception {
|
||||
resourceFactory = new BatchResourceFactoryBean();
|
||||
resourceFactory.setSingleton(false);
|
||||
resourceFactory.setResourceLoader(new DefaultResourceLoader() {
|
||||
resource = new StepExecutionProxyResource();
|
||||
resource.setResourceLoader(new DefaultResourceLoader() {
|
||||
public Resource getResource(String location) {
|
||||
return new ByteArrayResource("foo".getBytes());
|
||||
}
|
||||
});
|
||||
Resource resource = (Resource) resourceFactory.getObject();
|
||||
assertNotNull(resource);
|
||||
resource.beforeStep(stepExecution);
|
||||
assertTrue(resource.exists());
|
||||
}
|
||||
|
||||
private void doTestPathName(String filename, String path) throws Exception, IOException {
|
||||
Resource resource = (Resource) resourceFactory.getObject();
|
||||
String returnedPath = resource.getFile().getAbsolutePath();
|
||||
String absolutePath = new File(path + jobInstance.getJobName() + pathsep + filename).getAbsolutePath();
|
||||
assertEquals(absolutePath, returnedPath);
|
||||
@@ -30,6 +30,7 @@ import org.springframework.batch.core.domain.JobInterruptedException;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepContribution;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepListener;
|
||||
import org.springframework.batch.core.listener.StepListenerSupport;
|
||||
import org.springframework.batch.execution.job.JobSupport;
|
||||
import org.springframework.batch.execution.repository.SimpleJobRepository;
|
||||
@@ -387,6 +388,20 @@ public class ItemOrientedStepTests extends TestCase {
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
|
||||
public void testListenerCalledBeforeStreamOpened() throws Exception {
|
||||
itemOrientedStep.setListeners(new BatchListener[] {new MockRestartableItemReader() {
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
list.add("foo");
|
||||
}
|
||||
public void open(ExecutionContext executionContext) throws StreamException {
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
}});
|
||||
StepExecution stepExecution = new StepExecution(itemOrientedStep, new JobExecution(jobInstance));
|
||||
itemOrientedStep.execute(stepExecution);
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
public void testDirectlyInjectedListenerOnError() throws Exception {
|
||||
itemOrientedStep.setListeners(new Object[] {new StepListenerSupport() {
|
||||
public ExitStatus onErrorInStep(Throwable e) {
|
||||
@@ -634,7 +649,7 @@ public class ItemOrientedStepTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private class MockRestartableItemReader extends ItemStreamSupport implements ItemReader {
|
||||
private class MockRestartableItemReader extends ItemStreamSupport implements ItemReader, StepListener {
|
||||
|
||||
private boolean getExecutionAttributesCalled = false;
|
||||
|
||||
@@ -663,18 +678,23 @@ public class ItemOrientedStepTests extends TestCase {
|
||||
return restoreFromCalled;
|
||||
}
|
||||
|
||||
public void open(ExecutionContext executionContext) throws StreamException {
|
||||
}
|
||||
|
||||
public void close(ExecutionContext executionContext) throws StreamException {
|
||||
}
|
||||
|
||||
public void mark() throws MarkFailedException {
|
||||
}
|
||||
|
||||
public void reset() throws ResetFailedException {
|
||||
}
|
||||
|
||||
public ExitStatus afterStep() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
}
|
||||
|
||||
public ExitStatus onErrorInStep(Throwable e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,12 +9,13 @@ import java.util.List;
|
||||
import org.apache.commons.lang.SerializationUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
import org.springframework.batch.execution.scope.StepContextAware;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepListener;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.KeyedItemReader;
|
||||
import org.springframework.batch.item.exception.StreamException;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.sample.item.writer.StagingItemWriter;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
@@ -24,14 +25,14 @@ import org.springframework.jdbc.support.lob.LobHandler;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class StagingItemReader extends JdbcDaoSupport implements ItemStream, KeyedItemReader, StepContextAware {
|
||||
public class StagingItemReader extends JdbcDaoSupport implements ItemStream, KeyedItemReader, StepListener {
|
||||
|
||||
// Key for buffer in transaction synchronization manager
|
||||
private static final String BUFFER_KEY = StagingItemReader.class.getName() + ".BUFFER";
|
||||
|
||||
private static Log logger = LogFactory.getLog(StagingItemReader.class);
|
||||
|
||||
private StepContext stepContext;
|
||||
private StepExecution stepExecution;
|
||||
|
||||
private LobHandler lobHandler = new DefaultLobHandler();
|
||||
|
||||
@@ -76,15 +77,6 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for injection of the step context.
|
||||
*
|
||||
* @param stepContext the stepContext to set
|
||||
*/
|
||||
public void setStepContext(StepContext stepContext) {
|
||||
this.stepContext = stepContext;
|
||||
}
|
||||
|
||||
private List retrieveKeys() {
|
||||
|
||||
synchronized (lock) {
|
||||
@@ -93,7 +85,7 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key
|
||||
|
||||
"SELECT ID FROM BATCH_STAGING WHERE JOB_ID=? AND PROCESSED=? ORDER BY ID",
|
||||
|
||||
new Object[] { stepContext.getStepExecution().getJobExecution().getJobId(), StagingItemWriter.NEW },
|
||||
new Object[] { stepExecution.getJobExecution().getJobId(), StagingItemWriter.NEW },
|
||||
|
||||
new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
@@ -234,4 +226,25 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key
|
||||
public void update(ExecutionContext executionContext) {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.domain.StepListener#afterStep()
|
||||
*/
|
||||
public ExitStatus afterStep() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.domain.StepListener#beforeStep(org.springframework.batch.core.domain.StepExecution)
|
||||
*/
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
this.stepExecution = stepExecution;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.domain.StepListener#onErrorInStep(java.lang.Throwable)
|
||||
*/
|
||||
public ExitStatus onErrorInStep(Throwable e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,11 +5,12 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.commons.lang.SerializationUtils;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
import org.springframework.batch.execution.scope.StepContextAware;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepListener;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.exception.ClearFailedException;
|
||||
import org.springframework.batch.item.exception.FlushFailedException;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.support.JdbcDaoSupport;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
@@ -18,7 +19,7 @@ import org.springframework.jdbc.support.lob.LobHandler;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class StagingItemWriter extends JdbcDaoSupport implements StepContextAware, ItemWriter {
|
||||
public class StagingItemWriter extends JdbcDaoSupport implements StepListener, ItemWriter {
|
||||
|
||||
public static final String NEW = "N";
|
||||
|
||||
@@ -28,7 +29,7 @@ public class StagingItemWriter extends JdbcDaoSupport implements StepContextAwar
|
||||
|
||||
private DataFieldMaxValueIncrementer incrementer;
|
||||
|
||||
private StepContext stepContext;
|
||||
private StepExecution stepExecution;
|
||||
|
||||
private LobHandler lobHandler = new DefaultLobHandler();
|
||||
|
||||
@@ -52,15 +53,6 @@ public class StagingItemWriter extends JdbcDaoSupport implements StepContextAwar
|
||||
+ ClassUtils.getShortName(StagingItemWriter.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for injection of the step context.
|
||||
*
|
||||
* @param stepContext the stepContext to set
|
||||
*/
|
||||
public void setStepContext(StepContext stepContext) {
|
||||
this.stepContext = stepContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the key generator for the staging table.
|
||||
*
|
||||
@@ -77,7 +69,7 @@ public class StagingItemWriter extends JdbcDaoSupport implements StepContextAwar
|
||||
*/
|
||||
public void write(Object data) {
|
||||
final long id = incrementer.nextLongValue();
|
||||
final long jobId = stepContext.getStepExecution().getJobExecution().getJobId().longValue();
|
||||
final long jobId = stepExecution.getJobExecution().getJobId().longValue();
|
||||
final byte[] blob = SerializationUtils.serialize((Serializable) data);
|
||||
getJdbcTemplate()
|
||||
.update("INSERT into BATCH_STAGING (ID, JOB_ID, VALUE, PROCESSED) values (?,?,?,?)",
|
||||
@@ -101,4 +93,25 @@ public class StagingItemWriter extends JdbcDaoSupport implements StepContextAwar
|
||||
public void flush() throws FlushFailedException {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.domain.StepListener#afterStep()
|
||||
*/
|
||||
public ExitStatus afterStep() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.domain.StepListener#beforeStep(org.springframework.batch.core.domain.StepExecution)
|
||||
*/
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
this.stepExecution = stepExecution;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.domain.StepListener#onErrorInStep(java.lang.Throwable)
|
||||
*/
|
||||
public ExitStatus onErrorInStep(Throwable e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,9 +32,7 @@
|
||||
</property>
|
||||
<property name="itemWriter">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.item.writer.StagingItemWriter"
|
||||
scope="step">
|
||||
<aop:scoped-proxy />
|
||||
class="org.springframework.batch.sample.item.writer.StagingItemWriter">
|
||||
<property name="dataSource"
|
||||
ref="dataSource" />
|
||||
<property name="lobHandler"
|
||||
@@ -60,9 +58,7 @@
|
||||
</property>
|
||||
<property name="itemReader">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.item.reader.StagingItemReader"
|
||||
scope="step">
|
||||
<aop:scoped-proxy />
|
||||
class="org.springframework.batch.sample.item.reader.StagingItemReader">
|
||||
<property name="lobHandler"
|
||||
ref="lobHandler" />
|
||||
<property name="dataSource"
|
||||
@@ -91,8 +87,7 @@
|
||||
</bean>
|
||||
|
||||
<bean id="fileInputTemplate" parent="testInputTemplate"
|
||||
autowire-candidate="false" scope="step">
|
||||
<aop:scoped-proxy />
|
||||
autowire-candidate="false">
|
||||
</bean>
|
||||
|
||||
<bean id="fixedFileDescriptor"
|
||||
|
||||
@@ -4,12 +4,7 @@ import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.execution.scope.SimpleStepContext;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
import org.springframework.batch.execution.scope.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.context.RepeatContextSupport;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
import org.springframework.batch.sample.item.writer.StagingItemWriter;
|
||||
import org.springframework.batch.sample.tasklet.JobSupport;
|
||||
import org.springframework.batch.sample.tasklet.StepSupport;
|
||||
@@ -20,7 +15,7 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
|
||||
private StagingItemWriter writer;
|
||||
|
||||
private StagingItemReader provider;
|
||||
private StagingItemReader reader;
|
||||
|
||||
private Long jobId = new Long(11);
|
||||
|
||||
@@ -28,8 +23,8 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public void setProvider(StagingItemReader provider) {
|
||||
this.provider = provider;
|
||||
public void setProvider(StagingItemReader reader) {
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
@@ -37,12 +32,14 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
"staging-test-context.xml") };
|
||||
}
|
||||
|
||||
protected void prepareTestInstance() throws Exception {
|
||||
StepContext stepScopeContext = new SimpleStepContext(new StepExecution(new StepSupport("stepName"),
|
||||
new JobExecution(new JobInstance(jobId, new JobParameters(), new JobSupport("testJob")))));
|
||||
StepSynchronizationManager.register(stepScopeContext);
|
||||
RepeatSynchronizationManager.register(new RepeatContextSupport(null));
|
||||
super.prepareTestInstance();
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpBeforeTransaction()
|
||||
*/
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
StepExecution stepExecution = new StepExecution(new StepSupport("stepName"),
|
||||
new JobExecution(new JobInstance(jobId, new JobParameters(), new JobSupport("testJob"))));
|
||||
reader.beforeStep(stepExecution);
|
||||
writer.beforeStep(stepExecution);
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
@@ -50,11 +47,11 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
writer.write("BAR");
|
||||
writer.write("SPAM");
|
||||
writer.write("BUCKET");
|
||||
provider.open(new ExecutionContext());
|
||||
reader.open(new ExecutionContext());
|
||||
}
|
||||
|
||||
protected void onTearDownAfterTransaction() throws Exception {
|
||||
provider.close(null);
|
||||
reader.close(null);
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STAGING");
|
||||
}
|
||||
|
||||
@@ -66,7 +63,7 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemWriter.NEW, before);
|
||||
|
||||
Object item = provider.read();
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
@@ -89,7 +86,7 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
|
||||
public void testProviderRollsBackMultipleTimes() throws Exception {
|
||||
|
||||
provider.mark();
|
||||
reader.mark();
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
@@ -98,34 +95,34 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
new Object[] { jobId, StagingItemWriter.NEW });
|
||||
assertEquals(4, count);
|
||||
|
||||
Object item = provider.read();
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
item = provider.read();
|
||||
item = reader.read();
|
||||
assertEquals("BAR", item);
|
||||
|
||||
provider.reset();
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
item = provider.read();
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
item = provider.read();
|
||||
item = reader.read();
|
||||
assertEquals("BAR", item);
|
||||
item = provider.read();
|
||||
item = reader.read();
|
||||
assertEquals("SPAM", item);
|
||||
|
||||
provider.reset();
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
item = provider.read();
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
}
|
||||
|
||||
public void testProviderRollsBackProcessIndicator() throws Exception {
|
||||
|
||||
provider.mark();
|
||||
reader.mark();
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
@@ -138,10 +135,10 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemWriter.NEW, before);
|
||||
|
||||
Object item = provider.read();
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
provider.reset();
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
@@ -151,7 +148,7 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemWriter.NEW, after);
|
||||
|
||||
item = provider.read();
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.item.writer;
|
||||
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.sample.tasklet.JobSupport;
|
||||
import org.springframework.batch.sample.tasklet.StepSupport;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class StagingItemWriterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private StagingItemWriter writer;
|
||||
|
||||
public void setWriter(StagingItemWriter processor) {
|
||||
this.writer = processor;
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(StagingItemWriter.class,
|
||||
"staging-test-context.xml") };
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpBeforeTransaction()
|
||||
*/
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
StepExecution stepExecution = new StepExecution(new StepSupport("stepName"),
|
||||
new JobExecution(new JobInstance(new Long(12L), new JobParameters(), new JobSupport("testJob"))));
|
||||
writer.beforeStep(stepExecution);
|
||||
}
|
||||
|
||||
public void testProcessInsertsNewItem() throws Exception {
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
|
||||
writer.write("FOO");
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
|
||||
assertEquals(before + 1, after);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,8 +12,7 @@
|
||||
class="org.springframework.batch.execution.scope.StepScope" />
|
||||
|
||||
<bean id="processor"
|
||||
class="org.springframework.batch.sample.item.writer.StagingItemWriter"
|
||||
scope="step">
|
||||
class="org.springframework.batch.sample.item.writer.StagingItemWriter">
|
||||
<property name="incrementer">
|
||||
<bean id="jobIncrementer" parent="incrementerParent">
|
||||
<property name="incrementerName"
|
||||
@@ -24,8 +23,7 @@
|
||||
</bean>
|
||||
|
||||
<bean id="provider"
|
||||
class="org.springframework.batch.sample.item.reader.StagingItemReader"
|
||||
scope="step">
|
||||
class="org.springframework.batch.sample.item.reader.StagingItemReader">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user