在同一消息中从嵌入式代理接收消息的最佳方法是什么?(ActiveMQ)

时间:2021-10-09 09:49:58

Currently there is a embedded broker running in my application, and I want to consume the queue within the same application in different thread. It works when I use TCP Transport, but I found I can not use VM Transport when the broker and the consumer in the same application. (It works if I create another process for consumer) Is there a better way to do in my situation?

目前在我的应用程序中运行了一个嵌入式代理,我想在不同的线程中使用相同应用程序中的队列。它在我使用TCP传输时有效,但我发现当代理和消费者在同一个应用程序中时我不能使用VM传输。 (如果我为消费者创建另一个流程,它可以工作)在我的情况下有更好的方法吗?

Broker config with Spring

Broker配置Spring

<amq:broker  id="myBroker" brokerName="myBroker">       
    <amq:transportConnectors>    
        <amq:transportConnector uri="tcp://localhost:7777" />
        <amq:transportConnector uri="vm://myBroker" />
    </amq:transportConnectors>
</amq:broker>

Consumer

public class TestConsumer {
    private static String brokerURL = "tcp://localhost:7777";
    private static transient ConnectionFactory factory;
    private transient Connection connection;
    private transient Session session;

    public TestConsumer() throws JMSException {
        factory = new ActiveMQConnectionFactory(brokerURL);
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    public void close() throws JMSException {
        if (connection != null) {
            connection.close();
        }
    }               
    public Session getSession() {
        return session;
    }

}

Listener

public class Listener implements MessageListener {

    public void onMessage(Message message) {
        try {
            //do something here
            System.out.println(message);
        } catch (Exception e) {
            e.printStackTrace();
        }           
    }

}

In Main

TestConsumer consumer = new TestConsumer();
Destination destination = consumer.getSession().createQueue("TESTQUEUE");
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
messageConsumer.setMessageListener(new Listener());

It works when brokerURL is "tcp:localhost:7777" or is "vm://myBroker" but Broker and Consumer are in different processes. I just can not use VM Transport when the two are in the same application.

当brokerURL是“tcp:localhost:7777”或者是“vm:// myBroker”时它可以工作,但Broker和Consumer处于不同的进程中。当两者在同一个应用程序中时,我就无法使用VM Transport。

1 个解决方案

#1


0  

Try to use VM trnasport. If I understand you correctly your application sends and receives messages itself. In this case VM transport is the best choice: it even does not exit your JVM and transfers messages using direct API calls.

尝试使用VM trnasport。如果我理解正确,您的应用程序本身就会发送和接收消息。在这种情况下,VM传输是最佳选择:它甚至不会退出JVM并使用直接API调用传输消息。

See this article for details. http://activemq.apache.org/configuring-transports.html

有关详细信息,请参阅此文http://activemq.apache.org/configuring-transports.html

#1


0  

Try to use VM trnasport. If I understand you correctly your application sends and receives messages itself. In this case VM transport is the best choice: it even does not exit your JVM and transfers messages using direct API calls.

尝试使用VM trnasport。如果我理解正确,您的应用程序本身就会发送和接收消息。在这种情况下,VM传输是最佳选择:它甚至不会退出JVM并使用直接API调用传输消息。

See this article for details. http://activemq.apache.org/configuring-transports.html

有关详细信息,请参阅此文http://activemq.apache.org/configuring-transports.html