Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to receive messages from embedded broker within same message?(ActiveMQ)
    text
    copied!<p>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 <code>TCP Transport</code>, but I found I can not use <code>VM Transport</code> 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? </p> <p>Broker config with Spring</p> <pre><code>&lt;amq:broker id="myBroker" brokerName="myBroker"&gt; &lt;amq:transportConnectors&gt; &lt;amq:transportConnector uri="tcp://localhost:7777" /&gt; &lt;amq:transportConnector uri="vm://myBroker" /&gt; &lt;/amq:transportConnectors&gt; &lt;/amq:broker&gt; </code></pre> <p>Consumer </p> <pre><code>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; } } </code></pre> <p>Listener</p> <pre><code>public class Listener implements MessageListener { public void onMessage(Message message) { try { //do something here System.out.println(message); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>In Main</p> <pre><code>TestConsumer consumer = new TestConsumer(); Destination destination = consumer.getSession().createQueue("TESTQUEUE"); MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination); messageConsumer.setMessageListener(new Listener()); </code></pre> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload