IN PROGRESS - BATCH-711: Upgrade ItemWriter and implementations to use parameterized types
This commit is contained in:
@@ -16,13 +16,12 @@
|
||||
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.sample.domain.CustomerDebit;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.batch.sample.domain.Order;
|
||||
* Converts <code>Order</code> object to a String.
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class OrderTransformer implements ItemTransformer {
|
||||
public class OrderTransformer implements ItemTransformer<Order, List<String>> {
|
||||
|
||||
/**
|
||||
* Aggregators for all types of lines in the output file
|
||||
@@ -46,8 +46,7 @@ public class OrderTransformer implements ItemTransformer {
|
||||
* Converts information from an Order object to a collection of Strings for
|
||||
* output.
|
||||
*/
|
||||
public Object transform(Object data) {
|
||||
Order order = (Order) data;
|
||||
public List<String> transform(Order order) {
|
||||
|
||||
List<String> result = new ArrayList<String>();
|
||||
|
||||
|
||||
@@ -17,9 +17,9 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class BatchSqlCustomerCreditIncreaseWriter implements ItemWriter, InitializingBean {
|
||||
public class BatchSqlCustomerCreditIncreaseWriter implements ItemWriter<CustomerCredit>, InitializingBean {
|
||||
|
||||
private ItemWriter delegate;
|
||||
private ItemWriter<CustomerCredit> delegate;
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal(1000);
|
||||
|
||||
@@ -28,7 +28,7 @@ public class BatchSqlCustomerCreditIncreaseWriter implements ItemWriter, Initial
|
||||
* {@link BatchSqlUpdateItemWriter}.
|
||||
* @param delegate the delegate to set
|
||||
*/
|
||||
public void setDelegate(ItemWriter delegate) {
|
||||
public void setDelegate(ItemWriter<CustomerCredit> delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@@ -44,23 +44,14 @@ public class BatchSqlCustomerCreditIncreaseWriter implements ItemWriter, Initial
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.processor.DelegatingItemWriter#doProcess(java.lang.Object)
|
||||
*/
|
||||
public void write(Object data) throws Exception {
|
||||
CustomerCredit customerCredit = ((CustomerCredit) data).increaseCreditBy(FIXED_AMOUNT);
|
||||
public void write(CustomerCredit customerCredit) throws Exception {
|
||||
delegate.write(customerCredit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClearFailedException
|
||||
* @see org.springframework.batch.item.database.BatchSqlUpdateItemWriter#clear()
|
||||
*/
|
||||
public void clear() throws ClearFailedException {
|
||||
delegate.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FlushFailedException
|
||||
* @see org.springframework.batch.item.database.BatchSqlUpdateItemWriter#flush()
|
||||
*/
|
||||
public void flush() throws FlushFailedException {
|
||||
delegate.flush();
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CustomerCreditIncreaseWriter extends AbstractItemWriter {
|
||||
public class CustomerCreditIncreaseWriter extends AbstractItemWriter<CustomerCredit> {
|
||||
|
||||
public static final BigDecimal FIXED_AMOUNT = new BigDecimal("1000");
|
||||
|
||||
@@ -28,9 +28,9 @@ public class CustomerCreditIncreaseWriter extends AbstractItemWriter {
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.processor.DelegatingItemWriter#doProcess(java.lang.Object)
|
||||
*/
|
||||
public void write(Object data) throws Exception {
|
||||
CustomerCredit customerCredit = ((CustomerCredit) data).increaseCreditBy(FIXED_AMOUNT);
|
||||
customerCreditDao.writeCredit(customerCredit);
|
||||
public void write(CustomerCredit customerCredit) throws Exception {
|
||||
CustomerCredit result = customerCredit.increaseCreditBy(FIXED_AMOUNT);
|
||||
customerCreditDao.writeCredit(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,12 +22,11 @@ import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
|
||||
|
||||
|
||||
public class CustomerCreditUpdateWriter extends AbstractItemWriter {
|
||||
public class CustomerCreditUpdateWriter extends AbstractItemWriter<CustomerCredit> {
|
||||
private double creditFilter = 800;
|
||||
private CustomerCreditDao dao;
|
||||
|
||||
public void write(Object data) throws Exception {
|
||||
CustomerCredit customerCredit = (CustomerCredit) data;
|
||||
public void write(CustomerCredit customerCredit) throws Exception {
|
||||
|
||||
if (customerCredit.getCredit().doubleValue() > creditFilter) {
|
||||
dao.writeCredit(customerCredit);
|
||||
|
||||
@@ -27,12 +27,11 @@ import org.springframework.batch.sample.domain.Trade;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CustomerUpdateWriter extends AbstractItemWriter {
|
||||
public class CustomerUpdateWriter extends AbstractItemWriter<Trade> {
|
||||
|
||||
private CustomerDebitDao dao;
|
||||
|
||||
public void write(Object data) {
|
||||
Trade trade = (Trade) data;
|
||||
public void write(Trade trade) {
|
||||
CustomerDebit customerDebit = new CustomerDebit();
|
||||
customerDebit.setName(trade.getCustomer());
|
||||
customerDebit.setDebit(trade.getPrice());
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.batch.item.support.AbstractItemWriter;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class DummyItemWriter extends AbstractItemWriter {
|
||||
public class DummyItemWriter extends AbstractItemWriter<Object> {
|
||||
|
||||
public void write(Object item) throws Exception {
|
||||
// NO-OP
|
||||
|
||||
@@ -9,15 +9,15 @@ import org.springframework.batch.item.validator.ValidationException;
|
||||
/**
|
||||
* Remembers all items written - useful for testing.
|
||||
*/
|
||||
public class ItemTrackingItemWriter extends AbstractItemWriter {
|
||||
public class ItemTrackingItemWriter<T> extends AbstractItemWriter<T> {
|
||||
|
||||
private List<Object> items = new ArrayList<Object>();
|
||||
private List<T> items = new ArrayList<T>();
|
||||
|
||||
private int failure = -1;
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
public void write(Object item) throws Exception {
|
||||
public void write(T item) throws Exception {
|
||||
|
||||
items.add(item);
|
||||
if (++counter == failure) {
|
||||
@@ -25,7 +25,7 @@ public class ItemTrackingItemWriter extends AbstractItemWriter {
|
||||
}
|
||||
}
|
||||
|
||||
public List<Object> getItems() {
|
||||
public List<T> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.UnexpectedJobExecutionException;
|
||||
import org.springframework.batch.item.support.DelegatingItemWriter;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
|
||||
|
||||
public class OrderWriter extends DelegatingItemWriter {
|
||||
public Object doProcess(Object data) {
|
||||
if (!(data instanceof Order)) {
|
||||
throw new UnexpectedJobExecutionException("OrderProcessor can process only Order objects");
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,10 @@ import org.springframework.batch.sample.domain.Person;
|
||||
|
||||
|
||||
|
||||
public class PersonWriter extends AbstractItemWriter {
|
||||
public class PersonWriter extends AbstractItemWriter<Person> {
|
||||
private static Log log = LogFactory.getLog(PersonWriter.class);
|
||||
|
||||
public void write(Object data) {
|
||||
public void write(Person data) {
|
||||
if (!(data instanceof Person)) {
|
||||
log.warn("PersonProcessor can process only Person objects, skipping record");
|
||||
|
||||
|
||||
@@ -4,12 +4,12 @@ import org.springframework.batch.item.support.AbstractItemWriter;
|
||||
import org.springframework.batch.sample.dao.PlayerDao;
|
||||
import org.springframework.batch.sample.domain.Player;
|
||||
|
||||
public class PlayerItemWriter extends AbstractItemWriter {
|
||||
public class PlayerItemWriter extends AbstractItemWriter<Player> {
|
||||
|
||||
private PlayerDao playerDao;
|
||||
|
||||
public void write(Object data) throws Exception {
|
||||
playerDao.savePlayer((Player)data);
|
||||
public void write(Player player) throws Exception {
|
||||
playerDao.savePlayer(player);
|
||||
}
|
||||
|
||||
public void setPlayerDao(PlayerDao playerDao) {
|
||||
|
||||
@@ -8,11 +8,11 @@ import org.springframework.batch.item.support.AbstractItemWriter;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RetrySampleItemWriter extends AbstractItemWriter {
|
||||
public class RetrySampleItemWriter<T> extends AbstractItemWriter<T> {
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
public void write(Object data) throws Exception {
|
||||
public void write(T data) throws Exception {
|
||||
counter++;
|
||||
if (counter == 2 || counter == 3) {
|
||||
throw new RuntimeException("Temporary error");
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.springframework.util.ClassUtils;
|
||||
/**
|
||||
* Database {@link ItemWriter} implementing the process indicator pattern.
|
||||
*/
|
||||
public class StagingItemWriter extends JdbcDaoSupport implements StepExecutionListener, ItemWriter {
|
||||
public class StagingItemWriter<T> extends JdbcDaoSupport implements StepExecutionListener, ItemWriter<T> {
|
||||
|
||||
public static final String NEW = "N";
|
||||
|
||||
@@ -70,7 +70,7 @@ public class StagingItemWriter extends JdbcDaoSupport implements StepExecutionLi
|
||||
*
|
||||
* @see ItemWriter#write(java.lang.Object)
|
||||
*/
|
||||
public void write(Object data) {
|
||||
public void write(T data) {
|
||||
final long id = incrementer.nextLongValue();
|
||||
final long jobId = stepExecution.getJobExecution().getJobId().longValue();
|
||||
final byte[] blob = SerializationUtils.serialize((Serializable) data);
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.springframework.batch.sample.domain.Trade;
|
||||
* Delegates the actual writing to custom DAO delegate. Allows configurable
|
||||
* exception raising for testing skip and restart.
|
||||
*/
|
||||
public class TradeWriter extends AbstractItemWriter {
|
||||
public class TradeWriter extends AbstractItemWriter<Trade> {
|
||||
private static Log log = LogFactory.getLog(TradeWriter.class);
|
||||
|
||||
private TradeDao dao;
|
||||
@@ -44,17 +44,10 @@ public class TradeWriter extends AbstractItemWriter {
|
||||
this.failure = failure;
|
||||
}
|
||||
|
||||
public void write(Object data) {
|
||||
if (!(data instanceof Trade)) {
|
||||
log.warn("TradeProcessor can process only Trade objects, skipping record");
|
||||
public void write(Trade trade) {
|
||||
|
||||
return;
|
||||
}
|
||||
log.debug(trade);
|
||||
|
||||
Trade trade = (Trade) data;
|
||||
log.debug(data);
|
||||
|
||||
// TODO put some processing of the trade object here
|
||||
dao.writeTrade(trade);
|
||||
|
||||
if (index++ == failure) {
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.batch.sample.domain.Trade;
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class TradeFieldSetMapper implements FieldSetMapper {
|
||||
|
||||
public static final int ISIN_COLUMN = 0;
|
||||
|
||||
Reference in New Issue
Block a user