English 中文(简体)
RabbitMQ - Consumer Application
  • 时间:2024-11-03

RabbitMQ - Consumer Apppcation


Previous Page Next Page  

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.

Advertisements