|
| 1 | +# Java code for RabbitMQ tutorials (AMQP 1.0 client) |
| 2 | + |
| 3 | +Here you can find Java examples for the [RabbitMQ tutorials](https://www.rabbitmq.com/getstarted.html) using the [RabbitMQ AMQP 1.0 Java client](https://rabbitmq.github.io/rabbitmq-amqp-java-client/stable/htmlsingle/). |
| 4 | + |
| 5 | +You need a RabbitMQ node running locally. The client requires **RabbitMQ 4.0 or newer** with AMQP 1.0 (enabled by default on port **5672**). The examples use the default `guest` user. See also the [AMQP 1.0 client libraries overview](https://www.rabbitmq.com/client-libraries/amqp-client-libraries). |
| 6 | + |
| 7 | +This directory is a self-contained [Maven](https://maven.apache.org/) project. Use `./mvnw` on Unix-like systems or `mvnw.cmd` on Windows. |
| 8 | + |
| 9 | +Run integration tests with `./mvnw test` (they expect a broker on `localhost`). |
| 10 | + |
| 11 | +## Code |
| 12 | + |
| 13 | +#### Tutorial one: "Hello World!" |
| 14 | + |
| 15 | +```shell |
| 16 | +# terminal tab 1 |
| 17 | +./mvnw compile exec:java -Dexec.mainClass=Recv |
| 18 | + |
| 19 | +# terminal tab 2 |
| 20 | +./mvnw compile exec:java -Dexec.mainClass=Send |
| 21 | +``` |
| 22 | + |
| 23 | +#### Tutorial two: Work Queues |
| 24 | + |
| 25 | +```shell |
| 26 | +# terminal tab 1 |
| 27 | +./mvnw compile exec:java -Dexec.mainClass=Worker |
| 28 | + |
| 29 | +# terminal tab 2 |
| 30 | +./mvnw compile exec:java -Dexec.mainClass=Worker |
| 31 | + |
| 32 | +# terminal tab 3 |
| 33 | +./mvnw compile exec:java -Dexec.mainClass=NewTask -Dexec.args="First Message" |
| 34 | +./mvnw compile exec:java -Dexec.mainClass=NewTask -Dexec.args="Second Message" |
| 35 | +./mvnw compile exec:java -Dexec.mainClass=NewTask -Dexec.args="Third Message" |
| 36 | +./mvnw compile exec:java -Dexec.mainClass=NewTask -Dexec.args="Fourth Message" |
| 37 | +./mvnw compile exec:java -Dexec.mainClass=NewTask -Dexec.args="Fifth Message" |
| 38 | +``` |
| 39 | + |
| 40 | +#### Tutorial three: Publish/Subscribe |
| 41 | + |
| 42 | +```shell |
| 43 | +# terminal tab 1 |
| 44 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogs |
| 45 | + |
| 46 | +# terminal tab 2 |
| 47 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogs |
| 48 | + |
| 49 | +# terminal tab 3 |
| 50 | +./mvnw compile exec:java -Dexec.mainClass=EmitLog |
| 51 | +``` |
| 52 | + |
| 53 | +#### Tutorial four: Routing |
| 54 | + |
| 55 | +```shell |
| 56 | +# terminal tab 1 |
| 57 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogsDirect -Dexec.args="warning error" |
| 58 | + |
| 59 | +# terminal tab 2 |
| 60 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogsDirect -Dexec.args="info warning error" |
| 61 | + |
| 62 | +# terminal tab 3 |
| 63 | +./mvnw compile exec:java -Dexec.mainClass=EmitLogDirect -Dexec.args="info Run. Run. Or it will explode." |
| 64 | +./mvnw compile exec:java -Dexec.mainClass=EmitLogDirect -Dexec.args="warning Run. Run. Or it will explode." |
| 65 | +./mvnw compile exec:java -Dexec.mainClass=EmitLogDirect -Dexec.args="error Run. Run. Or it will explode." |
| 66 | +``` |
| 67 | + |
| 68 | +#### Tutorial five: Topics |
| 69 | + |
| 70 | +```shell |
| 71 | +# terminal tab 1 |
| 72 | +# To receive all the logs: |
| 73 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogsTopic -Dexec.args="#" |
| 74 | + |
| 75 | +# To receive all logs from the facility "kern": |
| 76 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogsTopic -Dexec.args="kern.*" |
| 77 | + |
| 78 | +# Or if you want to hear only about "critical" logs: |
| 79 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogsTopic -Dexec.args="*.critical" |
| 80 | + |
| 81 | +# You can create multiple bindings: |
| 82 | +./mvnw compile exec:java -Dexec.mainClass=ReceiveLogsTopic -Dexec.args="kern.* *.critical" |
| 83 | + |
| 84 | +# terminal tab 2 |
| 85 | +# And to emit a log with a routing key "kern.critical" type: |
| 86 | +./mvnw compile exec:java -Dexec.mainClass=EmitLogTopic -Dexec.args="kern.critical A critical kernel error" |
| 87 | +``` |
| 88 | + |
| 89 | +#### Tutorial six: RPC |
| 90 | + |
| 91 | +```shell |
| 92 | +# terminal tab 1 |
| 93 | +./mvnw compile exec:java -Dexec.mainClass=RPCServer |
| 94 | + |
| 95 | +# terminal tab 2 |
| 96 | +./mvnw compile exec:java -Dexec.mainClass=RPCClient |
| 97 | +``` |
| 98 | + |
| 99 | +#### Tutorial seven: Publisher Confirms |
| 100 | + |
| 101 | +```shell |
| 102 | +./mvnw compile exec:java -Dexec.mainClass=PublisherConfirms |
| 103 | +``` |
0 commit comments