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

标签 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"));
}
}

最佳答案

实际上“不”。由于您使用 JMS 应用程序,因此不必在网络上相互查找

它是一个 JMS,因此它们都应该知道要连接的 Broker URL,用于发送部分和接收部分。

您只需要正确配置application.properties:

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

并继续在两侧使用相同的@EnableAutoConfiguration

关于java - 如何通过Spring JMS在两个java应用程序之间交换消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25742781/

相关文章:

java - 我在第一次运行时创建的属性文件在第二次运行时被清空

java - 从编辑页面返回到不同的引用页面

java - 如何使用 Java 和 Spring 知道 BlockingQueue 中当前有多少元素

java - 如何使用jms.Connection.setExceptionListner释放资源并重新连接?

java - JSF a4j :commandButton not working when 'disabled' is set

java - 将 MCQ 从字符串文件中提取到单个 Questions 对象的 Arraylist 中

java - 使用自定义类 Spring Boot Java 的 @Query 注解

eclipse - 无法解析 JRE : jdk7 (Standard VM)

glassfish - 将远程 JMS 客户端连接到 GlassFish 3

java - 远程 JMS 连接仍使用 localhost