簡體   English   中英

無法在ActiveMQ中使用來自嵌入式遠程代理的消息

[英]Unable to consume messages from an embedded remote broker in ActiveMQ

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

我錯過了什么?!

基本上你啟動一次代理(將它嵌入到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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM