如何通过Spring JMS在两个java应用程序之间交换消息?

时间:2014-09-09 10:52:45

标签: java spring jms spring-jms

我有两个简单的Spring应用程序充当发送方和接收方(下面的代码)。虽然关注this guide我可以在同一个应用程序中发送和接收消息,但我找不到如何使它在两个单独的应用程序之间工作。我假设应该有一种配置两个应用程序的方法,以便在网络上找到彼此。我怎么能用Spring配置呢?

发件人申请

@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();
    }
  }
}

接收方申请

@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 <" + message + ">");
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    context.close();
    FileSystemUtils.deleteRecursively(new File("activemq-data"));
}
}

1 个答案:

答案 0 :(得分:1)

实际上&#34;否&#34;。由于您使用JMS应用程序,因此不必查找each other on the network

它是一个JMS,所以他们都应该知道要连接的Broker URL,如发送部分和接收。

您只需要正确配置application.properties

spring.activemq.broker-url=tcp://192.168.1.210:9876
spring.activemq.user=admin
spring.activemq.password=secret

继续在双方使用相同的@EnableAutoConfiguration