| 1 | /* |
| 2 | * Copyright 2012 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 | |
| 17 | package org.springframework.batch.item.amqp; |
| 18 | |
| 19 | import org.apache.commons.logging.Log; |
| 20 | import org.apache.commons.logging.LogFactory; |
| 21 | import org.springframework.amqp.core.AmqpTemplate; |
| 22 | import org.springframework.batch.item.ItemWriter; |
| 23 | import org.springframework.util.Assert; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * <p> |
| 29 | * AMQP {@link ItemWriter} implementation using an {@link AmqpTemplate} to |
| 30 | * send messages. Messages will be sent to the nameless exchange if not specified |
| 31 | * on the provided {@link AmqpTemplate}. |
| 32 | * </p> |
| 33 | * |
| 34 | * @author Chris Schaefer |
| 35 | */ |
| 36 | public class AmqpItemWriter<T> implements ItemWriter<T> { |
| 37 | private final AmqpTemplate amqpTemplate; |
| 38 | private final Log log = LogFactory.getLog(getClass()); |
| 39 | |
| 40 | public AmqpItemWriter(final AmqpTemplate amqpTemplate) { |
| 41 | Assert.notNull(amqpTemplate, "AmpqTemplate must not be null"); |
| 42 | |
| 43 | this.amqpTemplate = amqpTemplate; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void write(final List<? extends T> items) throws Exception { |
| 48 | if (log.isDebugEnabled()) { |
| 49 | log.debug("Writing to AMQP with " + items.size() + " items."); |
| 50 | } |
| 51 | |
| 52 | for (T item : items) { |
| 53 | amqpTemplate.convertAndSend(item); |
| 54 | } |
| 55 | } |
| 56 | } |