简体   繁体   English

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

[英]What is the best way to receive messages from embedded broker within same message?(ActiveMQ)

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.当我使用TCP Transport时它工作,但我发现当代理和消费者在同一个应用程序中时我不能使用VM Transport (It works if I create another process for consumer) Is there a better way to do in my situation? (如果我为消费者创建另一个流程,它会起作用)在我的情况下有更好的方法吗?

Broker config with Spring使用 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.当 brokerURL 为 "tcp:localhost:7777" 或 "vm://myBroker" 但 Broker 和 Consumer 处于不同的进程时,它会起作用。 I just can not use VM Transport when the two are in the same application.当两者在同一个应用程序中时,我无法使用 VM Transport。

Try to use VM trnasport. 尝试使用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传输是最佳选择:它甚至不会退出您的JVM并使用直接API调用来传输消息。

See this article for details. 有关详细信息,请参见本文。 http://activemq.apache.org/configuring-transports.html http://activemq.apache.org/configuring-transports.html

I would like to know if we can browse a message like when the msg reaches active mq and and when the msg goes out of activemq.我想知道我们是否可以浏览消息,例如 msg 何时到达 active mq 以及 msg 何时离开 activemq。 Its an embedded broker它是一个嵌入式经纪人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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