1

我是一个 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() 行上。

我错过了什么?!

4

1 回答 1

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/

于 2012-10-29T19:01:50.790 回答