RabbitMQ Connection with Java

Adil Abdullah
3 min readJan 18, 2023

--

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);

Builder bui=new BasicProperties().builder().correlationId(“324324535767”);

BasicProperties bp=bui.build();

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(properties.getCorrelationId());

if (properties.getCorrelationId().equals(“324324535712”)) {

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();

}

}

}

}

--

--

Adil Abdullah

Currently working as Full stack Java developer at Contour Software at their Easit AB division part of Jonas group. Working on Java and IBM stack.