Giter VIP home page Giter VIP logo

gs-messaging-rabbitmq's Introduction

tags projects
messaging
rabbitmq
stream
spring-amqp
spring-cloud-stream

This guide walks you through the process of setting up a RabbitMQ AMQP server that publishes and subscribes to messages.

What you’ll build

You’ll build an application that publishes a message using Spring AMQP’s RabbitTemplate and subscribes to the message on a POJO using MessageListenerAdapter.

Set up RabbitMQ broker

Before you can build your messaging application, you need to set up the server that will handle receiving and sending messages.

RabbitMQ is an AMQP server. The server is freely available at http://www.rabbitmq.com/download.html. You can download it manually, or if you are using a Mac with homebrew:

brew install rabbitmq

Unpack the server and launch it with default settings.

rabbitmq-server

You should see something like this:

            RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.
##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
##  ##
##########  Logs: /usr/local/var/log/rabbitmq/[email protected]
######  ##        /usr/local/var/log/rabbitmq/[email protected]
##########
            Starting broker... completed with 6 plugins.

You can also use Docker Compose to quickly launch a RabbitMQ server if you have docker running locally. There is a docker-compose.yml in the root of the "complete" project in Github. It is very simple:

docker-compose.yml

rabbitmq:
  image: rabbitmq:management
  ports:
    - "5672:5672"
    - "15672:15672"

With this file in the current directory you can run docker-compose up to get RabbitMQ running in a container.

Create a RabbitMQ message receiver

With any messaging-based application, you need to create a receiver that will respond to published messages.

src/main/java/hello/Receiver.java

link:complete/src/main/java/hello/Receiver.java[role=include]

The Receiver is a simple POJO that defines a method for receiving messages. When you register it to receive messages, you can name it anything you want.

Note
For convenience, this POJO also has a CountDownLatch. This allows it to signal that the message is received. This is something you are not likely to implement in a production application.

Register the listener and send a message

Spring AMQP’s RabbitTemplate provides everything you need to send and receive messages with RabbitMQ. Specifically, you need to configure:

  • A message listener container

  • Declare the queue, the exchange, and the binding between them

  • A component to send some messages to test the listener

Note
Spring Boot automatically creates a connection factory and a RabbitTemplate, reducing the amount of code you have to write.

You’ll use RabbitTemplate to send messages, and you will register a Receiver with the message listener container to receive messages. The connection factory drives both, allowing them to connect to the RabbitMQ server.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

The bean defined in the listenerAdapter() method is registered as a message listener in the container defined in container(). It will listen for messages on the "spring-boot" queue. Because the Receiver class is a POJO, it needs to be wrapped in the MessageListenerAdapter, where you specify it to invoke receiveMessage.

Note
JMS queues and AMQP queues have different semantics. For example, JMS sends queued messages to only one consumer. While AMQP queues do the same thing, AMQP producers don’t send messages directly to queues. Instead, a message is sent to an exchange, which can go to a single queue, or fanout to multiple queues, emulating the concept of JMS topics. For more, see Understanding AMQP.

The message listener container and receiver beans are all you need to listen for messages. To send a message, you also need a Rabbit template.

The queue() method creates an AMQP queue. The exchange() method creates a topic exchange. The binding() method binds these two together, defining the behavior that occurs when RabbitTemplate publishes to an exchange.

Note
Spring AMQP requires that the Queue, the TopicExchange, and the Binding be declared as top level Spring beans in order to be set up properly.

Send a Test Message

Test messages are sent by a CommandLineRunner, which also waits for the latch in the receiver and closes the application context:

src/main/java/hello/Runner.java

link:complete/src/main/java/hello/Runner.java[role=include]

The runner can be mocked out in tests, so that the receiver can be tested in isolation.

Run the Application

The main() method starts that process by creating a Spring application context. This starts the message listener container, which will start listening for messages. There is a Runner bean which is then automatically executed: it retrieves the RabbitTemplate from the application context and sends a "Hello from RabbitMQ!" message on the "spring-boot" queue. Finally, it closes the Spring application context and the application ends.

You should see the following output:

Sending message...
Received <Hello from RabbitMQ!>

Summary

Congratulations! You’ve just developed a simple publish-and-subscribe application with Spring and RabbitMQ. There’s more you can do with Spring and RabbitMQ than what is covered here, but this should provide a good start.

gs-messaging-rabbitmq's People

Contributors

btalbott avatar buzzardo avatar cbeams avatar gregturn avatar habuma avatar kadoppe avatar royclarkson avatar shadowmanos avatar tspaeth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.