001/* 002 * Copyright 2012 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 */ 016package org.springframework.batch.sample.rabbitmq.amqp; 017 018import org.springframework.amqp.core.AmqpTemplate; 019import org.springframework.amqp.rabbit.core.RabbitTemplate; 020import org.springframework.context.ApplicationContext; 021import org.springframework.context.ConfigurableApplicationContext; 022import org.springframework.context.support.ClassPathXmlApplicationContext; 023 024/** 025 * <p> 026 * Simple producer class that sends {@link String} messages to the configured queue to be processed. 027 * </p> 028 */ 029public final class AmqpMessageProducer { 030 private AmqpMessageProducer() {} 031 032 private static final int SEND_MESSAGE_COUNT = 10; 033 private static final String[] BEAN_CONFIG = { "classpath:/META-INF/spring/jobs/messaging/rabbitmq-beans.xml", 034 "classpath:/META-INF/spring/config-beans.xml" }; 035 036 public static void main(String[] args) { 037 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(BEAN_CONFIG); 038 AmqpTemplate amqpTemplate = applicationContext.getBean("inboundAmqpTemplate", RabbitTemplate.class); 039 040 for (int i = 0; i < SEND_MESSAGE_COUNT; i++ ) { 041 amqpTemplate.convertAndSend("foo message: " + i); 042 } 043 044 ((ConfigurableApplicationContext) applicationContext).close(); 045 } 046}