Rabbitmq java basic consume

Rabbitmq java basic consume

This tutorial assumes RabbitMQ is installed and running on localhost on the standard port ( 5672 ). In case you use a different host, port or credentials, connections settings would require adjusting.

Where to get help

If you’re having trouble going through this tutorial you can contact us through the mailing list or RabbitMQ community Slack.

RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that the letter carrier will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office, and a letter carrier.

Читайте также:  File Upload Form

The major difference between RabbitMQ and the post office is that it doesn’t deal with paper, instead it accepts, stores, and forwards binary blobs of data ‒ messages.

RabbitMQ, and messaging in general, uses some jargon.

    Producing means nothing more than sending. A program that sends messages is a producer :

Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don’t. An application can be both a producer and consumer, too.

«Hello World»

(using the Java Client)

In this part of the tutorial we’ll write two programs in Java; a producer that sends a single message, and a consumer that receives messages and prints them out. We’ll gloss over some of the detail in the Java API, concentrating on this very simple thing just to get started. It’s a «Hello World» of messaging.

In the diagram below, «P» is our producer and «C» is our consumer. The box in the middle is a queue — a message buffer that RabbitMQ keeps on behalf of the consumer.

(P) - data-lazy-src=

Declaring a queue is idempotent — it will only be created if it doesn’t exist already. The message content is a byte array, so you can encode whatever you like there.

Sending doesn’t work!

If this is your first time using RabbitMQ and you don’t see the «Sent» message then you may be left scratching your head wondering what could be wrong. Maybe the broker was started without enough free disk space (by default it needs at least 200 MB free) and is therefore refusing to accept messages. Check the broker logfile to confirm and reduce the limit if necessary. The configuration file documentation will show you how to set disk_free_limit .

Receiving

That’s it for our publisher. Our consumer listens for messages from RabbitMQ, so unlike the publisher which publishes a single message, we’ll keep the consumer running to listen for messages and print them out.

[|||] - data-lazy-src=

Putting it all together

You can compile both of these with just the RabbitMQ java client on the classpath:

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

To run them, you’ll need rabbitmq-client.jar and its dependencies on the classpath. In a terminal, run the consumer (receiver):

java -cp .:amqp-client-5.16.0.jar:slf4j-api-1.7.36.jar:slf4j-simple-1.7.36.jar Recv

then, run the publisher (sender):

java -cp .:amqp-client-5.16.0.jar:slf4j-api-1.7.36.jar:slf4j-simple-1.7.36.jar Send

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

The consumer will print the message it gets from the publisher via RabbitMQ. The consumer will keep running, waiting for messages (Use Ctrl-C to stop it), so try running the publisher from another terminal.

Listing queues

You may wish to see what queues RabbitMQ has and how many messages are in them. You can do it (as a privileged user) using the rabbitmqctl tool:

sudo rabbitmqctl list_queues
rabbitmqctl.bat list_queues

Time to move on to part 2 and build a simple work queue.

Hint

To save typing, you can set an environment variable for the classpath e.g.

export CP=.:amqp-client-5.16.0.jar:slf4j-api-1.7.36.jar:slf4j-simple-1.7.36.jar java -cp $CP Send
set CP=.;amqp-client-5.16.0.jar;slf4j-api-1.7.36.jar;slf4j-simple-1.7.36.jar java -cp %CP% Send

Production [Non-]Suitability Disclaimer

Please keep in mind that this and other tutorials are, well, tutorials. They demonstrate one new concept at a time and may intentionally oversimplify some things and leave out others. For example topics such as connection management, error handling, connection recovery, concurrency and metric collection are largely omitted for the sake of brevity. Such simplified code should not be considered production ready.

Please take a look at the rest of the documentation before going live with your app. We particularly recommend the following guides: Publisher Confirms and Consumer Acknowledgements, Production Checklist and Monitoring.

Getting Help and Providing Feedback

If you have questions about the contents of this tutorial or any other topic related to RabbitMQ, don’t hesitate to ask them on the RabbitMQ mailing list.

Help Us Improve the Docs

If you’d like to contribute an improvement to the site, its source is available on GitHub. Simply fork the repository and submit a pull request. Thank you!

1 «Hello World!»

The simplest thing that does something

(P) - data-lazy-src=

Sending messages to many consumers at once

Producer - data-lazy-src=

Receiving messages based on a pattern (topics)

Producer - data-lazy-src=

Reliable publishing with publisher confirms

Источник

Оцените статью