0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

条件を満たしたらスキップするItemProcessorを実装したい

0
Posted at

Spring Batchで、ある条件を満たしたらそのアイテムはスキップするItemProcessorを実装したい場合は、以下ドキュメントにもある通り、ItemProcessorの戻り値をnullにすればよいです。

たとえば、注文Orderのうち、価格Priceが200以下のものをフィルターしたい場合、ItemProcessorは次の通り実装します。

import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

@Component
public class OrderFilterProcessor implements ItemProcessor<Order, Order> {
	@Override
	public Order process(Order order) throws Exception {
		if (order.getPrice() <= 200) {
			return null;
		}
		return order;
	}
}

あるいは、ItemProcessorは関数インターフェースなので、StepのBean定義にて、以下のように書くこともできます。

@Configuration
public class BatchConfig {
	@Bean
	public Step sampleStep(
			JobRepository jobRepository,
			PlatformTransactionManager platformTransactionManager,
			ItemReader<Order> sampleReader,
			ItemWriter<Order> sampleWriter) {
		return new StepBuilder("sampleStep", jobRepository)
				.<Order, Order>chunk(5, platformTransactionManager)
				.reader(sampleReader)
				.processor(order -> order.getPrice() <= 200 ? null : order)
				.writer(sampleWriter)
				.build();
	}
 }

環境情報

  • Spring Boot 3.3.1
  • Spring Batch 5.1.2
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?