如何通过SpringJMS在两个java应用程序之间交换消息?
我有两个简单的Spring应用程序作为发送方和接收方(代码如下)。虽然我可以在同一个应用程序中发送和接收消息,但我找不到如何使它在两个独立的应用程序之间工作。我认为应该有一种方法来配置这两个应用程序,以便在网络上找到对方。如何使用Spring配置实现这一点 发件人应用程序如何通过SpringJMS在两个java应用程序之间交换消息?,java,spring,jms,spring-jms,Java,Spring,Jms,Spring Jms,我有两个简单的Spring应用程序作为发送方和接收方(代码如下)。虽然我可以在同一个应用程序中发送和接收消息,但我找不到如何使它在两个独立的应用程序之间工作。我认为应该有一种方法来配置这两个应用程序,以便在网络上找到对方。如何使用Spring配置实现这一点 发件人应用程序 @Configuration @EnableAutoConfiguration public class Application { static String mailboxDestination = "mailbox-
@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"));
}
}
接收器应用程序
@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"));
}
}
@配置
@启用自动配置
公共类应用程序
{
静态字符串mailboxDestination=“邮箱目的地”;
@豆子
接受者{
返回新的接收器();
}
@豆子
MessageListenerAdapter适配器(接收器){
messageListener适配器messageListener
=新MessageListenerAdapter(接收方);
setDefaultListenerMethod(“receiveMessage”);
返回消息侦听器;
}
@豆子
SimpleMessageListenerContainer容器(MessageListenerAdapter messageListener,
连接工厂(连接工厂){
SimpleMessageListenerContainer容器=新SimpleMessageListenerContainer();
container.setMessageListener(messageListener);
container.setConnectionFactory(connectionFactory);
container.setDestinationName(mailboxDestination);
返回容器;
}
公共静态void main(字符串[]args){
//从上一次运行中清除所有ActiveMQ数据
递归删除(新文件(“activemq数据”);
//启动应用程序
ConfigurableApplicationContext上下文=SpringApplication.run(Application.class,args);
}
}
公共类接收器
{
@自动连线
可配置的应用程序上下文上下文;
/**
*收到消息后,打印出来,然后关闭应用程序。
*最后,清理所有ActiveMQ服务器内容。
*/
公共无效接收消息(字符串消息){
系统输出打印项次(“收到”);
试一试{
睡眠(10000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
context.close();
递归删除(新文件(“activemq数据”);
}
}
实际上是“否”。由于您使用JMS应用程序,因此不必在网络上相互查找
这是一个JMS,所以他们都应该知道一个Broker
URL来连接,以及发送部分和接收部分
您只需正确配置应用程序。属性
:
spring.activemq.broker-url=tcp://192.168.1.210:9876
spring.activemq.user=admin
spring.activemq.password=secret
并继续在两侧使用相同的@EnableAutoConfiguration
。我明白了。因此,我应该创建application.properties
文件并添加这些属性。我正在使用spring工具套件提供的嵌入式ActiveMQ,因此不确定如何配置这些属性。当然,您必须拥有real
ActiveMQ代理才能通过网络交换消息。