| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.core.listener; |
| 17 | |
| 18 | import org.springframework.batch.core.ChunkListener; |
| 19 | import org.springframework.batch.core.ItemReadListener; |
| 20 | import org.springframework.batch.core.ItemWriteListener; |
| 21 | import org.springframework.batch.core.SkipListener; |
| 22 | import org.springframework.batch.core.StepExecution; |
| 23 | import org.springframework.batch.core.StepExecutionListener; |
| 24 | import org.springframework.batch.core.StepListener; |
| 25 | import org.springframework.batch.item.ItemStream; |
| 26 | import org.springframework.batch.repeat.ExitStatus; |
| 27 | |
| 28 | /** |
| 29 | * @author Dave Syer |
| 30 | * |
| 31 | */ |
| 32 | public class MulticasterBatchListener implements StepExecutionListener, ChunkListener, ItemReadListener, |
| 33 | ItemWriteListener, SkipListener { |
| 34 | |
| 35 | private CompositeStepExecutionListener stepListener = new CompositeStepExecutionListener(); |
| 36 | |
| 37 | private CompositeChunkListener chunkListener = new CompositeChunkListener(); |
| 38 | |
| 39 | private CompositeItemReadListener itemReadListener = new CompositeItemReadListener(); |
| 40 | |
| 41 | private CompositeItemWriteListener itemWriteListener = new CompositeItemWriteListener(); |
| 42 | |
| 43 | private CompositeSkipListener skipListener = new CompositeSkipListener(); |
| 44 | |
| 45 | /** |
| 46 | * Initialise the listener instance. |
| 47 | */ |
| 48 | public MulticasterBatchListener() { |
| 49 | super(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register each of the objects as listeners. Once registered, calls to the |
| 54 | * {@link MulticasterBatchListener} broadcast to the individual listeners. |
| 55 | * |
| 56 | * @param listeners an array of listener objects of types known to the |
| 57 | * multicaster. |
| 58 | */ |
| 59 | public void setListeners(StepListener[] listeners) { |
| 60 | for (int i = 0; i < listeners.length; i++) { |
| 61 | register(listeners[i]); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Register the listener for callbacks on the appropriate interfaces |
| 67 | * implemented. Any {@link StepListener} can be provided, or an |
| 68 | * {@link ItemStream}. Other types will be ignored. |
| 69 | */ |
| 70 | public void register(StepListener listener) { |
| 71 | if (listener instanceof StepExecutionListener) { |
| 72 | this.stepListener.register((StepExecutionListener) listener); |
| 73 | } |
| 74 | if (listener instanceof ChunkListener) { |
| 75 | this.chunkListener.register((ChunkListener) listener); |
| 76 | } |
| 77 | if (listener instanceof ItemReadListener) { |
| 78 | this.itemReadListener.register((ItemReadListener) listener); |
| 79 | } |
| 80 | if (listener instanceof ItemWriteListener) { |
| 81 | this.itemWriteListener.register((ItemWriteListener) listener); |
| 82 | } |
| 83 | if (listener instanceof SkipListener) { |
| 84 | this.skipListener.register((SkipListener) listener); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @see org.springframework.batch.core.listener.CompositeStepExecutionListener#afterStep(StepExecution) |
| 90 | */ |
| 91 | public ExitStatus afterStep(StepExecution stepExecution) { |
| 92 | return stepListener.afterStep(stepExecution); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param stepExecution |
| 97 | * @see org.springframework.batch.core.listener.CompositeStepExecutionListener#beforeStep(org.springframework.batch.core.StepExecution) |
| 98 | */ |
| 99 | public void beforeStep(StepExecution stepExecution) { |
| 100 | stepListener.beforeStep(stepExecution); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param e |
| 105 | * @see org.springframework.batch.core.listener.CompositeStepExecutionListener#onErrorInStep(StepExecution, Throwable) |
| 106 | */ |
| 107 | public ExitStatus onErrorInStep(StepExecution stepExecution, Throwable e) { |
| 108 | return stepListener.onErrorInStep(stepExecution, e); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * |
| 113 | * @see org.springframework.batch.core.listener.CompositeChunkListener#afterChunk() |
| 114 | */ |
| 115 | public void afterChunk() { |
| 116 | chunkListener.afterChunk(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * |
| 121 | * @see org.springframework.batch.core.listener.CompositeChunkListener#beforeChunk() |
| 122 | */ |
| 123 | public void beforeChunk() { |
| 124 | chunkListener.beforeChunk(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param item |
| 129 | * @see org.springframework.batch.core.listener.CompositeItemReadListener#afterRead(java.lang.Object) |
| 130 | */ |
| 131 | public void afterRead(Object item) { |
| 132 | itemReadListener.afterRead(item); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * |
| 137 | * @see org.springframework.batch.core.listener.CompositeItemReadListener#beforeRead() |
| 138 | */ |
| 139 | public void beforeRead() { |
| 140 | itemReadListener.beforeRead(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param ex |
| 145 | * @see org.springframework.batch.core.listener.CompositeItemReadListener#onReadError(java.lang.Exception) |
| 146 | */ |
| 147 | public void onReadError(Exception ex) { |
| 148 | itemReadListener.onReadError(ex); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * |
| 153 | * @see org.springframework.batch.core.listener.CompositeItemWriteListener#afterWrite(Object) |
| 154 | */ |
| 155 | public void afterWrite(Object item) { |
| 156 | itemWriteListener.afterWrite(item); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param item |
| 161 | * @see org.springframework.batch.core.listener.CompositeItemWriteListener#beforeWrite(java.lang.Object) |
| 162 | */ |
| 163 | public void beforeWrite(Object item) { |
| 164 | itemWriteListener.beforeWrite(item); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param ex |
| 169 | * @param item |
| 170 | * @see org.springframework.batch.core.listener.CompositeItemWriteListener#onWriteError(java.lang.Exception, |
| 171 | * java.lang.Object) |
| 172 | */ |
| 173 | public void onWriteError(Exception ex, Object item) { |
| 174 | itemWriteListener.onWriteError(ex, item); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param t |
| 179 | * @see org.springframework.batch.core.listener.CompositeSkipListener#onSkipInRead(java.lang.Throwable) |
| 180 | */ |
| 181 | public void onSkipInRead(Throwable t) { |
| 182 | skipListener.onSkipInRead(t); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @param item |
| 187 | * @param t |
| 188 | * @see org.springframework.batch.core.listener.CompositeSkipListener#onSkipInWrite(java.lang.Object, java.lang.Throwable) |
| 189 | */ |
| 190 | public void onSkipInWrite(Object item, Throwable t) { |
| 191 | skipListener.onSkipInWrite(item, t); |
| 192 | } |
| 193 | |
| 194 | } |