IN PROGRESS - issue BATCH-476: samples cleanup
http://jira.springframework.org/browse/BATCH-476
This commit is contained in:
@@ -31,7 +31,7 @@ import org.springframework.beans.factory.DisposableBean;
|
||||
public class FlatFileCustomerCreditDao implements CustomerCreditDao,
|
||||
DisposableBean {
|
||||
|
||||
private ItemWriter outputSource;
|
||||
private ItemWriter itemWriter;
|
||||
|
||||
private String separator = "\t";
|
||||
|
||||
@@ -46,7 +46,7 @@ public class FlatFileCustomerCreditDao implements CustomerCreditDao,
|
||||
String line = "" + customerCredit.getName() + separator
|
||||
+ customerCredit.getCredit();
|
||||
|
||||
outputSource.write(line);
|
||||
itemWriter.write(line);
|
||||
}
|
||||
|
||||
public void setSeparator(String separator) {
|
||||
@@ -54,19 +54,19 @@ public class FlatFileCustomerCreditDao implements CustomerCreditDao,
|
||||
}
|
||||
|
||||
public void setOutputSource(ItemWriter outputSource) {
|
||||
this.outputSource = outputSource;
|
||||
this.itemWriter = outputSource;
|
||||
}
|
||||
|
||||
public void open(ExecutionContext executionContext) throws Exception {
|
||||
if (outputSource instanceof ItemStream) {
|
||||
((ItemStream) outputSource).open(executionContext);
|
||||
if (itemWriter instanceof ItemStream) {
|
||||
((ItemStream) itemWriter).open(executionContext);
|
||||
}
|
||||
opened = true;
|
||||
}
|
||||
|
||||
public void close() throws Exception {
|
||||
if (outputSource instanceof ItemStream) {
|
||||
((ItemStream) outputSource).close(null);
|
||||
if (itemWriter instanceof ItemStream) {
|
||||
((ItemStream) itemWriter).close(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.batch.sample.domain.CustomerDebit;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
|
||||
public class CustomerUpdateMapper implements RowMapper {
|
||||
public class CustomerDebitRowMapper implements RowMapper {
|
||||
|
||||
public static final String CUSTOMER_COLUMN = "customer";
|
||||
public static final String PRICE_COLUMN = "price";
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.item.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Game;
|
||||
|
||||
public class GameFieldSetMapper implements FieldSetMapper {
|
||||
|
||||
public Object mapLine(FieldSet fs) {
|
||||
|
||||
if(fs == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
Game game = new Game();
|
||||
game.setId(fs.readString("id"));
|
||||
game.setYear(fs.readInt("year"));
|
||||
game.setTeam(fs.readString("team"));
|
||||
game.setWeek(fs.readInt("week"));
|
||||
game.setOpponent(fs.readString("opponent"));
|
||||
game.setCompletes(fs.readInt("completes"));
|
||||
game.setAttempts(fs.readInt("attempts"));
|
||||
game.setPassingYards(fs.readInt("passingYards"));
|
||||
game.setPassingTd(fs.readInt("passingTd"));
|
||||
game.setInterceptions(fs.readInt("interceptions"));
|
||||
game.setRushes(fs.readInt("rushes"));
|
||||
game.setRushYards(fs.readInt("rushYards"));
|
||||
game.setReceptions(fs.readInt("receptions", 0));
|
||||
game.setReceptionYards(fs.readInt("receptionYards"));
|
||||
game.setTotalTd(fs.readInt("totalTd"));
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import org.springframework.batch.item.file.mapping.FieldSet;
|
||||
import org.springframework.batch.item.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Player;
|
||||
|
||||
public class PlayerMapper implements FieldSetMapper {
|
||||
public class PlayerFieldSetMapper implements FieldSetMapper {
|
||||
|
||||
public Object mapLine(FieldSet fs) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.mapping;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.PlayerSummary;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* RowMapper used to map a ResultSet to a (@link PlayerSummary)
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class PlayerSummaryRowMapper implements RowMapper {
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
|
||||
*/
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
|
||||
PlayerSummary summary = new PlayerSummary();
|
||||
|
||||
summary.setId(rs.getString(1));
|
||||
summary.setYear(rs.getInt(2));
|
||||
summary.setCompletes(rs.getInt(3));
|
||||
summary.setAttempts(rs.getInt(4));
|
||||
summary.setPassingYards(rs.getInt(5));
|
||||
summary.setPassingTd(rs.getInt(6));
|
||||
summary.setInterceptions(rs.getInt(7));
|
||||
summary.setRushes(rs.getInt(8));
|
||||
summary.setRushYards(rs.getInt(9));
|
||||
summary.setReceptions(rs.getInt(10));
|
||||
summary.setReceptionYards(rs.getInt(11));
|
||||
summary.setTotalTd(rs.getInt(12));
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package org.springframework.batch.sample.step.support;
|
||||
|
||||
import org.springframework.batch.core.JobInterruptedException;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.step.StepInterruptionPolicy;
|
||||
|
||||
public class NoopStepInterruptionPolicy implements StepInterruptionPolicy {
|
||||
|
||||
public void checkInterrupted(StepExecution stepExecution)
|
||||
throws JobInterruptedException {
|
||||
// no-op
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,82 +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.tasklet;
|
||||
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.AbstractItemWriter;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.batch.sample.dao.TradeDao;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple implementation of a {@link Tasklet}, which illustrates the reading
|
||||
* and processing of input data. This can be viable in cases, when the input
|
||||
* reading and processing logic need not to be reused in different contexts. In
|
||||
* general it is recommended to separate these two concerns using an
|
||||
* {@link ItemOrientedTasklet}.
|
||||
*
|
||||
* Note this class is thread-safe, as per the 'standard' module implementations
|
||||
* provided by the framework.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class SimpleTradeWriter extends AbstractItemWriter implements ItemStream {
|
||||
|
||||
/*
|
||||
* writes a Trade object to output
|
||||
*/
|
||||
private TradeDao tradeDao;
|
||||
|
||||
/**
|
||||
* number of trade objects processed
|
||||
*/
|
||||
private int tradeCount = 0;
|
||||
|
||||
/**
|
||||
* The input template is read using the readAndMap method, which accepts a
|
||||
* FieldSetMapper. This call returns a Trade object, which is then
|
||||
* processed. Because this is a simple example job, the data is simply
|
||||
* written out without any processing.
|
||||
*/
|
||||
public void write(Object item) throws Exception {
|
||||
Assert.isInstanceOf(Trade.class, item, "Only items of type: [" + Trade.class + "] are supported by this writer");
|
||||
tradeCount++;
|
||||
tradeDao.writeTrade((Trade)item);
|
||||
}
|
||||
|
||||
public void setTradeDao(TradeDao tradeDao) {
|
||||
this.tradeDao = tradeDao;
|
||||
}
|
||||
|
||||
public void open(ExecutionContext context) throws ItemStreamException {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ExecutionContextProvider#getExecutionContext()
|
||||
*/
|
||||
public void update(ExecutionContext executionContext) {
|
||||
executionContext.putLong("trade.count", tradeCount);
|
||||
}
|
||||
|
||||
public void close(ExecutionContext executionContext) throws ItemStreamException {
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@
|
||||
</property>
|
||||
<property name="fieldSetMapper">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.mapping.PlayerMapper" />
|
||||
class="org.springframework.batch.sample.mapping.PlayerFieldSetMapper" />
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
|
||||
|
||||
<bean id="simpleSample" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<list>
|
||||
<bean id="step1" parent="simpleStep">
|
||||
<property name="itemReader" ref="fileItemReader" />
|
||||
<property name="itemWriter">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.tasklet.SimpleTradeWriter">
|
||||
<property name="tradeDao" ref="tradeDao" />
|
||||
</bean>
|
||||
</property>
|
||||
<property name="commitInterval" value="2" />
|
||||
</bean>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="tradeDao"
|
||||
class="org.springframework.batch.sample.dao.JdbcTradeDao">
|
||||
<property name="jdbcTemplate" ref="jdbcTemplate" />
|
||||
<property name="incrementer">
|
||||
<bean parent="incrementerParent">
|
||||
<property name="incrementerName" value="TRADE_SEQ" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- This input source is injected into the test case to verify the output - not used by the job at all -->
|
||||
<bean id="testInputTemplate"
|
||||
class="org.springframework.batch.item.file.FlatFileItemReader">
|
||||
<property name="resource" ref="fileLocator" />
|
||||
<property name="lineTokenizer" ref="tradeTokenizer" />
|
||||
<property name="fieldSetMapper" ref="tradeMapper" />
|
||||
</bean>
|
||||
|
||||
<bean id="fileItemReader" parent="testInputTemplate"
|
||||
autowire-candidate="false" />
|
||||
|
||||
<bean id="tradeMapper"
|
||||
class="org.springframework.batch.sample.mapping.TradeFieldSetMapper" />
|
||||
|
||||
<bean id="fileLocator"
|
||||
class="org.springframework.core.io.ClassPathResource">
|
||||
<constructor-arg type="java.lang.String"
|
||||
value="data/simpleTaskletJob/input/20070122.teststream.ImportTradeDataStep.txt" />
|
||||
</bean>
|
||||
|
||||
<bean id="tradeTokenizer"
|
||||
class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
|
||||
<property name="names" value="ISIN, Quantity, Price, Customer" />
|
||||
<property name="columns" value="1-12, 13-15, 16-20, 21-29" />
|
||||
</bean>
|
||||
|
||||
<bean id="tradeLogAdvice"
|
||||
class="org.springframework.batch.sample.advice.TradeWriterLogAdvice" />
|
||||
|
||||
<aop:config>
|
||||
<aop:aspect id="tradeWriterLogging" ref="tradeLogAdvice">
|
||||
|
||||
<aop:after
|
||||
pointcut="execution( * org.springframework.batch.sample.dao.TradeDao+.writeTrade(org.springframework.batch.sample.domain.Trade)) and args(trade)"
|
||||
method="doBasicLogging" />
|
||||
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
|
||||
</beans>
|
||||
@@ -1,25 +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;
|
||||
|
||||
/**
|
||||
* This job differs from FixedLengthImportJob only in internal job, therefore
|
||||
* the test is reused, only the job config location is overridden
|
||||
*/
|
||||
public class SimpleJobFunctionalTests extends FixedLengthImportJobFunctionalTests {
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.dao.TradeDao;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
|
||||
public class SimpleTradeTaskletTests extends TestCase {
|
||||
|
||||
private boolean inputCalled = false;
|
||||
private boolean writerCalled = false;
|
||||
|
||||
public void testReadAndProcess() throws Exception {
|
||||
|
||||
//create input
|
||||
FlatFileItemReader input = new FlatFileItemReader() {
|
||||
|
||||
private boolean done = false;
|
||||
|
||||
public Object read() {
|
||||
if (!done) {
|
||||
Trade trade = new Trade("1234", 5, new BigDecimal(100), "testName");
|
||||
inputCalled = true;
|
||||
done = true;
|
||||
return trade;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//create writer
|
||||
TradeDao dao = new TradeDao() {
|
||||
public void writeTrade(Trade trade) {
|
||||
assertEquals("1234",trade.getIsin());
|
||||
assertEquals(5, trade.getQuantity());
|
||||
assertEquals(new BigDecimal(100), trade.getPrice());
|
||||
assertEquals("testName", trade.getCustomer());
|
||||
writerCalled = true;
|
||||
}
|
||||
};
|
||||
|
||||
//create module
|
||||
SimpleTradeWriter module = new SimpleTradeWriter();
|
||||
module.setTradeDao(dao);
|
||||
|
||||
module.write(input.read());
|
||||
|
||||
//verify whether input and writer were called
|
||||
assertTrue(inputCalled);
|
||||
assertTrue(writerCalled);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user