title: how to exchange messages between two java applications via Spring JMS?
body: <p>I have two simple Spring applications acting as sender and receiver (code below). Although following <a href="http://spring.io/guides/gs/messaging-jms/" rel="nofollow">this guide</a> I can send and receive the messages within the same application I could not find how to make it work between two separate applications. I assume there should be a way of configuring both applications to find each other on the network. How can I do that using Spring configuration?</p>
  
  <p><strong>Sender application</strong></p>
  
  <pre><code>@Configuration
  @EnableAutoConfiguration
  public class Application 
  {
  
  static String mailboxDestination = "mailbox-destination";
  
  
  public static void main(String[] args) {
      // Clean out any ActiveMQ data from a previous run
      FileSystemUtils.deleteRecursively(new File("activemq-data"));
  
      // Launch the application
      ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
  
      // Send a message
      MessageCreator messageCreator = new MessageCreator() {
          @Override
          public Message createMessage(Session session) throws JMSException {
              return session.createTextMessage("ping!");
          }
      };
      JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
      System.out.println("Sending a new message.");
      jmsTemplate.send(mailboxDestination, messageCreator);
      try {
          Thread.sleep(10000);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p><strong>Receiver application</strong></p>
  
  <pre><code>@Configuration
  @EnableAutoConfiguration
  public class Application 
  {
  
  static String mailboxDestination = "mailbox-destination";
  
  @Bean
  Receiver receiver() {
      return new Receiver();
  }
  
  @Bean
  MessageListenerAdapter adapter(Receiver receiver) {
      MessageListenerAdapter messageListener
              = new MessageListenerAdapter(receiver);
      messageListener.setDefaultListenerMethod("receiveMessage");
      return messageListener;
  }
  
  @Bean
  SimpleMessageListenerContainer container(MessageListenerAdapter messageListener,
                                           ConnectionFactory connectionFactory) {
      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
      container.setMessageListener(messageListener);
      container.setConnectionFactory(connectionFactory);
      container.setDestinationName(mailboxDestination);
      return container;
  }
  
  public static void main(String[] args) {
      // Clean out any ActiveMQ data from a previous run
      FileSystemUtils.deleteRecursively(new File("activemq-data"));
  
      // Launch the application
      ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
  
  
  
  }
  
  }
  
  
  
  public class Receiver 
  {
  
  @Autowired
  ConfigurableApplicationContext context;
  
  /**
   * When you receive a message, print it out, then shut down the application.
   * Finally, clean up any ActiveMQ server stuff.
   */
  public void receiveMessage(String message) {
      System.out.println("Received &lt;" + message + "&gt;");
      try {
          Thread.sleep(10000);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      context.close();
      FileSystemUtils.deleteRecursively(new File("activemq-data"));
  }
  }
  </code></pre>