English 中文(简体)
RabbitMQ - Subscriber Application
  • 时间:2024-12-22

RabbitMQ - Subscriber Apppcation


Previous Page Next Page  

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.

Advertisements