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;