[英]Unable to consume messages from an embedded remote broker in ActiveMQ
I am an ActiveMQ begginer. 我是一名ActiveMQ初学者。 My main looks like this:
我的主要看起来像这样:
public static void main(String[] args) throws Exception {
BrokerService broker = new BrokerService();
if(isProducer(args)){
broker.addConnector("tcp://localhost:8001");
broker.start();
// start producer...
}
else{
broker.addConnector("tcp://localhost:9000");
broker.addNetworkConnector("static:(tcp://localhost:8001)");
broker.start(); // Getting stuck here!!!
// start consumer
}
waitForever();
} }
I start this application twice, once aa producer and once as a consumer. 我开始这个应用程序两次,一次是生产者,一次是消费者。 When I start the consumer, it gets stuck on the broker.start() line.
当我启动使用者时,它会卡在broker.start()行上。
What am I missing?! 我错过了什么?!
Basicly you start the broker once (embedding it into a jvm). 基本上你启动一次代理(将它嵌入到jvm中)。
BrokerService broker = new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
Then you connect to the broker (this code is needed in both consumer and producer application): 然后连接到代理(在使用者和生产者应用程序中都需要此代码):
url = "vm://localhost:61616" //if you are in same jvm
url2 = "tcp://localhost:61616" //if you are in diff jvm or other host
connectionFactory = new ActiveMQConnectionFactory(username,password,url);
connection = (ActiveMQConnection) connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, ackMode);
Then setup a consumer 然后设置一个消费者
Destination queue = session.createQueue("queuename");
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(new MessageConsumer());
Setup a producer and send a message 设置生产者并发送消息
MessageProducer producer = session.createProducer(queue);
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setObject(object);
producer.send(objectMessage);
look at for example: http://jmsexample.zcage.com/index2.html 例如: http : //jmsexample.zcage.com/index2.html
or http://svn.apache.org/repos/asf/activemq/branches/activemq-5.6/assembly/src/release/example/src/ 或http://svn.apache.org/repos/asf/activemq/branches/activemq-5.6/assembly/src/release/example/src/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.