Вопрос

I am an ActiveMQ begginer. 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 a a producer and once as a consumer. When I start the consumer, it gets stuck on the broker.start() line.

What am I missing?!

Это было полезно?

Решение

Basicly you start the broker once (embedding it into a 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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top