View Javadoc

1   package org.springframework.batch.sample.rabbitmq.amqp;
2   
3   import org.springframework.amqp.core.AmqpTemplate;
4   import org.springframework.amqp.rabbit.core.RabbitTemplate;
5   import org.springframework.context.ApplicationContext;
6   import org.springframework.context.ConfigurableApplicationContext;
7   import org.springframework.context.support.ClassPathXmlApplicationContext;
8   
9   /**
10   * <p>
11   * Simple producer class that sends {@link String} messages to the configured queue to be processed.
12   * </p>
13   */
14  public class AmqpMessageProducer {
15      public static final int SEND_MESSAGE_COUNT = 10;
16      public static final String[] BEAN_CONFIG = { "classpath:/META-INF/spring/jobs/messaging/rabbitmq-beans.xml",
17              "classpath:/META-INF/spring/config-beans.xml" };
18  
19      public static void main(String[] args) {
20          ApplicationContext applicationContext = new ClassPathXmlApplicationContext(BEAN_CONFIG);
21          AmqpTemplate amqpTemplate = applicationContext.getBean("inboundAmqpTemplate", RabbitTemplate.class);
22  
23          for (int i = 0; i < SEND_MESSAGE_COUNT; i++ ) {
24              amqpTemplate.convertAndSend("foo message: " + i);
25          }
26  
27          ((ConfigurableApplicationContext) applicationContext).close();
28      }
29  }