ActiveMQ and embedded broker

谁说胖子不能爱 提交于 2019-12-03 00:04:07

You can embed a broker into your code in a number of ways, much of which is documented here. You may want to try upgrading you version since what you are using appears to be quite old as it defaulting to the now deprecated AMQ Store instead of the newer KahaDB store. You might be having issues because of a race between the client threads as they use the different connection factories which could race to create in VM brokers. If you set the create=false option on the producer and ensure the consumer thread starts after that could address the issue, or you could create the VM broker ahead of time and the add create=false to both thread's and that might do the trick.

BrokerService broker = new BrokerService();
// configure the broker
broker.setBrokerName("localhost");
broker.setUseJmx(false);
broker.start();

And then in the client code just attach via this connection factory configuration.

ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");

When I run your code, I got the below exception:

javax.jms.JMSException: Could not connect to broker URL: tcp://localhost. 
Reason java.lang.IllegalArgumentException: port out of range:-1

Your broker is running and listening to port 61616, so any client which tries to connect to broker need to have the port in its URL.

The client code tries to connect to localhost but doesn't mention the port to which it has to connect. Both the producer and consumer code needs to be fixed.

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost");

To

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

After fixing the port, I was able to run your code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!