gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:33:41 24 4
gpt4 key购买 nike

我有两个简单的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com