Giter VIP home page Giter VIP logo

gs-messaging-rabbitmq's Introduction

This guide walks you through the process of creating a Spring Boot application that publishes and subscribes to a RabbitMQ AMQP server.

What You Will Build

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

Setting up the RabbitMQ Broker

Before you can build your messaging application, you need to set up a server to handle receiving and sending messages. https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/docker_compose_support.adoc

If you choose to run the RabbitMQ server yourself instead of using Spring Boot Docker Compose support, you have a few options:

  • Download the server and manually run it

  • Install with Homebrew, if you use a Mac

  • Manually run the compose.yaml file with docker-compose up

If you go with any of these alternate approaches, you should remove the spring-boot-docker-compose dependency from the Maven or Gradle build file. You will also need to add configuration to an application.properties file, as described in greater detail in the [_preparing_to_build_the_application] section. As mentioned earlier, this guide assumes that you use Docker Compose support in Spring Boot, so additional changes to application.properties are not required at this point.

Starting with Spring Initializr

You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this guide.

To manually initialize the project:

  1. Navigate to start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. Click Dependencies and select Spring for RabbitMQ and Docker Compose Support.

  4. Click Generate.

  5. Download the resulting ZIP file, which is an archive of an application that is configured with your choices.

Note
If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

Create a RabbitMQ Message Receiver

With any messaging-based application, you need to create a receiver that responds to published messages. The following listing (from src/main/java/com/example/messagingrabbitmq/Receiver.java) shows how to do so:

link:complete/src/main/java/com/example/messagingrabbitmq/Receiver.java[role=include]

The Receiver is a 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 lets it signal that the message has been 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. However, you need to:

  • Configure a message listener container.

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

  • Configure 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 will use RabbitTemplate to send messages, and you will register a Receiver with the message listener container to receive messages. The connection factory drives both, letting them connect to the RabbitMQ server. The following listing (from src/main/java/com/example/messagingrabbitmq/MessagingRabbitmqApplication.java) shows how to create the application class:

link:complete/src/main/java/com/example/messagingrabbitmq/MessagingRabbitmqApplication.java[role=include]

The @SpringBootApplication annotation offers a number of benefits, as described in the reference documentation.

The bean defined in the listenerAdapter() method is registered as a message listener in the container (defined in container()). It listens 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 that it invokes 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 do not send messages directly to queues. Instead, a message is sent to an exchange, which can go to a single queue or fan out to multiple queues, emulating the concept of JMS topics.

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.

In this case, we use a topic exchange, and the queue is bound with a routing key of foo.bar.#, which means that any messages sent with a routing key that begins with foo.bar. are routed to the queue.

Send a Test Message

In this sample, test messages are sent by a CommandLineRunner, which also waits for the latch in the receiver and closes the application context. The following listing (from src/main/java/com.example.messagingrabbitmq/Runner.java) shows how it works:

link:complete/src/main/java/com/example/messagingrabbitmq/Runner.java[role=include]

Notice that the template routes the message to the exchange with a routing key of foo.bar.baz, which matches the binding.

In tests, you can mock out the runner 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 starts listening for messages. There is a Runner bean, which is then automatically run. 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 can run the main method through your IDE. Note that, if you have cloned the project from the solution repository, your IDE may look in the wrong place for the compose.yaml file. You can configure your IDE to look in the correct place or you could use the command line to run the application. The ./gradlew bootRun and ./mvnw spring-boot:run commands will launch the application and automatically find the compose.yaml file.

Preparing to Build the Application

To run the code without Spring Boot Docker Compose support, you need a version of RabbitMQ running locally to connect to. To do this, you can use Docker Compose, but you must first make two changes to the compose.yaml file. First, modify the ports entry in compose.yaml to be '5672:5672'. Second, add a container_name.

The compose.yaml should now be:

services:
  rabbitmq:
    container_name: 'guide-rabbit'
    image: 'rabbitmq:latest'
    environment:
      - 'RABBITMQ_DEFAULT_PASS=secret'
      - 'RABBITMQ_DEFAULT_USER=myuser'
    ports:
      - '5672:5672'

You can now run docker-compose up to start the RabbitMQ service. Now you should have an external RabbitMQ server that is ready to accept requests.

Additionally, you need to tell Spring how to connect to the RabbitMQ server (this was handled automatically with Spring Boot Docker Compose support). Add the following code to a new application.properties file in src/main/resources:

spring.rabbitmq.password=secret
spring.rabbitmq.username=myuser

Regardless of how you chose to build and run the application, you should see the following output:

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

Summary

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

gs-messaging-rabbitmq's People

Contributors

bclozel avatar btalbott avatar buzzardo avatar cbeams avatar garyrussell avatar gregturn avatar habuma avatar kadoppe avatar manish-giri avatar naxa777 avatar nikos avatar phoneixs avatar robertmcnees avatar royclarkson avatar shadowmanos avatar spring-operator avatar tspaeth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gs-messaging-rabbitmq's Issues

Guide doesn't use the configured exchange/routing key

https://stackoverflow.com/questions/49304107/how-to-identify-the-exchange-to-which-the-message-is-routed-to-in-spring-boot-ra/49304417#comment85708903_49304417

The Runner sends the message to the default exchange ("") with the queue name as the routing key

rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");

It would be better to send to the explicit exchange and use some other routing key to bind the queue.

rabbitTemplate.convertAndSend("spring-boot-exchange", "some.routing.key", "Hello from RabbitMQ!");

Since the exchange is a topic exchange, it can use a binding like some.routing.#.

Or, drop the exchange/binding from the config if we want the "simplest" GSG.

Remove unused CI files

The file test/run.sh is no longer required. This file and the test directory can be deleted.

The file was originally used for Jenkins CI. Now that this project has been converted to GitHub Actions, this file can be removed.

Wrong class link in the guide

This guide has link to:

:complete/src/main/java/com/example/messagingrabbitmq/MessagingRabbitApplication.java[]

Which is invalid and should be changed (I guess) to:

complete/src/main/java/com/example/messagingrabbitmq/MessagingRabbitmqApplication.java

cannot access hello - zip END header not found

With starter 2.1.4.RELEASE I get the following error with mvn compile.

[ERROR] /tmp/rabbit/gs-messaging-rabbitmq/complete/src/main/java/hello/Runner.java:[1,1] cannot access hello
[ERROR] zip END header not found

And a lot more ERRORs like this.

Problem with building sample rabbitmq code in gradle

After launching rabbitmq-server and then ran the sample code in gradle, but sure why cannot build :

https://bitbucket.org/niceseb/myrabbitmq

Caused by: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
        at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:367)
        at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
        at org.gradle.api.tasks.JavaExec.exec(JavaExec.java:75)
        at org.springframework.boot.gradle.run.BootRunTask.exec(BootRunTask.java:63)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:227)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:585)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:568)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
        ... 14 more


BUILD FAILED

Docker compose file for RabbitMQ throws an error

Based on the instructions here - https://github.com/spring-guides/gs-messaging-rabbitmq?tab=readme-ov-file#what-you-need

The docker compose file for installing rabbitmq looks like this

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

This is either incomplete or incorrect, because docker-compose up command fails with this file.

D:\Downloads\messaging-rabbitmq
docker-compose up
validating D:\Downloads\messaging-rabbitmq\docker-compose.yml: (root) Additional property rabbitmq is not allowed

Version information

D:\Downloads\messaging-rabbitmq
docker compose version
Docker Compose version v2.24.3-desktop.1


D:\Downloads\messaging-rabbitmq
docker version
Client:
 Cloud integration: v1.0.35+desktop.10
 Version:           25.0.2
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        29cf629
 Built:             Thu Feb  1 00:24:09 2024
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.27.1 (136059)
 Engine:
  Version:          25.0.2
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       fce6e0c
  Built:            Thu Feb  1 00:23:17 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0

Not able to run the Complete part

Here is my errors

Exception in thread "main" java.lang.IllegalArgumentException: Attribute 'exclude' is of type [Class[]], but [String[]] was expected. Cause: 
    at org.springframework.core.annotation.AnnotationAttributes.doGet(AnnotationAttributes.java:117)
    at org.springframework.core.annotation.AnnotationAttributes.getStringArray(AnnotationAttributes.java:70)
    at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:63)
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:386)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:204)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:163)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:138)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:284)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:225)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:683)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
    at Application.main(Application.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Here is the link on the stackoverflow

http://stackoverflow.com/questions/29892408/whats-wrong-with-rabbitmq-in-spring-boot

Upgrade Spring Boot to the latest version

Update the guide to use the most recent Spring Boot version.

Files that require changes are:

initial/build.gradle
initial/pom.xml
complete/build.gradle
complete/pom.xml

Not acknowledging receive

When running the sample app on the second time, it seems to be receiving the message from the previous run, I guess its not acknowledging the message from RabbitMQ?
p.s I'm very new to RabbitMQ, so I am not sure if this is working as expected.

NoSuchBeanDefinitionException for AnnotationConfigApplicationContext

I was wondering if you could please help me, I took the exact code from the complete folder and when I try and run it I get the following error:

Caused by:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.context.annotation.AnnotationConfigApplicationContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

It seems to be swearing about

    @Autowired
    AnnotationConfigApplicationContext context;

I know my rabbit server is running as i have checked with rabbitmqctl status and its all ok. And then I found the RabbitAutoConfigurationTests which all run and pass.

Any idea what I could be doing wrong?

Documentation should reference more recent Docker Compose command

The current documentation states references docker-compose up when it should use docker compose up instead. More details here.

The text to be changed is in the Preparing to Build the Application section of the README.adoc:

You can now run docker-compose up to start the RabbitMQ service. Now you should have an external RabbitMQ server that is ready to accept requests.

Should instead be:

You can now run docker compose up to start the RabbitMQ service. Now you should have an external RabbitMQ server that is ready to accept requests.

Introduce Spring Native Support

Opening this issue to continue the conversation found in this PR.

In order to build this guide as is with native compilation a custom hint needs to be provided. Specific details on the hint required can be found in this commit. The parts about native compilation were reverted prior to merging.

Consider adding native support compilation to this guide.

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.