RabbitMQ

 



Exchange and queue are an integral part of messaging, without exchange also we can push the message to the consumers. But the exchange is used as a router that can rout the message to the respective queue.

Exchange is in 4 different types

  1. Direct Exchange
  2. Fanout Exchange
  3. Topic Exchange 
  4. Headers Exchange



note*: Exchanges are bound to one routing key , which helps to route the destination queue.

There is one more type of Exchange:  The default exchange is implicitly bound to every queue, with a routing key equal to the queue name. It is not possible to explicitly bind to, or unbind from the default exchange. It also cannot be deleted.


Install erlangOTP and set ERLANG_HOME to the base directory then set path %ERLANG_HOME%/bin

After that extract the rabbitmq_server-3.8.6 and extract it and execute as below  “F:\Softwares\rabbitmq_server-3.8.6\sbin> .\rabbitmq-plugins.bat enable rabbitmq_management”

After that execute rabbitmq-server.bat after successful execution login to rabbitmq.

 

Check the generated logs to see the port number,” Management plugin: HTTP (non-TLS) listener started on port 15672” default is 15672

Enter http://localhost:15672/


Username and password will be guest

after login please explore the tabs

Create a queue Queue-A

will check the basic sample code for the initial communication

public class PublishMsg {

       public static void main(String[] arg) throws IOException, TimeoutException

       {

              ConnectionFactory confactory=new ConnectionFactory();

              com.rabbitmq.client.Connection connection=confactory.newConnection();

              Channel channel=connection.createChannel();

              String msg="Testing the connection";

              channel.basicPublish("", "Queue-A", null, msg.getBytes());

             channel.close();

              connection.close();

       }

}

 

*note: channel.basicPublish("", "Queue-A", null, msg.getBytes());

the first parameter for exchange and we are not using any exchange so kept empty and no properties so kept argument as null.

 

Add dependency to pom

<dependency>

    <groupId>com.rabbitmq</groupId>

    <artifactId>amqp-client</artifactId>

    <version>5.9.0</version>

</dependency>

Now the queue is ready to serve the message

 

Click on the Queue-A and check the get message section


To consume the message

public class ConsumeMsg {

      

       public static void main(String[] arg) throws IOException, TimeoutException

       {

      

       ConnectionFactory confactory=new ConnectionFactory();

       com.rabbitmq.client.Connection connection=confactory.newConnection();

       Channel channel=connection.createChannel();

      

       DeliverCallback deliCallBack=(consumerTag , messageFrom)->{

              String msg=new String(messageFrom.getBody());

              System.out.println("Meg Received"+msg);

       };

       channel.basicConsume("Queue-A",true, deliCallBack,consumerTag->{});

      

       }

Output message

And the queue is empty 

*Note: If there is more than one consumer then it will serve in round-robin fashion.

Comments

Popular posts from this blog

Linux Basics

JAVA 8 Predefined Functions