1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.item.support;
18
19 import java.util.List;
20
21 import org.springframework.batch.item.ExecutionContext;
22 import org.springframework.batch.item.ItemStream;
23 import org.springframework.batch.item.ItemStreamException;
24 import org.springframework.batch.item.ItemStreamWriter;
25 import org.springframework.batch.item.ItemWriter;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.util.Assert;
28
29
30
31
32
33
34
35
36
37
38 public class CompositeItemWriter<T> implements ItemStreamWriter<T>, InitializingBean {
39
40 private List<ItemWriter<? super T>> delegates;
41
42 private boolean ignoreItemStream = false;
43
44 public void setIgnoreItemStream(boolean ignoreItemStream) {
45 this.ignoreItemStream = ignoreItemStream;
46 }
47
48 @Override
49 public void write(List<? extends T> item) throws Exception {
50 for (ItemWriter<? super T> writer : delegates) {
51 writer.write(item);
52 }
53 }
54
55 @Override
56 public void afterPropertiesSet() throws Exception {
57 Assert.notNull(delegates, "The 'delgates' may not be null");
58 Assert.notEmpty(delegates, "The 'delgates' may not be empty");
59 }
60
61 public void setDelegates(List<ItemWriter<? super T>> delegates) {
62 this.delegates = delegates;
63 }
64
65 @Override
66 public void close() throws ItemStreamException {
67 for (ItemWriter<? super T> writer : delegates) {
68 if (!ignoreItemStream && (writer instanceof ItemStream)) {
69 ((ItemStream) writer).close();
70 }
71 }
72 }
73
74 @Override
75 public void open(ExecutionContext executionContext) throws ItemStreamException {
76 for (ItemWriter<? super T> writer : delegates) {
77 if (!ignoreItemStream && (writer instanceof ItemStream)) {
78 ((ItemStream) writer).open(executionContext);
79 }
80 }
81 }
82
83 @Override
84 public void update(ExecutionContext executionContext) throws ItemStreamException {
85 for (ItemWriter<? super T> writer : delegates) {
86 if (!ignoreItemStream && (writer instanceof ItemStream)) {
87 ((ItemStream) writer).update(executionContext);
88 }
89 }
90 }
91
92 }