简体   繁体   中英

how to exchange messages between two java applications via Spring JMS?

I have two simple Spring applications acting as sender and receiver (code below). Although following this guide 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?

Sender application

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

Receiver application

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

Actually "NO". Since you use JMS aplications don't have to find each other on the network .

It is a JMS, so they both just should know a Broker URL to connect, as for sending part as well as for receiving.

You just need to configure application.properties correctly:

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

And continue to use the same @EnableAutoConfiguration on both sides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM