views:

1117

answers:

3

I'm investigating using ActiveMQ as an embedded in-process message queue in my application, but I'm a bit stuck on how I go about starting such an application up. I envision it like so (pseudocode, of course):

configureBroker ()
broker.start ()

createProducer (broker)
producer.start ()

for each desired consumer
    createConsumer (broker)
    consumer.start ()

waitForSignal ()
signalProducerShutdown ()

waitForEmptyQueues ()
signalConsumerShutdown ()

broker.stop ()

I've tried to assemble a simple version of this, but I'm stuck on how to write the producers and consumers in such a way as to have them work forever, or until told to quit. What is the best way to do this? I'm speaking specifically about the threading aspect; what do I need/want to spawn off in its own thread, etc...

I'm completely new to message queue based applications, so please be verbose with your examples.

A: 

Check out the Objectware Community wiki for info on how achieve this

flalar
Since their comment on this is "TODO: Explain technical implementation of processHelper and jmsHelper.", I don't think that they have the information I want.
Chris R
A: 

Hey, I'm also interested! Would be great if anybody would share some info on starting ActiveMQ broker within Java app.

raflik
+1  A: 

When you specify the ActiveMQConnectionFactory, you can specify "vm://" where name is the intra-vm specific name of your broker and it will start the broker within the VM.

For example,

String broker = "vm://stackOverflowTest";
ActiveMQConnectionFactory connectionFactory = 
        new ActiveMQConnectionFactory(broker);
Connection amqcon = connectionFactory.createConnection();
amqcon.start();

From there you can create your producers or consumers the same as if it was over the network. As long as you use the same name for the broker, you can have multiple threads / code open / talk to the same VM instance.

This solution only allows communication with the VM, it does not open any external ports. I'm assuming this is what you were looking for since you said you wanted embedded, in-process queues.

Steven Mastandrea