【问题标题】:What is the best way to receive messages from embedded broker within same message?(ActiveMQ)在同一条消息中从嵌入式代理接收消息的最佳方式是什么?(ActiveMQ)
【发布时间】:2012-10-06 09:31:23
【问题描述】:

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

使用 Spring 进行代理配置

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

消费者

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;
    }

}

听众

public class Listener implements MessageListener {

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

}

在主要

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

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

【问题讨论】:

    标签: java activemq


    【解决方案1】:

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

    详情请参阅这篇文章。 http://activemq.apache.org/configuring-transports.html

    【讨论】:

    • 我完全同意。但是当将它们全部放入同一个应用程序时,我无法使其正常工作。将在几分钟内发布我的代码。
    • 顺便说一下,消息是从外部发送的。会有所不同吗?
    • 感谢回复,终于发现自己犯了一些错误。 VM传输实际上最适合我的情况。它现在可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多