Rabbitmq java примеры использования

Rabbitmq java примеры использования

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.

Читайте также:  Cannot setup python sdk pycharm

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 Spring AMQP)

In this part of the tutorial we’ll write two programs using the spring-amqp library; 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 Spring AMQP 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=

Spring Boot offers numerous features but we will only highlight a few here. First, Spring Boot applications have the option of providing their properties through either an application.properties or application.yml file (there are many more options as well but this will get us going). You’ll find an application.properties file in the generated project with nothing in it. Rename application.properties to application.yml file with the following properties:

spring: profiles: active: usage_message logging: level: org: ERROR tutorial: client: duration: 10000

Create a new package tut1 where we can put the tutorial code. We’ll now create a Java configuration file Tut1Config.java to describe our Spring beans in the following manner:

package org.springframework.amqp.tutorials.tut1; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Profile() @Configuration public class Tut1Config < @Bean public Queue hello() < return new Queue("hello"); >@Profile("receiver") @Bean public Tut1Receiver receiver() < return new Tut1Receiver(); >@Profile("sender") @Bean public Tut1Sender sender() < return new Tut1Sender(); >>

Note that we’ve defined the first tutorial profile as either tut1 , the package name, or hello-world . We use the @Configuration annotation to let Spring know that this is a Java Configuration and in it we create the definition for our Queue («hello») and define our Sender and Receiver beans.

We will run all of our tutorials through the Boot Application now by simply passing in which profiles we are using. To enable this we will modify the generated RabbitAmqpTutorialsApplication class with the following:

import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class RabbitAmqpTutorialsApplication < @Profile("usage_message") @Bean public CommandLineRunner usage() < return args ->< System.out.println("This app uses Spring Profiles to control its behavior.\n"); System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender"); >; > @Profile("!usage_message") @Bean public CommandLineRunner tutorial() < return new RabbitAmqpTutorialsRunner(); >public static void main(String[] args) throws Exception < SpringApplication.run(RabbitAmqpTutorialsApplication.class, args); >>

and add the RabbitAmqpTutorialsRunner class as follows:

package org.springframework.amqp.tutorials.tut1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.context.ConfigurableApplicationContext; public class RabbitAmqpTutorialsRunner implements CommandLineRunner < @Value("$") private int duration; @Autowired private ConfigurableApplicationContext ctx; @Override public void run(String. arg0) throws Exception < System.out.println("Ready . running for " + duration + "ms"); Thread.sleep(duration); ctx.close(); >>

Sending

(P) - data-lazy-src=

You’ll notice that Spring AMQP removes the boilerplate code leaving you with only the logic of the messaging to be concerned about. We autowire in the queue that was configured in our bean definition in the Tut1Config class and like many spring connection abstractions, we wrap the boilerplate RabbitMQ client classes with a RabbitTemplate that can be autowired into the sender. All that is left is to create a message and invoke the template’s convertAndSend method passing in the queue name from the bean we defined and the message we just created.

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

The receiver is equally simple. We annotate our receiver class with @RabbitListener and pass in the name of the queue. We then annotate our receive method with @RabbitHandler passing in the payload that has been pushed to the queue.

package org.springframework.amqp.tutorials.tut1; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; @RabbitListener(queues = "hello") public class Tut1Receiver < @RabbitHandler public void receive(String in) < System.out.println(" [x] Received '" + in + "'"); >>

Putting it all together

We must now build the JAR file:

The application uses Spring Profiles to control what tutorial it’s running, and whether it’s a sender or receiver. To run the receiver, execute the following command:

# consumer java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=hello-world,receiver

Open another shell to run the sender:

# sender java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=hello-world,sender

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.

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

Источник

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