Use lobHandler in Staging*
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package org.springframework.batch.sample.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
|
||||
public class Game {
|
||||
public class Game implements Serializable {
|
||||
|
||||
private String id;
|
||||
private int year;
|
||||
|
||||
@@ -40,6 +40,14 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
private volatile Iterator keys;
|
||||
|
||||
/**
|
||||
* Public setter for the {@link LobHandler}.
|
||||
* @param lobHandler the {@link LobHandler} to set (defaults to {@link DefaultLobHandler}).
|
||||
*/
|
||||
public void setLobHandler(LobHandler lobHandler) {
|
||||
this.lobHandler = lobHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Types;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.commons.lang.SerializationUtils;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
@@ -9,21 +10,37 @@ import org.springframework.batch.execution.scope.StepContextAware;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.exception.ClearFailedException;
|
||||
import org.springframework.batch.item.exception.FlushFailedException;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.support.JdbcDaoSupport;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
import org.springframework.jdbc.support.lob.DefaultLobHandler;
|
||||
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 StepContextAware, ItemWriter {
|
||||
|
||||
public static final String NEW = "N";
|
||||
|
||||
public static final String DONE = "Y";
|
||||
|
||||
public static final Object WORKING = "W";
|
||||
|
||||
private DataFieldMaxValueIncrementer incrementer;
|
||||
|
||||
private StepContext stepContext;
|
||||
|
||||
private LobHandler lobHandler = new DefaultLobHandler();
|
||||
|
||||
/**
|
||||
* Public setter for the {@link LobHandler}.
|
||||
* @param lobHandler the {@link LobHandler} to set (defaults to
|
||||
* {@link DefaultLobHandler}).
|
||||
*/
|
||||
public void setLobHandler(LobHandler lobHandler) {
|
||||
this.lobHandler = lobHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check mandatory properties.
|
||||
*
|
||||
@@ -31,19 +48,14 @@ public class StagingItemWriter extends JdbcDaoSupport implements
|
||||
*/
|
||||
protected void initDao() throws Exception {
|
||||
super.initDao();
|
||||
Assert
|
||||
.notNull(
|
||||
incrementer,
|
||||
"DataFieldMaxValueIncrementer is required - set the incrementer property in the "
|
||||
+ ClassUtils
|
||||
.getShortName(StagingItemWriter.class));
|
||||
Assert.notNull(incrementer, "DataFieldMaxValueIncrementer is required - set the incrementer property in the "
|
||||
+ ClassUtils.getShortName(StagingItemWriter.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for injection of the step context.
|
||||
*
|
||||
* @param stepContext
|
||||
* the stepContext to set
|
||||
* @param stepContext the stepContext to set
|
||||
*/
|
||||
public void setStepContext(StepContext stepContext) {
|
||||
this.stepContext = stepContext;
|
||||
@@ -52,8 +64,7 @@ public class StagingItemWriter extends JdbcDaoSupport implements
|
||||
/**
|
||||
* Setter for the key generator for the staging table.
|
||||
*
|
||||
* @param incrementer
|
||||
* the {@link DataFieldMaxValueIncrementer} to set
|
||||
* @param incrementer the {@link DataFieldMaxValueIncrementer} to set
|
||||
*/
|
||||
public void setIncrementer(DataFieldMaxValueIncrementer incrementer) {
|
||||
this.incrementer = incrementer;
|
||||
@@ -65,14 +76,20 @@ public class StagingItemWriter extends JdbcDaoSupport implements
|
||||
* @see ItemWriter#write(java.lang.Object)
|
||||
*/
|
||||
public void write(Object data) {
|
||||
Long id = new Long(incrementer.nextLongValue());
|
||||
Long jobId = stepContext.getStepExecution().getJobExecution().getJobId();
|
||||
byte[] blob = SerializationUtils.serialize((Serializable) data);
|
||||
final long id = incrementer.nextLongValue();
|
||||
final long jobId = stepContext.getStepExecution().getJobExecution().getJobId().longValue();
|
||||
final byte[] blob = SerializationUtils.serialize((Serializable) data);
|
||||
getJdbcTemplate()
|
||||
.update(
|
||||
"INSERT into BATCH_STAGING (ID, JOB_ID, VALUE, PROCESSED) values (?,?,?,?)",
|
||||
new Object[] { id, jobId, blob, NEW },
|
||||
new int[] { Types.BIGINT, Types.BIGINT, Types.BLOB, Types.CHAR});
|
||||
.update("INSERT into BATCH_STAGING (ID, JOB_ID, VALUE, PROCESSED) values (?,?,?,?)",
|
||||
new PreparedStatementSetter() {
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
ps.setLong(1, id);
|
||||
ps.setLong(2, jobId);
|
||||
lobHandler.getLobCreator().setBlobAsBytes(ps, 3, blob);
|
||||
ps.setString(4, NEW);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public void close() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user