JMS consume multiple topics

后端 未结 1 1857
遇见更好的自我
遇见更好的自我 2021-01-27 01:27

I am new to Java and working on a project that consumes multiple (different) topics and sends it to another server. I was wondering what the best way to handling multiple topics

1条回答
  •  爱一瞬间的悲伤
    2021-01-27 02:23

    The way I would go about it is to use the listener feature.

    Your object implements the MessageListener interface and then you add to the consumer your message listener. In this case the client library will handle the threading for you in reading the messages from the queue and despatching them to the listeners.

    import javax.jms.Connection;
    import javax.jms.Destination;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
    import javax.jms.Session;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class MyMessageConsumer implements MessageListener {
    
        public static void main() {
            try {
    
                MyMessageConsumer myMessageConsumer = new MyMessageConsumer();
    
                // This example is using the ActiveMQ client library
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("nio://localhost:61616");
                Connection connection = connectionFactory.createConnection();
                connection.start();
    
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    
                Destination destination1 = session.createQueue("MyTopic1");
                MessageConsumer consumer1 = session.createConsumer(destination1);
                consumer1.setMessageListener(myMessageConsumer);
    
                Destination destination2 = session.createQueue("MyTopic2");
                MessageConsumer consumer2 = session.createConsumer(destination2);
                consumer2.setMessageListener(myMessageConsumer);
    
            } catch (Exception e) {
                System.out.println("Caught: " + e);
                e.printStackTrace();
            }
        }
    
        @Override
        public void onMessage(Message message) {
            // Handle my messages here
        }
    }
    

    Session Transactions

    In this option, we use transacted messages and it will deliver the message if session.rollback() is called. You acknowledge() when your operation was successful or rollback() when it was not.

    package io.bessel.test;

    import javax.jms.Connection;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageListener;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class MyMessageConsumer implements MessageListener {
    
        public static void main(String ... arguments) {
            try {
                // This example is using the ActiveMQ client library
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("nio://localhost:61616");
                Connection connection = connectionFactory.createConnection();
                connection.start();
    
                Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    
                MyMessageConsumer myMessageConsumer = new MyMessageConsumer(session);
    
                Destination destination1 = session.createQueue("MyTopic1");
                MessageConsumer consumer1 = session.createConsumer(destination1);
                consumer1.setMessageListener(myMessageConsumer);
    
                Destination destination2 = session.createQueue("MyTopic2");
                MessageConsumer consumer2 = session.createConsumer(destination2);
                consumer2.setMessageListener(myMessageConsumer);
    
            } catch (Exception e) {
                System.out.println("Caught: " + e);
                e.printStackTrace();
            }
        }
    
        private final Session session;
    
        public MyMessageConsumer(Session session) {
            this.session = session;
        }
    
        public void onMessage(Message message) {
            if (message instanceof TextMessage) {
                try {
                    String text = ((TextMessage) message).getText();
                    System.out.println(String.format("Received message: %s", text));
                    this.session.rollback();
    
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题