IN PROGRESS - issue BATCH-7: Remove transaction synchronization and state management from input/output sources (formerly buffering)

http://jira.springframework.org/browse/BATCH-7

Added ItemStream as base interface for ItemReader/Writer.
This commit is contained in:
dsyer
2008-01-30 18:42:48 +00:00
parent 11708012b5
commit bebab9c8d2
42 changed files with 340 additions and 235 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.batch.sample.dao;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.sample.domain.CustomerCredit;
import org.springframework.beans.factory.DisposableBean;
@@ -56,17 +55,13 @@ public class FlatFileCustomerCreditWriter implements CustomerCreditDao,
this.outputSource = outputSource;
}
public void open() {
if (outputSource instanceof ResourceLifecycle) {
((ResourceLifecycle) outputSource).open();
}
public void open() throws Exception {
outputSource.open();
opened = true;
}
public void close() {
if (outputSource instanceof ResourceLifecycle) {
((ResourceLifecycle) outputSource).close();
}
public void close() throws Exception {
outputSource.close();
}
/*

View File

@@ -1,6 +1,10 @@
package org.springframework.batch.sample.dao;
import java.util.Properties;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.sample.domain.Game;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.Assert;
@@ -34,7 +38,36 @@ public class JdbcGameDao extends JdbcDaoSupport implements ItemWriter {
this.getJdbcTemplate().update(INSERT_GAME, args);
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -1,6 +1,10 @@
package org.springframework.batch.sample.dao;
import java.util.Properties;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.sample.domain.PlayerSummary;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.Assert;
@@ -28,7 +32,36 @@ public class JdbcPlayerSummaryDao extends JdbcDaoSupport implements ItemWriter {
getJdbcTemplate().update(INSERT_SUMMARY, args);
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -5,6 +5,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.logging.Log;
@@ -12,7 +13,8 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.batch.execution.scope.StepContext;
import org.springframework.batch.execution.scope.StepContextAware;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
import org.springframework.batch.sample.item.writer.StagingItemWriter;
import org.springframework.beans.factory.DisposableBean;
@@ -26,7 +28,7 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
public class StagingItemReader extends JdbcDaoSupport implements KeyedItemReader, ResourceLifecycle, DisposableBean,
public class StagingItemReader extends JdbcDaoSupport implements KeyedItemReader, DisposableBean,
StepContextAware {
// Key for buffer in transaction synchronization manager
@@ -250,5 +252,21 @@ public class StagingItemReader extends JdbcDaoSupport implements KeyedItemReader
return "list=" + list + "; iter.hasNext()=" + iter.hasNext();
}
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -3,6 +3,7 @@ package org.springframework.batch.sample.item.writer;
import java.math.BigDecimal;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.CustomerCreditDao;
import org.springframework.batch.sample.domain.CustomerCredit;
@@ -11,7 +12,7 @@ import org.springframework.batch.sample.domain.CustomerCredit;
*
* @author Robert Kasanicky
*/
public class CustomerCreditIncreaseWriter implements ItemWriter {
public class CustomerCreditIncreaseWriter extends AbstractItemWriter implements ItemWriter {
public static final BigDecimal FIXED_AMOUNT = new BigDecimal(1000);
@@ -34,6 +35,9 @@ public class CustomerCreditIncreaseWriter implements ItemWriter {
customerCreditDao.writeCredit(customerCredit);
}
public void open() throws Exception {
}
public void close() throws Exception {
}

View File

@@ -17,12 +17,13 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.CustomerCreditDao;
import org.springframework.batch.sample.domain.CustomerCredit;
public class CustomerCreditUpdateWriter implements ItemWriter {
public class CustomerCreditUpdateWriter extends AbstractItemWriter implements ItemWriter {
private double creditFilter = 800;
private CustomerCreditDao dao;

View File

@@ -17,6 +17,7 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.JdbcCustomerDebitWriter;
import org.springframework.batch.sample.domain.CustomerDebit;
import org.springframework.batch.sample.domain.Trade;
@@ -27,7 +28,7 @@ import org.springframework.batch.sample.domain.Trade;
*
* @author Robert Kasanicky
*/
public class CustomerUpdateWriter implements ItemWriter {
public class CustomerUpdateWriter extends AbstractItemWriter implements ItemWriter {
private JdbcCustomerDebitWriter dao;
public void write(Object data) {
@@ -42,9 +43,6 @@ public class CustomerUpdateWriter implements ItemWriter {
this.dao = outputSource;
}
public void close() {
}
public void init() {
}
}

View File

@@ -15,19 +15,16 @@
*/
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
/**
* @author Dave Syer
*
*/
public class DummyItemWriter implements ItemWriter {
public class DummyItemWriter extends AbstractItemWriter {
public void write(Object item) throws Exception {
// NO-OP
}
public void close() throws Exception {
}
}

View File

@@ -18,12 +18,12 @@ package org.springframework.batch.sample.item.writer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.domain.Person;
public class PersonWriter implements ItemWriter {
public class PersonWriter extends AbstractItemWriter {
private static Log log = LogFactory.getLog(PersonWriter.class);
public void write(Object data) {
@@ -36,9 +36,6 @@ public class PersonWriter implements ItemWriter {
log.debug("Processing: " + data);
}
public void close() {
}
public void init() {
}
}

View File

@@ -1,10 +1,10 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.PlayerDao;
import org.springframework.batch.sample.domain.Player;
public class PlayerItemWriter implements ItemWriter {
public class PlayerItemWriter extends AbstractItemWriter {
PlayerDao playerDao;
@@ -16,6 +16,4 @@ public class PlayerItemWriter implements ItemWriter {
this.playerDao = playerDao;
}
public void close() throws Exception {
}
}

View File

@@ -2,11 +2,14 @@ package org.springframework.batch.sample.item.writer;
import java.io.Serializable;
import java.sql.Types;
import java.util.Properties;
import org.apache.commons.lang.SerializationUtils;
import org.springframework.batch.execution.scope.StepContext;
import org.springframework.batch.execution.scope.StepContextAware;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.util.Assert;
@@ -73,7 +76,37 @@ public class StagingItemWriter extends JdbcDaoSupport implements
new int[] { Types.BIGINT, Types.BIGINT, Types.BLOB, Types.CHAR});
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -18,13 +18,13 @@ package org.springframework.batch.sample.item.writer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.TradeDao;
import org.springframework.batch.sample.domain.Trade;
public class TradeWriter implements ItemWriter {
public class TradeWriter extends AbstractItemWriter {
private static Log log = LogFactory.getLog(TradeWriter.class);
private TradeDao dao;