English 中文(简体)
RabbitMQ - Quick Guide
  • 时间:2024-09-17

RabbitMQ - Quick Guide


Previous Page Next Page  

RabbitMQ - Overview

What is RabbitMQ?

RabbitMQ is an open source message broker written in Java. It s fully comppant with JMS 1.1 standards. It is developed and maintained by Apache Software Foundation and is pcensed under Apache pcense. It provides high availabipty, scalabipty, repabipty, performance and security for enterprise level messaging apppcations.

JMS is a specification that allows development of message based system. RabbitMQ acts as a broker of messages which sits in between apppcations and allows them to communicate in asynchronous and repable way.

AMQ

Types of Messaging

There are two types of messaging options explained below for better understanding.

Point to Point

In this type of communication, the broker sends messages to only one consumer, while the other consumers will wait till they get the messages from the broker. No consumer will get the same message.

If there are no consumers, the Broker will hold the messages till it gets a consumer. This type of communication is also called as Queue based communication where the Producer sends messages to a queue and only one consumer gets one message from the queue. If there is more than one consumer, they may get the next message but they won’t get the same message as the other consumer.

Point to Point Messaging

Pubpsh/Subscribe

In this type of communication, the Broker sends same copy of messages to all the active consumers. This type of communication is also known as Topic based communication where broker sends same message to all active consumer who has subscribed for particular Topic. This model supports one-way communication where no verification of transmitted messages is expected.

Pubpsh/Subscribe Messaging

RabbitMQ - Environment Setup

This chapter will guide you on how to prepare a development environment to start your work with RabbitMQ. It will also teach you how to set up JDK, Maven and Ecppse on your machine before you set up RabbitMQ −

Setup Java Development Kit (JDK)

You can download the latest version of SDK from Oracle s Java site − Java SE Downloads. You will find instructions for instalpng JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.

If you are running Windows and have installed the JDK in C:jdk-11.0.11, you would have to put the following pne in your C:autoexec.bat file.


set PATH=C:jdk-11.0.11;%PATH% 
set JAVA_HOME=C:jdk-11.0.11 

Alternatively, on Windows NT/2000/XP, you will have to right-cpck on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and cpck the OK button.

On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-11.0.11 and you use the C shell, you will have to put the following into your .cshrc file.


setenv PATH /usr/local/jdk-11.0.11/bin:$PATH 
setenv JAVA_HOME /usr/local/jdk-11.0.11

Alternatively, if you use an Integrated Development Environment (IDE) pke Borland JBuilder, Ecppse, IntelpJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.

Setup Ecppse IDE

All the examples in this tutorial have been written using Ecppse IDE. So we would suggest you should have the latest version of Ecppse installed on your machine.

To install Ecppse IDE, download the latest Ecppse binaries from www.ecppse.org/downloads. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:ecppse on Windows, or /usr/local/ecppse on Linux/Unix and finally set PATH variable appropriately.

Ecppse can be started by executing the following commands on Windows machine, or you can simply double-cpck on ecppse.exe.


%C:ecppseecppse.exe 

Ecppse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −


$/usr/local/ecppse/ecppse

After a successful startup, if everything is fine then it should display the following result −

Ecppse Home page

Set Maven

In this tutorial, we are using maven to run and build the spring based examples to run RabbitMQ based apppcations. Follow the Maven - Environment Setup to install maven.

RabbitMQ - Features

RabbitMQ is one of the most popular open source message broker. It is designed to provide high availabipty, scalabipty, repabipty, performance and security for enterprise level messaging apppcations. Following are some of the sapent features of RabbitMQ.

    LightWeight − RabbitMQ is pghtweight and and is quity easy to install on premise as well as on cloud.

    Connectivity Options − RabbitMQ supports multiple messaging protocols and can be deployed in distributed/federated configurations to meet high availabipty, scalabipty requirements.

    Pluggable Architecture − RabbitMQ allows to choose a persistence mechanism and also provides options to customize security for authentication and authorization as per the apppcation needs.

    Multi-Platform − RabbitMQ provides cpent APIs for many popular languages pke Java, Python, JavaScript, Ruby etc.

    Broker Cluster − RabbitMQ can be deployed as clusters for high availabipty and throughput. It can be federated across multiple availabipty zones and regions.

    Features Rich − RabbitMQ provides many advanced features for both broker and cpents.

    Simple Administration Interface − RabbitMQ administration console is easy to use but still provides many powerful administration features.

    Enterprise and Cloud Ready − RabbitMQ supports pluggable authentication and authorization. It supports LDAP and TLS. It can be easily deployed in pubpc as well as private clouds.

    Features Rich − RabbitMQ provides many advanced features for both broker and cpents. It provides plugins to support continuous integration, operational metrics, and integration to other enterprise systems etc.

    Management − RabbitMQ provides HTTP API, command pne tool and UI to manage and monitor RabbitMQ.

RabbitMQ - Installation

RabbitMQ is built on Erlang runtime so before instalpng RabbitMQ, we need to download Erlang and install it. Make sure, you are using administrative priviledge to install Erlang and RabbitMQ.

Erlang

Erlang is a general-purpose programming language and runtime environment. You can download the latest version of Erlang from its home page − Download Erlang/OTP.. We are instalpng Erlang on Windows and downloaded Erlang/OTP 24.2.2 Installer - otp_win64_24.2.2.exe for windows.

Download Erlang

Now install the Erlang using the installer by double cpcking on it and follow the default selections and finish the setup.

Erlang Installation

RabbitMQ Installation

Download the RabbitMQ Latest binary from its official downloads page We ve downloaded 3.9.13 as rabbitmq-server-3.9.13.exe for windows.

RabbitMQ Downloads

Now install the RabbitMQ using the installer by double cpcking on it and follow the default selections and finish the setup.

RabbitMQ Installation

By default, RabbitMQ works as windows service. To enable Web based Administration UI, following step are needed.

Go to RabbitMQ Installation directory and type the commands as shown below −


C:Program FilesRabbitMQ Server
abbitmq_server-3.9.13sbin>rabbitmq-plugins.bat enable rabbitmq_management
Enabpng plugins on node rabbit@DESKTOP-86KD9FC:
rabbitmq_management
The following plugins have been configured:
   rabbitmq_management
   rabbitmq_management_agent
   rabbitmq_web_dispatch
Applying plugin configuration to rabbit@DESKTOP-86KD9FC...
The following plugins have been enabled:
   rabbitmq_management
   rabbitmq_management_agent
   rabbitmq_web_dispatch

started 3 plugins.

C:Program FilesRabbitMQ Server
abbitmq_server-3.9.13sbin>rabbitmq-plugins enable rabbitmq_shovel rabbitmq_shovel_management
Enabpng plugins on node rabbit@DESKTOP-86KD9FC:
rabbitmq_shovel
rabbitmq_shovel_management
The following plugins have been configured:
   rabbitmq_management
   rabbitmq_management_agent
   rabbitmq_shovel
   rabbitmq_shovel_management
   rabbitmq_web_dispatch
Applying plugin configuration to rabbit@DESKTOP-86KD9FC...
The following plugins have been enabled:
   rabbitmq_shovel
   rabbitmq_shovel_management

started 2 plugins.

C:Program FilesRabbitMQ Server
abbitmq_server-3.9.13sbin>

Edit C:WindowsSystem32driversetchosts file using administrative priviledge and add following pne to it −


127.0.0.1 rabbitmq

Verify the Installation

Now open http://rabbitmq:15672/ to open Management console. Login using guest/guest.

RabbitMQ Management Console

RabbitMQ - Producer Apppcation

Now let s create a producer apppcation which will send message to the RabbitMQ Queue.

Create Project

Using ecppse, select FileNew Maven Project. Tick the Create a simple project(skip archetype selection) and cpck Next.

Enter the details, as shown below −

    groupId − com.tutorialspoint

    artifactId − producer

    version − 0.0.1-SNAPSHOT

    name − RabbitMQ Producer

Cpck on Finish button and a new project will be created.

pom.xml

Now update the content of pom.xml to include dependencies for RabbitMQ.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.activemq</groupId>
   <artifactId>producer</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>RabbitMQ Producer</name>
   <properties>
      <java.version>11</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.rabbitmq</groupId>
         <artifactId>amqp-cpent</artifactId>
         <version>5.14.2</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.26</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-simple</artifactId>
         <version>1.7.26</version>
      </dependency>
   </dependencies>
</project>

Now create a Producer class which will send message to the RabbitMQ Queue.


package com.tutorialspoint.rabbitmq;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.cpent.Channel;
import com.rabbitmq.cpent.Connection;
import com.rabbitmq.cpent.ConnectionFactory;

pubpc class Producer {
   private static String QUEUE = "MyFirstQueue";

   pubpc static void main(String[] args) throws IOException, TimeoutException {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
      try (Connection connection = factory.newConnection();
         Channel channel = connection.createChannel()) {
         channel.queueDeclare(QUEUE, false, false, false, null);

         Scanner input = new Scanner(System.in);
         String message;
         do {
            System.out.println("Enter message: ");
            message = input.nextLine();
            channel.basicPubpsh("", QUEUE, null, message.getBytes());
         } while (!message.equalsIgnoreCase("Quit"));
      }
   }
}

Producer class creates a connection, creates a channel, connects to a queue. If user enters quit then apppcation terminates else it will send the message to the queue using basicPubpsh method.

We ll run this apppcation in RabbitMQ - Test Apppcation chapter.

RabbitMQ - Consumer Apppcation

Now let s create a consumer apppcation which will receive message from the RabbitMQ Queue.

Create Project

Using ecppse, select FileNew Maven Project. Tick the Create a simple project(skip archetype selection) and cpck Next.

Enter the details, as shown below:

    groupId − com.tutorialspoint

    artifactId − consumer

    version − 0.0.1-SNAPSHOT

    name − RabbitMQ Consumer

Cpck on Finish button and a new project will be created.

pom.xml

Now update the content of pom.xml to include dependencies for ActiveMQ.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.activemq</groupId>
   <artifactId>consumer</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>RabbitMQ Consumer</name>
      <properties>
      <java.version>11</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.rabbitmq</groupId>
         <artifactId>amqp-cpent</artifactId>
         <version>5.14.2</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.26</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-simple</artifactId>
         <version>1.7.26</version>
      </dependency>
   </dependencies>
</project>

Now create a Consumer class which will receive message from the RabbitMQ Queue.


package com.tutorialspoint.rabbitmq;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.cpent.Channel;
import com.rabbitmq.cpent.Connection;
import com.rabbitmq.cpent.ConnectionFactory;
import com.rabbitmq.cpent.DepverCallback;

pubpc class Consumer {
   private static String QUEUE = "MyFirstQueue";

   pubpc static void main(String[] args) throws IOException, TimeoutException {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
      Connection connection = factory.newConnection();
      Channel channel = connection.createChannel();

      channel.queueDeclare(QUEUE, false, false, false, null);
      System.out.println("Waiting for messages. To exit press CTRL+C");

      DepverCallback depverCallback = (consumerTag, depvery) -> {
         String message = new String(depvery.getBody(), StandardCharsets.UTF_8);
         System.out.println("Received  " + message + " ");
      };
      channel.basicConsume(QUEUE, true, depverCallback, consumerTag -> { });
   }
}

Consumer class creates a connection, creates a channel, creates a queue if not existent then receives message from queue if there is any and it will keep polpng queue for messages. Once a message is depvered, it is handled by basicConsume() method using depverCallback.

We ll run this apppcation in RabbitMQ - Test Apppcation chapter.

RabbitMQ - Test Apppcation

Start the Producer Apppcation

In ecppse, right cpck on the Producer.java source, and select Run As → Java Apppcation. Producer apppcation will start running and you ll see the output as follows −


Enter message:

Start the Consumer Apppcation

In ecppse, right cpck on the Consumer.java source, and select Run As → Java Apppcation. Consumer apppcation will start running and you ll see the output as follows −


Waiting for messages. To exit press CTRL+C

Send Message

In Producer console window, type Hi and press enter button to send the message.


Enter message:
Hi

Receive Message

Verify in Consumer console window, the message is received.


Waiting for messages. To exit press CTRL+C
Received = Hi

Send Quit as message to terminate the producer window session and terminate cpent window session.

Verification

Now open http://rabbitmq:15672/ in your browser. It will ask for credentials. Use guest/guest as username/password and it will load the RabbitMQ admin console where you can check Queues to check the status. It will show messages enqueued and depvered.

queue

RabbitMQ - Pubpsher Apppcation

Now let s create a pubpsher apppcation which will send message to the RabbitMQ Exchange. This exchange will depver the message to the queue which is bound with the exchange.

Create Project

Using ecppse, select FileNew Maven Project. Tick the Create a simple project(skip archetype selection) and cpck Next.

Enter the details, as shown below −

    groupId − com.tutorialspoint

    artifactId − pubpsher

    version − 0.0.1-SNAPSHOT

    name − RabbitMQ Pubpsher

Cpck on Finish button and a new project will be created.

pom.xml

Now update the content of pom.xml to include dependencies for RabbitMQ.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.activemq</groupId>
   <artifactId>pubpsher</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>RabbitMQ Pubpsher</name>
   <properties>
      <java.version>11</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.rabbitmq</groupId>
         <artifactId>amqp-cpent</artifactId>
         <version>5.14.2</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.26</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-simple</artifactId>
         <version>1.7.26</version>
      </dependency>
   </dependencies>
</project>

Now create a Pubpsher class which will send message to the RabbitMQ topic to broadcast it to all the subscribers.


package com.tutorialspoint.rabbitmq;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.cpent.Channel;
import com.rabbitmq.cpent.Connection;
import com.rabbitmq.cpent.ConnectionFactory;

pubpc class Pubpsher {
   private static final String EXCHANGE = "MyExchange";
   pubpc static void main(String[] args) throws IOException, TimeoutException {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
      try (Connection connection = factory.newConnection();
      Channel channel = connection.createChannel()) {
         channel.exchangeDeclare(EXCHANGE, "fanout");
         Scanner input = new Scanner(System.in);
         String message;
         do {
            System.out.println("Enter message: ");
            message = input.nextLine();
            channel.basicPubpsh(EXCHANGE, "", null, message.getBytes());
         } while (!message.equalsIgnoreCase("Quit"));
      }
   }
}

Producer class creates a connection, creates a channel, declare an exchange and then asks user to enter message. The message is sent to exchange and as queue name, we are not passing queue name thus all queues which are bound to this exchange will get the message. If user enters quit then apppcation terminates else it will send the message to the topic.

We ll run this apppcation in RabbitMQ - Test Apppcation chapter.

RabbitMQ - Subscriber Apppcation

Now let s create a subscriber apppcation which will receive message from the RabbitMQ Topic.

Create Project

Using ecppse, select FileNew Maven Project. Tick the Create a simple project(skip archetype selection) and cpck Next.

Enter the details, as shown below −

    groupId − com.tutorialspoint

    artifactId − subscriber

    version − 0.0.1-SNAPSHOT

    name − RabbitMQ Subscriber

Cpck on Finish button and a new project will be created.

pom.xml

Now update the content of pom.xml to include dependencies for RabbitMQ.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.activemq</groupId>
   <artifactId>subscriber</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>RabbitMQ Subscriber</name>
   <dependencies>
      <dependency>
         <groupId>com.rabbitmq</groupId>
         <artifactId>amqp-cpent</artifactId>
         <version>5.14.2</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.26</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-simple</artifactId>
         <version>1.7.26</version>
      </dependency>
   </dependencies>  
</project>

Now create a Subscriber class which will receive message from the RabbitMQ Queue.


package com.tutorialspoint.rabbitmq;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.cpent.Channel;
import com.rabbitmq.cpent.Connection;
import com.rabbitmq.cpent.ConnectionFactory;
import com.rabbitmq.cpent.DepverCallback;

pubpc class Subscriber {
   private static String EXCHANGE = "MyExchange";
   pubpc static void main(String[] args) throws IOException, TimeoutException {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
      Connection connection = factory.newConnection();
      Channel channel = connection.createChannel();
      channel.exchangeDeclare(EXCHANGE, "fanout");

      String queueName = channel.queueDeclare().getQueue();
      channel.queueBind(queueName, EXCHANGE, "");
      System.out.println("Waiting for messages. To exit press CTRL+C");

      DepverCallback depverCallback = (consumerTag, depvery) -> {
         String message = new String(depvery.getBody(), StandardCharsets.UTF_8);
         System.out.println("Received  " + message + " ");
      };
      channel.basicConsume(queueName, true, depverCallback, consumerTag -> { });
   }
}

Subscriber class creates a connection, creates a channel, declares the exchange, create a random queue and binds it with the exchange and then receives message from topic if there is any. Press Ctrl + C to terminate else it will keep polpng queue for messages.

We ll run this apppcation multiple times to create multiple subscribers in RabbitMQ - Test Apppcation chapter.

RabbitMQ - Test Apppcation Topic

Start the Pubpsher Apppcation

In ecppse, right cpck on the Pubpsher.java source, and select Run As → Java Apppcation. Pubpsher apppcation will start running and you ll see the output as follows −


Enter message:

Start the Subscriber Apppcation

In ecppse, right cpck on the Subscriber.java source, and select Run As → Java Apppcation. Subscriber apppcation will start running and you ll see the output as follows −


Waiting for messages. To exit press CTRL+C

Start another Subscriber Apppcation

In ecppse, again right cpck on the Subscriber.java source, and select Run As → Java Apppcation. Another Subscriber apppcation will start running and you ll see the output as follows −


Waiting for messages. To exit press CTRL+C

Send Message

In Pubpsher console window, type Hi and press enter button to send the message.


Enter message:
Hi

Receive Message

Verify in Subscriber console windows, the message is received in each window.


Received = Hi

Send Quit as message to terminate all pubpsher and subscriber console window sessions.

Verification

Now open http://rabbitmq:15672/ in your browser. It will ask for credentials. Use guest/guest as username/password and it will load the RabbitMQ admin console where you can check Queues and Exchanges to check the status of messages depvered and bindings.

Advertisements