簡體   English   中英

如何從另一個應用程序使用 ActiveMQ Artemis jms 主題 Java

[英]How to consume ActiveMQ Artemis jms topic from another application Java

我有一種情況,如果我從應用程序中即時呼叫我的消費者,它可以使用下面的代碼。 我如何從不同的應用程序接收此消息? 工作代碼如下:

public class TopicExample {
    public static void main(final String[] args) throws Exception {
        Connection connection = null;
        InitialContext initialContext = null;
        try {
            initialContext = new InitialContext();
            Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
            connection = cf.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(topic);
            MessageConsumer messageConsumer = session.createConsumer(topic);
            TextMessage message = session.createTextMessage("This is a text message ");
            producer.send(message);
            System.out.println("Sent message: " + message.getText());
            connection.start();
            System.out.println("*************************************************************");
            TextMessage messageReceived = (TextMessage) messageConsumer.receive(1000);
            System.out.println("messageConsumer received: " + messageReceived.getText());
            messageConsumer.close();
        } finally {
            if (initialContext != null) {
                initialContext.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}

我正在使用相同的 artemis 代理,如果我檢查消費者和生產者顯示的控制台,但消息始終為 null with TextMessage messageReceived = (TextMessage) messageConsumer.receive(1000); 如果我使用MessageConsumer messageConsumer = session.createConsumer(topic); 它永遠不會去那里。 我在不同應用程序中不起作用的代碼如下:

public class ExampleSubscriber {
    public static void main(String[] args) {
        Connection connection = null;
        InitialContext initialContext = null;
        try {
            initialContext = new InitialContext();
            Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
            connection = cf.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageConsumer messageConsumer = session.createConsumer(topic);
           // messageConsumer.setMessageListener(new DefaultMessageListener()); does not work
           // TextMessage messageReceived = (TextMessage) messageConsumer.receive(1000); 
           // System.out.println(messageReceived); if i dont use listener this is always null
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            if (initialContext != null) {
                try {
                    initialContext.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private static class DefaultMessageListener implements MessageListener {
        @Override
        public void onMessage(Message message) {
            System.out.println(message); //never comes here when using listener
        }
    }
}

消費者應用程序需要啟動JMS 連接以接收傳入消息,即

connection.start();

您必須確保在使用者主動偵聽消息將消息發送到 JMS 主題。 正如 JMS 規范所定義的,JMS 主題使用者只有在連接時才會收到發送到主題的消息。 唯一的例外是如果消費者已經創建了持久訂閱,但您沒有這樣做。

謝謝大家,我嘗試了您的所有建議,但出於某種奇怪的原因,它沒有用。 經過整整 2 晚的研究它只在我使用 jms 2.0 實現時有效

    try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
         JMSContext jmsContext = cf.createContext()) {
    //add your consumer code here for consumer
    //add your producer code here for producer
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM