Skip to content

Latest commit

 

History

History
132 lines (103 loc) · 5.03 KB

java-examples.md

File metadata and controls

132 lines (103 loc) · 5.03 KB

Java Examples

This section covers how to connect to ActiveMQ using the Java programming language.

Simple Listener/Producer

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.jar
cd clients/simple-producer
mvn clean package
java -jar target/simple-producer.jar

Tasks

  • Run both apps
  • Change send message
  • Make number of messages/queue name configurable via app properties

Queue Example

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 package

Now open three command prompts and run in each:

java -jar target/simple-listener.jar

Your 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: consumer output

Tasks

  • 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.

Topic Example

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 package

Now open multiple command prompts and run in each:

java -jar target/topic-listener.jar

Once you send messages, you should see an output like this: topic output

Tasks

  • 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.

Message Content: Send JSON

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.jar
cd clients/json-producer
mvn clean package
java -jar target/json-producer.jar

Tasks

  • 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?

Message Content: Send XML

TODO

Message Content: Serialized Java Objects

TODO

Using Jolokia API

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

Tasks

  • 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

Using HTTP as transport

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.jar

This 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.

Tasks

  • Run example
  • Run example using either replica or master/slave setup. Shutdown one of the instances and observe output.