Use the Chunk API consistently

This commit replaces the usage of List with Chunk
where appropriate. Summary of changes:

- The Chunk class was moved from the `org.springframework.batch.core.step.item` package to the `org.springframework.batch.item` package
- The signature of the method `ItemWriter#write(List)` was changed to `ItemWriter#write(Chunk)`
- All implementations of `ItemWriter` were updated to use the Chunk API instead of List
- All methods in the `ItemWriteListener` interface were updated to use the Chunk API instead of List
- All implementations of `ItemWriteListener` were updated to use the Chunk API instead of List
- The constructor of `ChunkRequest` was changed to accept a Chunk instead of a Collection of items
- The return type of `ChunkRequest#getItems()` was changed from List to Chunk

Resolves #3954
This commit is contained in:
Mahmoud Ben Hassine
2022-08-17 21:06:09 +02:00
parent bf2e6ab0e5
commit e67c0069f1
175 changed files with 1077 additions and 763 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 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.
@@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
@@ -53,7 +54,7 @@ public class InfiniteLoopWriter implements StepExecutionListener, ItemWriter<Obj
}
@Override
public void write(List<? extends Object> items) throws Exception {
public void write(Chunk<? extends Object> items) throws Exception {
try {
Thread.sleep(500);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2022 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.
@@ -24,6 +24,7 @@ import java.util.ListIterator;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
@@ -72,14 +73,14 @@ public class StagingItemWriter<T> extends JdbcDaoSupport implements StepExecutio
* @see ItemWriter#write(java.util.List)
*/
@Override
public void write(final List<? extends T> items) {
final ListIterator<? extends T> itemIterator = items.listIterator();
public void write(final Chunk<? extends T> chunk) {
final ListIterator<? extends T> itemIterator = chunk.getItems().listIterator();
getJdbcTemplate().batchUpdate("INSERT into BATCH_STAGING (ID, JOB_ID, VALUE, PROCESSED) values (?,?,?,?)",
new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
return items.size();
return chunk.size();
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,6 +18,7 @@ package org.springframework.batch.sample.domain.football.internal;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.football.Game;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@@ -38,7 +39,7 @@ public class JdbcGameDao extends JdbcDaoSupport implements ItemWriter<Game> {
}
@Override
public void write(List<? extends Game> games) {
public void write(Chunk<? extends Game> games) {
for (Game game : games) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2012 the original author or authors.
* Copyright 2006-2022 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.
@@ -20,6 +20,7 @@ import java.util.List;
import javax.sql.DataSource;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.football.PlayerSummary;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@@ -36,7 +37,7 @@ public class JdbcPlayerSummaryDao implements ItemWriter<PlayerSummary> {
private NamedParameterJdbcOperations namedParameterJdbcTemplate;
@Override
public void write(List<? extends PlayerSummary> summaries) {
public void write(Chunk<? extends PlayerSummary> summaries) {
for (PlayerSummary summary : summaries) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,6 +18,7 @@ package org.springframework.batch.sample.domain.football.internal;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.football.Player;
import org.springframework.batch.sample.domain.football.PlayerDao;
@@ -27,7 +28,7 @@ public class PlayerItemWriter implements ItemWriter<Player> {
private PlayerDao playerDao;
@Override
public void write(List<? extends Player> players) throws Exception {
public void write(Chunk<? extends Player> players) throws Exception {
for (Player player : players) {
playerDao.savePlayer(player);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 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.
@@ -20,6 +20,8 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.person.Person;
@@ -28,7 +30,7 @@ public class PersonWriter implements ItemWriter<Person> {
private static Log log = LogFactory.getLog(PersonWriter.class);
@Override
public void write(List<? extends Person> data) {
public void write(Chunk<? extends Person> data) {
if (log.isDebugEnabled()) {
log.debug("Processing: " + data);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,10 +18,12 @@ package org.springframework.batch.sample.domain.trade;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
*/
public class CustomerUpdateWriter implements ItemWriter<CustomerUpdate> {
@@ -29,7 +31,7 @@ public class CustomerUpdateWriter implements ItemWriter<CustomerUpdate> {
private CustomerDao customerDao;
@Override
public void write(List<? extends CustomerUpdate> items) throws Exception {
public void write(Chunk<? extends CustomerUpdate> items) throws Exception {
for (CustomerUpdate customerUpdate : items) {
if (customerUpdate.getOperation() == CustomerOperation.ADD) {
customerDao.insertCustomer(customerUpdate.getCustomerName(), customerUpdate.getCredit());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,6 +18,7 @@ package org.springframework.batch.sample.domain.trade.internal;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
@@ -26,6 +27,7 @@ import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
* Delegates actual writing to a custom DAO.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
public class CustomerCreditItemWriter implements ItemWriter<CustomerCredit> {
@@ -40,7 +42,7 @@ public class CustomerCreditItemWriter implements ItemWriter<CustomerCredit> {
}
@Override
public void write(List<? extends CustomerCredit> customerCredits) throws Exception {
public void write(Chunk<? extends CustomerCredit> customerCredits) throws Exception {
for (CustomerCredit customerCredit : customerCredits) {
customerCreditDao.writeCredit(customerCredit);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,6 +18,7 @@ package org.springframework.batch.sample.domain.trade.internal;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
@@ -29,7 +30,7 @@ public class CustomerCreditUpdateWriter implements ItemWriter<CustomerCredit> {
private CustomerCreditDao dao;
@Override
public void write(List<? extends CustomerCredit> customerCredits) throws Exception {
public void write(Chunk<? extends CustomerCredit> customerCredits) throws Exception {
for (CustomerCredit customerCredit : customerCredits) {
if (customerCredit.getCredit().doubleValue() > creditFilter) {
dao.writeCredit(customerCredit);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,6 +18,7 @@ package org.springframework.batch.sample.domain.trade.internal;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerDebit;
import org.springframework.batch.sample.domain.trade.CustomerDebitDao;
@@ -27,13 +28,14 @@ import org.springframework.batch.sample.domain.trade.Trade;
* Transforms Trade to a CustomerDebit and asks DAO delegate to write the result.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
public class CustomerUpdateWriter implements ItemWriter<Trade> {
private CustomerDebitDao dao;
@Override
public void write(List<? extends Trade> trades) {
public void write(Chunk<? extends Trade> trades) {
for (Trade trade : trades) {
CustomerDebit customerDebit = new CustomerDebit();
customerDebit.setName(trade.getCustomer());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,6 +18,7 @@ package org.springframework.batch.sample.domain.trade.internal;
import java.util.Collections;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
@@ -30,6 +31,7 @@ import org.springframework.beans.factory.DisposableBean;
*
* @see CustomerCreditDao
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
public class FlatFileCustomerCreditDao implements CustomerCreditDao, DisposableBean {
@@ -48,7 +50,7 @@ public class FlatFileCustomerCreditDao implements CustomerCreditDao, DisposableB
String line = "" + customerCredit.getName() + separator + customerCredit.getCredit();
itemWriter.write(Collections.singletonList(line));
itemWriter.write(Chunk.of(line));
}
public void setSeparator(String separator) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2022 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.
@@ -19,6 +19,8 @@ package org.springframework.batch.sample.domain.trade.internal;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
@@ -31,6 +33,7 @@ import org.springframework.util.Assert;
*
* @author Robert Kasanicky
* @author Michael Minella
* @author Mahmoud Ben Hassine
*/
public class HibernateAwareCustomerCreditItemWriter implements ItemWriter<CustomerCredit>, InitializingBean {
@@ -39,7 +42,7 @@ public class HibernateAwareCustomerCreditItemWriter implements ItemWriter<Custom
private SessionFactory sessionFactory;
@Override
public void write(List<? extends CustomerCredit> items) throws Exception {
public void write(Chunk<? extends CustomerCredit> items) throws Exception {
for (CustomerCredit credit : items) {
dao.writeCredit(credit);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 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.
@@ -23,6 +23,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamSupport;
import org.springframework.batch.item.ItemWriter;
@@ -48,7 +49,7 @@ public class TradeWriter extends ItemStreamSupport implements ItemWriter<Trade>
private BigDecimal totalPrice = BigDecimal.ZERO;
@Override
public void write(List<? extends Trade> trades) {
public void write(Chunk<? extends Trade> trades) {
for (Trade trade : trades) {
@@ -67,7 +68,7 @@ public class TradeWriter extends ItemStreamSupport implements ItemWriter<Trade>
}
@AfterWrite
public void updateTotalPrice(List<Trade> trades) {
public void updateTotalPrice(Chunk<Trade> trades) {
for (Trade trade : trades) {
this.totalPrice = this.totalPrice.add(trade.getPrice());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -17,16 +17,18 @@ package org.springframework.batch.sample.support;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class DummyItemWriter implements ItemWriter<Object> {
@Override
public void write(List<? extends Object> item) throws Exception {
public void write(Chunk<? extends Object> item) throws Exception {
// NO-OP
Thread.sleep(500);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2022 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.
@@ -18,19 +18,21 @@ package org.springframework.batch.sample.support;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
/**
* Simulates temporary output trouble - requires to retry 3 times to pass successfully.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
public class RetrySampleItemWriter<T> implements ItemWriter<T> {
private int counter = 0;
@Override
public void write(List<? extends T> items) throws Exception {
public void write(Chunk<? extends T> items) throws Exception {
int current = counter;
counter += items.size();
if (current < 3 && (counter >= 2 || counter >= 3)) {
@@ -39,7 +41,7 @@ public class RetrySampleItemWriter<T> implements ItemWriter<T> {
}
/**
* @return number of times {@link #write(List)} method was called.
* @return number of times {@link #write(Chunk)} method was called.
*/
public int getCounter() {
return counter;

View File

@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.test.AssertFile;
@@ -34,6 +35,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
@SpringJUnitConfig(
@@ -64,7 +66,7 @@ class RestartFileSampleFunctionalTests {
private boolean failed = false;
@Override
public void write(List<? extends CustomerCredit> arg0) throws Exception {
public void write(Chunk<? extends CustomerCredit> arg0) throws Exception {
for (CustomerCredit cc : arg0) {
if (!failed && cc.getName().equals("customer13")) {
failed = true;

View File

@@ -22,6 +22,8 @@ import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
@@ -31,6 +33,7 @@ import org.springframework.batch.support.transaction.TransactionAwareProxyFactor
* code base shifts.
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
*/
class CustomItemWriterTests {
@@ -38,9 +41,9 @@ class CustomItemWriterTests {
@Test
void testFlush() throws Exception {
CustomItemWriter<String> itemWriter = new CustomItemWriter<>();
itemWriter.write(Collections.singletonList("1"));
itemWriter.write(Chunk.of("1"));
assertEquals(1, itemWriter.getOutput().size());
itemWriter.write(Arrays.asList("2", "3"));
itemWriter.write(Chunk.of("2", "3"));
assertEquals(3, itemWriter.getOutput().size());
}
@@ -49,8 +52,8 @@ class CustomItemWriterTests {
private List<T> output = TransactionAwareProxyFactory.createTransactionalList();
@Override
public void write(List<? extends T> items) throws Exception {
output.addAll(items);
public void write(Chunk<? extends T> chunk) throws Exception {
output.addAll(chunk.getItems());
}
public List<T> getOutput() {

View File

@@ -27,6 +27,7 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.Chunk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -66,7 +67,7 @@ class StagingItemReaderTests {
StepExecution stepExecution = new StepExecution("stepName",
new JobExecution(new JobInstance(jobId, "testJob"), new JobParameters()));
writer.beforeStep(stepExecution);
writer.write(Arrays.asList("FOO", "BAR", "SPAM", "BUCKET"));
writer.write(Chunk.of("FOO", "BAR", "SPAM", "BUCKET"));
reader.beforeStep(stepExecution);
}

View File

@@ -27,6 +27,7 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.Chunk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -57,7 +58,7 @@ class StagingItemWriterTests {
@Test
void testProcessInsertsNewItem() {
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");
writer.write(Collections.singletonList("FOO"));
writer.write(Chunk.of("FOO"));
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");
assertEquals(before + 1, after);
}

View File

@@ -26,6 +26,7 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.football.Game;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
@@ -36,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
/**
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
*/
@SpringJUnitConfig(locations = { "/data-source-context.xml" })
@@ -77,7 +79,7 @@ class JdbcGameDaoIntegrationTests {
@Transactional
@Test
void testWrite() {
gameDao.write(Collections.singletonList(game));
gameDao.write(Chunk.of(game));
Game tempGame = jdbcTemplate.queryForObject("SELECT * FROM GAMES where PLAYER_ID=? AND YEAR_NO=?",
new GameRowMapper(), "XXXXX00 ", game.getYear());

View File

@@ -23,6 +23,8 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.football.PlayerSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -73,7 +75,7 @@ class JdbcPlayerSummaryDaoIntegrationTests {
@Test
@Transactional
void testWrite() {
playerSummaryDao.write(Collections.singletonList(summary));
playerSummaryDao.write(Chunk.of(summary));
PlayerSummary testSummary = jdbcTemplate.queryForObject("SELECT * FROM PLAYER_SUMMARY",
new PlayerSummaryMapper());

View File

@@ -22,6 +22,8 @@ import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
@@ -47,13 +49,13 @@ class CustomerCreditUpdateProcessorTests {
CustomerCredit credit = new CustomerCredit();
credit.setCredit(new BigDecimal(CREDIT_FILTER));
writer.write(Collections.singletonList(credit));
writer.write(Chunk.of(credit));
credit.setCredit(new BigDecimal(CREDIT_FILTER + 1));
dao.writeCredit(credit);
writer.write(Collections.singletonList(credit));
writer.write(Chunk.of(credit));
}
}

View File

@@ -21,6 +21,8 @@ import java.math.BigDecimal;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.trade.CustomerDebit;
import org.springframework.batch.sample.domain.trade.CustomerDebitDao;
import org.springframework.batch.sample.domain.trade.Trade;
@@ -44,7 +46,7 @@ class CustomerUpdateProcessorTests {
CustomerUpdateWriter processor = new CustomerUpdateWriter();
processor.setDao(dao);
processor.write(Collections.singletonList(trade));
processor.write(Chunk.of(trade));
}
}

View File

@@ -22,6 +22,8 @@ import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
@@ -65,7 +67,7 @@ class FlatFileCustomerCreditDaoTests {
writer.setSeparator(";");
output.write(Collections.singletonList("testName;1"));
output.write(Chunk.of("testName;1"));
output.open(new ExecutionContext());
writer.writeCredit(credit);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2022 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.
@@ -21,6 +21,7 @@ import java.util.List;
import javax.sql.DataSource;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.jdbc.core.JdbcOperations;
@@ -47,7 +48,7 @@ public class ItemTrackingTradeItemWriter implements ItemWriter<Trade> {
}
@Override
public void write(List<? extends Trade> items) throws Exception {
public void write(Chunk<? extends Trade> items) throws Exception {
List<Trade> newItems = new ArrayList<>();
for (Trade t : items) {

View File

@@ -21,6 +21,8 @@ import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.TradeDao;
@@ -44,7 +46,7 @@ class TradeProcessorTests {
writer.writeTrade(trade);
processor.write(Collections.singletonList(trade));
processor.write(Chunk.of(trade));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2022 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.
@@ -19,6 +19,7 @@ package org.springframework.batch.sample.iosample.internal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
@@ -28,6 +29,7 @@ import org.springframework.batch.sample.domain.trade.Trade;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class MultiLineTradeItemWriter implements ItemWriter<Trade>, ItemStream {
@@ -35,8 +37,8 @@ public class MultiLineTradeItemWriter implements ItemWriter<Trade>, ItemStream {
private FlatFileItemWriter<String> delegate;
@Override
public void write(List<? extends Trade> items) throws Exception {
List<String> lines = new ArrayList<>();
public void write(Chunk<? extends Trade> items) throws Exception {
Chunk<String> lines = new Chunk<>();
for (Trade t : items) {
lines.add("BEGIN");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2022 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.
@@ -19,6 +19,7 @@ package org.springframework.batch.sample.iosample.internal;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.Trade;
@@ -26,6 +27,7 @@ import org.springframework.batch.sample.domain.trade.TradeDao;
/**
* @author Dan Garrette
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class TradeCustomerItemWriter implements ItemWriter<CustomerCredit> {
@@ -35,7 +37,7 @@ public class TradeCustomerItemWriter implements ItemWriter<CustomerCredit> {
private int count;
@Override
public void write(List<? extends CustomerCredit> items) throws Exception {
public void write(Chunk<? extends CustomerCredit> items) throws Exception {
for (CustomerCredit c : items) {
Trade t = new Trade("ISIN" + count++, 100, new BigDecimal("1.50"), c.getName());
this.dao.writeTrade(t);

View File

@@ -22,11 +22,14 @@ import java.io.IOException;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
class ItemTrackingItemWriterTests {
@@ -39,7 +42,7 @@ class ItemTrackingItemWriterTests {
Trade a = new Trade("a", 0, null, null);
Trade b = new Trade("b", 0, null, null);
Trade c = new Trade("c", 0, null, null);
writer.write(Arrays.asList(a, b, c));
writer.write(Chunk.of(a, b, c));
assertEquals(3, writer.getItems().size());
}
@@ -49,13 +52,13 @@ class ItemTrackingItemWriterTests {
Trade a = new Trade("a", 0, null, null);
Trade b = new Trade("b", 0, null, null);
Trade c = new Trade("c", 0, null, null);
assertThrows(IOException.class, () -> writer.write(Arrays.asList(a, b, c)));
assertThrows(IOException.class, () -> writer.write(Chunk.of(a, b, c)));
assertEquals(0, writer.getItems().size());
Trade e = new Trade("e", 0, null, null);
Trade f = new Trade("f", 0, null, null);
Trade g = new Trade("g", 0, null, null);
writer.write(Arrays.asList(e, f, g));
writer.write(Chunk.of(e, f, g));
assertEquals(3, writer.getItems().size());
}

View File

@@ -23,10 +23,13 @@ import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.springframework.batch.item.Chunk;
/**
* Tests for {@link RetrySampleItemWriter}.
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
class RetrySampleItemWriterTests {
@@ -38,11 +41,11 @@ class RetrySampleItemWriterTests {
@Test
void testProcess() throws Exception {
Object item = null;
processor.write(Collections.singletonList(item));
processor.write(Chunk.of(item));
assertThrows(RuntimeException.class, () -> processor.write(Arrays.asList(item, item, item)));
assertThrows(RuntimeException.class, () -> processor.write(Chunk.of(item, item, item)));
processor.write(Collections.singletonList(item));
processor.write(Chunk.of(item));
assertEquals(5, processor.getCounter());
}