RabbitMQ Connection with Java

Open CMD

Go to this location C:\Program Files\RabbitMQ Server\rabbitmq_server-3.11.6\sbin

Execute below commands

rabbitmq-plugins.bat enable rabbitmq_management

rabbitmq-service.bat stop

rabbitmq-service.bat install

rabbitmq-service.bat start

URL: http://localhost:15672/

Default username and password

Username: guest

Password: guest

Overview:

Display statistics and metrics of all queues, exchanges and routing keys.

Connections:

Display connection when application tries to connect with RabbitMQ. Each application can have multiple connections.

Channels:

Each application which connect with RabbitMQ have one channel and multiple connections.

Exchange:

It is a intermediate communicator between queue and client application.

Queues:

Used to store message.

Java Code:

Maven dependency:

<dependency>

<groupId>com.rabbitmq</groupId>

<artifactId>amqp-client</artifactId>

<version>5.16.0</version>

</dependency>

Connection class:

package com.java.connection;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

public class Connections {

static Channel channel=null;

static Connection conn=null;

public static void main(String[] args)

{

System.out.println(Conn());

}

public static Channel Conn()

{

try {

ConnectionFactory factory = new ConnectionFactory();

factory.setHost(“localhost”);

conn = (Connection) factory.newConnection();

channel = conn.createChannel();

return channel;

}

catch(Exception ex)

{

ex.printStackTrace();

System.out.println(ex.getMessage());

return null;

}

}

}

WritingMessage class:

package com.java.messages;

import java.io.IOException;

import java.util.concurrent.TimeoutException;

import com.java.connection.Connections;

public class WriteMessages {

public static void main(String[] args)

{

writeMessage(“q2”,”route1",”test”,”This is first q2 message”);

}

public static void writeMessage(String queue,String routingKey,String exchange,String msg)

{

try {

Connections.Conn().queueBind(queue,exchange,routingKey);

Connections.Conn().basicPublish(exchange,routingKey, null,msg.getBytes());

System.out.println(“Succesfull”);

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println(e.getMessage());

e.printStackTrace();

}

finally

{

try {

Connections.Conn().close();

} catch (Exception ee) {

// TODO Auto-generated catch block

ee.printStackTrace();

}

}

}

}

ReadingMessage class:

package com.java.messages;

import java.io.IOException;

import com.java.connection.Connections;

import com.rabbitmq.client.AMQP;

import com.rabbitmq.client.DefaultConsumer;

import com.rabbitmq.client.DeliverCallback;

import com.rabbitmq.client.Envelope;

public class ReadMessage {

public static void main(String[] args)

{

readMessage(“q2”);

}

public static void readMessage(String queue)

{

try {

DefaultConsumer consumer = new DefaultConsumer(Connections.Conn()) {

@Override

public void handleDelivery(

String consumerTag,

Envelope envelope,

AMQP.BasicProperties properties,

byte[] body) throws IOException {

String message = new String(body, “UTF-8”);

System.out.println(“Consumed: “ + message);

}

};

Connections.Conn().basicConsume(queue, true, consumer);

// System.out.println(“Succesfull”);

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println(e.getMessage());

e.printStackTrace();

}

finally

{

try {

Connections.Conn().close();

} catch (Exception ee) {

// TODO Auto-generated catch block

ee.printStackTrace();

}

}

}

}

--

--

Currently working as Full stack Java developer at Contour Software. Working on Java and IBM stack.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adil Abdullah

Currently working as Full stack Java developer at Contour Software. Working on Java and IBM stack.