无法在ActiveMQ中使用来自嵌入式远程代理的消息

时间:2012-10-29 18:00:07

标签: java activemq remote-access messagebroker

我是一名ActiveMQ初学者。我的主要看起来像这样:

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();

}

我启动此应用程序两次,一次是生产者,一次是消费者。 当我启动使用者时,它会卡在broker.start()行上。

我缺少什么?!

1 个答案:

答案 0 :(得分:4)

基本上你启动一次代理(将它嵌入到jvm中)。

BrokerService broker = new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();

然后连接到代理(在使用者和生产者应用程序中都需要此代码):

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

然后设置消费者

Destination queue = session.createQueue("queuename");
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(new MessageConsumer());

设置制作人并发送消息

MessageProducer producer = session.createProducer(queue);
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setObject(object);
producer.send(objectMessage);

例如:http://jmsexample.zcage.com/index2.html

http://svn.apache.org/repos/asf/activemq/branches/activemq-5.6/assembly/src/release/example/src/