4

I am working with ActiveMQ and I create producer and a consumer for some messages.

This way I create the connection, and I create the destination:

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(username,password,"tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("MyQueue");

This way I create the producer and send the message:

Producer producer = session.createProducer(destination);
producer.send(msgToSend);

I create the consumer and I set to it a listener (the class that implements MessageListener interface)

Consumer consumer = session.createConsumer(destination);
consumer.setMessageListener(this);

The consummer connects to the destination and it is listening the messages. When it gets some messages from the queue "MyQueue", then onMessage() method from MessageListener is triggered and do whatever I want with the message.

My code work and I am able to produce and consume message. The producer is on a server and the consumer is on a client (a separate project).

To make it work, I installed the apache-activemq-5.14.4-bin.zip from here. and I put the dependency in the pom.xml:

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.14.0</version>
</dependency> 

Well, the ActiveMQ now installed is a service that starts when the computer starts. I want not to install it as a service but start it and stop it programmatically in java. For exampe, to press Start button and execute the code to start it and pres Stop and stop it.

Can I achieve programmatically and not installing ActiveMQ as a service?

Nelly Junior
  • 556
  • 2
  • 10
  • 30

2 Answers2

2

Please follow the link to start and stop the broker programatically http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html

LONGHORN007
  • 526
  • 9
  • 24
1

Yes, you can use ActiveMQ programmatically (for example in tests). Here are some details: http://activemq.apache.org/how-to-unit-test-jms-code.html

Vyacheslav Enis
  • 1,676
  • 12
  • 17