001/* 002 * Copyright 2006-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.sample.domain.trade.internal; 018 019import java.math.BigDecimal; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.springframework.batch.core.annotation.AfterWrite; 026import org.springframework.batch.item.ExecutionContext; 027import org.springframework.batch.item.ItemStreamSupport; 028import org.springframework.batch.item.ItemWriter; 029import org.springframework.batch.item.WriteFailedException; 030import org.springframework.batch.sample.domain.trade.Trade; 031import org.springframework.batch.sample.domain.trade.TradeDao; 032import org.springframework.util.Assert; 033 034/** 035 * Delegates the actual writing to custom DAO delegate. Allows configurable 036 * exception raising for testing skip and restart. 037 */ 038public class TradeWriter extends ItemStreamSupport implements ItemWriter<Trade> { 039 040 private static Log log = LogFactory.getLog(TradeWriter.class); 041 042 public static final String TOTAL_AMOUNT_KEY = "TOTAL_AMOUNT"; 043 044 private TradeDao dao; 045 046 private List<String> failingCustomers = new ArrayList<String>(); 047 048 private BigDecimal totalPrice = BigDecimal.ZERO; 049 050 @Override 051 public void write(List<? extends Trade> trades) { 052 053 for (Trade trade : trades) { 054 055 log.debug(trade); 056 057 dao.writeTrade(trade); 058 059 Assert.notNull(trade.getPrice(), "price must not be null"); // There must be a price to total 060 061 if (this.failingCustomers.contains(trade.getCustomer())) { 062 throw new WriteFailedException("Something unexpected happened!"); 063 } 064 } 065 066 } 067 068 @AfterWrite 069 public void updateTotalPrice(List<Trade> trades) { 070 for (Trade trade : trades) { 071 this.totalPrice = this.totalPrice.add(trade.getPrice()); 072 } 073 } 074 075 @Override 076 public void open(ExecutionContext executionContext) { 077 if (executionContext.containsKey(TOTAL_AMOUNT_KEY)) { 078 this.totalPrice = (BigDecimal) executionContext.get(TOTAL_AMOUNT_KEY); 079 } 080 else { 081 // 082 // Fresh run. Disregard old state. 083 // 084 this.totalPrice = BigDecimal.ZERO; 085 } 086 } 087 088 @Override 089 public void update(ExecutionContext executionContext) { 090 executionContext.put(TOTAL_AMOUNT_KEY, this.totalPrice); 091 } 092 093 public BigDecimal getTotalPrice() { 094 return totalPrice; 095 } 096 097 public void setDao(TradeDao dao) { 098 this.dao = dao; 099 } 100 101 /** 102 * Public setter for the the customers on which failure should occur. 103 * 104 * @param failingCustomers The customers to fail on 105 */ 106 public void setFailingCustomers(List<String> failingCustomers) { 107 this.failingCustomers = failingCustomers; 108 } 109}