001/* 002 * Copyright 2012-2018 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.item.amqp; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.springframework.amqp.core.AmqpTemplate; 022import org.springframework.batch.item.ItemWriter; 023import org.springframework.util.Assert; 024 025import java.util.List; 026 027/** 028 * <p> 029 * AMQP {@link ItemWriter} implementation using an {@link AmqpTemplate} to 030 * send messages. Messages will be sent to the nameless exchange if not specified 031 * on the provided {@link AmqpTemplate}. 032 * </p> 033 * 034 * @author Chris Schaefer 035 * @author Mahmoud Ben Hassine 036 */ 037public class AmqpItemWriter<T> implements ItemWriter<T> { 038 private final AmqpTemplate amqpTemplate; 039 private final Log log = LogFactory.getLog(getClass()); 040 041 public AmqpItemWriter(final AmqpTemplate amqpTemplate) { 042 Assert.notNull(amqpTemplate, "AmqpTemplate must not be null"); 043 044 this.amqpTemplate = amqpTemplate; 045 } 046 047 @Override 048 public void write(final List<? extends T> items) throws Exception { 049 if (log.isDebugEnabled()) { 050 log.debug("Writing to AMQP with " + items.size() + " items."); 051 } 052 053 for (T item : items) { 054 amqpTemplate.convertAndSend(item); 055 } 056 } 057}