This section covers how to connect to ActiveMQ using the Java programming language.
This example runs a producer, that sends a fixed number of messages to a queue. Second component is a listener, that receives messages and output received messages
Use the following (Linux) commands to run listener and producer. Adapt if you use Windows.
cd clients/simple-listener
mvn clean package
java -jar target/simple-listener.jarcd clients/simple-producer
mvn clean package
java -jar target/simple-producer.jar- Run both apps
- Change send message
- Make number of messages/queue name configurable via app properties
This example will show you, how a multitude of consumers can connect to a queue. Every message is delivered to exactly one consumer. So start single instance and compile simple listener like so:
cd clients/simple-listener
mvn clean packageNow open three command prompts and run in each:
java -jar target/simple-listener.jarYour setup has now a broker with three connected clients. You can either use admin console or simple producer to create messages.
Once you send messages, you should see an output like this:

- Run setup
- Run simple producer and observe output
- Shutdown all listeners and let producer send more messages. Restart a listener.
- Reduce wait time in producer loop and observe how fast your setup can deliver messages
- Bonus Challenge: Run replica setup and connect three listeners. Run producer and shutdown one instance.
In some scenarios message shall be received by every subscribed client. This communication model can be achieved by using topics. As always you need a running broker, so start single instance.
cd clients/topic-listener
mvn clean packageNow open multiple command prompts and run in each:
java -jar target/topic-listener.jarOnce you send messages, you should see an output like this:

- Run setup and observe output
- Shutdown all listeners and let producer send more messages. Restart a listener.
- Bonus Challenge: Run replica setup and connect three listeners. Run producer and shutdown one instance.
A quite popular format for sending data is JSON. It is especially useful, when exchanging messages between multiple programming technologies. It also (fairly) human readable.
As always you need a running broker, so start single instance.
Now run producer/listener
cd clients/json-listener
mvn clean package
java -jar target/json-listener.jarcd clients/json-producer
mvn clean package
java -jar target/json-producer.jar- Run example
- Try to send a JSON message from admin console
- Change producer to send a list of StudentResult, adapt listener to parse list.
- Create a CSV file that holds a list of student results. Read from this list and send all entries. Hint
- Look at StudentResult Java class - any thoughts, why example approach will not work well?
TODO
TODO
ActiveMQ ships with a powerful API called Jolokia. With that API almost everything in a running broker instance can be manipulated. This example shows how to connect to this API and some usage examples.
Start hello world example omitting producer client. Then execute following commands:
cd clients/jolokia-client
mvn clean package
java -jar target/jolokia-client.jar- Run example
- Add a method to list all topics
- Add a method, that gets total message count, see here for help
- Bonus challenge: Parse Jolokia output into Java objects
This example shows you, how to send messages using HTTP(s) as transport protocol- NOTE: There are many much better ways to achieve this. See sections Jolokia API or Camel for better approaches.
Start hello world example omitting producer client. Then execute following commands:
cd clients/http-producer
mvn clean package
java -jar target/http-producer.jarThis example sends messages via a http tunnel. Next to the drawbacks of this protocol, note, that some pretty outdated libraries are necessary to run this example.
- Run example
- Run example using either replica or master/slave setup. Shutdown one of the instances and observe output.