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:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user