rabbitmq-tutorials/java
2024-04-22 15:36:35 +02:00
..
EmitLog.java Use Enum instead of hard-corded String 2023-02-02 19:29:41 +09:00
EmitLogDirect.java Avoid ArrayIndexOutOfBoundsException in EmitLogDirect 2019-03-30 20:16:03 -04:00
EmitLogHeader.java Update Java tutorials to Java 8 2018-12-04 10:55:14 +01:00
EmitLogTopic.java Use Enum instead of hard-corded String 2023-02-02 19:29:41 +09:00
NewTask.java Update Java tutorials to Java 8 2018-12-04 10:55:14 +01:00
PublisherConfirms.java Update PublisherConfirms.java 2023-03-04 18:08:29 +08:00
README.md Bump Java client version to 5.21.0 2024-04-22 15:36:35 +02:00
ReceiveLogHeader.java Update Java tutorials to Java 8 2018-12-04 10:55:14 +01:00
ReceiveLogs.java Use Enum instead of hard-corded String 2023-02-02 19:29:41 +09:00
ReceiveLogsDirect.java Update Java tutorials to Java 8 2018-12-04 10:55:14 +01:00
ReceiveLogsTopic.java Use Enum instead of hard-corded String 2023-02-02 19:29:41 +09:00
recompile.sh Bump Java client version to 5.21.0 2024-04-22 15:36:35 +02:00
Recv.java Update Recv.java 2022-04-06 12:51:28 -04:00
RPCClient.java Simplified RPC examples 2022-09-20 19:28:25 +02:00
RPCServer.java Simplified RPC examples 2022-09-20 19:28:25 +02:00
Send.java import java.nio.charset.StandardCharsets 2019-11-14 19:16:27 +03:00
Worker.java Update Java tutorials to Java 8 2018-12-04 10:55:14 +01:00

Java code for RabbitMQ tutorials

Here you can find the Java code examples from RabbitMQ tutorials.

To successfully use the examples you will need a RabbitMQ node running locally.

Requirements

You'll need to download the following JAR files from Maven Central:

For example, with wget:

wget https://repo1.maven.org/maven2/com/rabbitmq/amqp-client/5.21.0/amqp-client-5.21.0.jar
wget https://repo1.maven.org/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.jar
wget https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/2.0.13/slf4j-simple-2.0.13.jar

Copy those files in your working directory, along the tutorials Java files.

To compile you only need the Rabbitmq Java client jar on the classpath. To run them you'll need all the dependencies, see examples below.

You can set an environment variable for the jar files on the classpath e.g.

export CP=.:amqp-client-5.21.0.jar:slf4j-api-2.0.13.jar:slf4j-simple-2.0.13.jar
java -cp $CP Send

On Windows, use a semicolon instead of a colon to separate items in the classpath:

set CP=.;amqp-client-5.21.0.jar;slf4j-api-2.0.13.jar;slf4j-simple-2.0.13.jar
java -cp %CP% Send

Code

Tutorial one: "Hello World!":

javac -cp amqp-client-5.21.0.jar Send.java Recv.java

# terminal tab 1
java -cp .:amqp-client-5.21.0.jar:slf4j-api-2.0.13.jar:slf4j-simple-2.0.13.jar Recv

# terminal tab 2
java -cp .:amqp-client-5.21.0.jar:slf4j-api-2.0.13.jar:slf4j-simple-2.0.13.jar Send

Tutorial two: Work Queues:

javac -cp $CP NewTask.java Worker.java

# terminal tab 1
java -cp $CP NewTask

# terminal tab 2
java -cp $CP Worker

Tutorial three: Publish/Subscribe

javac -cp $CP EmitLog.java ReceiveLogs.java

# terminal tab 1
java -cp $CP ReceiveLogs

# terminal tab 2
java -cp $CP EmitLog

Tutorial four: Routing

javac -cp $CP ReceiveLogsDirect.java EmitLogDirect.java

# terminal tab 1
java -cp $CP ReceiveLogsDirect warning error

# terminal tab 2
java -cp $CP ReceiveLogsDirect info warning error

# terminal tab 3
java -cp $CP EmitLogDirect error "Run. Run. Or it will explode."

Tutorial five: Topics

# To compile:
javac -cp $CP ReceiveLogsTopic.java EmitLogTopic.java

# To receive all the logs:
java -cp $CP ReceiveLogsTopic "#"

# To receive all logs from the facility "kern":
java -cp $CP ReceiveLogsTopic "kern.*"

# Or if you want to hear only about "critical" logs:
java -cp $CP ReceiveLogsTopic "*.critical"

# You can create multiple bindings:
java -cp $CP ReceiveLogsTopic "kern.*" "*.critical"

# And to emit a log with a routing key "kern.critical" type:
java -cp $CP EmitLogTopic "kern.critical" "A critical kernel error"

Tutorial six: RPC

# Compile and set up the classpath as usual (see tutorial one):
javac -cp $CP RPCClient.java RPCServer.java

# Our RPC service is now ready. We can start the server:
java -cp $CP RPCServer

# To request a fibonacci number run the client:
java -cp $CP RPCClient

Tutorial seven: Publisher Confirms

javac -cp $CP PublisherConfirms.java
java -cp $CP PublisherConfirms