Saturday, February 24, 2018

Topic Setup in RabbitMQ

1. Login into the RabbitMQ portal using guest credentials

http://localhost:15672/#/exchanges

2. Add a new exchange
  • Put Name jsa.topic select type as topic
  • Leave all other fields remain same and click on Add Exchange
3. Click on the newly created exchange jsa.topic
4. Go to the queue tab:

http://localhost:15672/#/queues

5.Click on Add Queues
  • Put Name: jsa.queue.1
  • Leave all other fields remain same and click on Add Queue
  • Put Name: jsa.queue.2
  • Leave all other fields remain same and click on Add Queue
  • Put Name: jsa.queue.3
  • Leave all other fields remain same and click on Add Queue
6. Click on the newly created queue jsa.queue.1
7.Go the binding tab in the jsa.queue.1


  • Put the From Exchange: jsa.topic
  • Leave other fields blank and click on bind button
  • Repeat the same for other queues.

Friday, February 23, 2018

Sending and Receiving Message from Queue in RabbitMQ Using Spring Boot

1. Create a New Maven Project(Create Simple Project)
2.Add the following dependency in the pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.Add the following code to the application.properties
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
jsa.rabbitmq.exchange=jsa.direct
jsa.rabbitmq.routingkey=jsa.routingkey
jsa.rabbitmq.queue=jsa.queue

4. Create a Producer class

@Component
public class Producer {

@Autowired
private AmqpTemplate amqpTemplate;

@Value("${jsa.rabbitmq.exchange}")
private String exchange;

@Value("${jsa.rabbitmq.routingkey}")
private String routingKey;

public void produceMsg(String msg)
{
amqpTemplate.convertAndSend(exchange,routingKey,msg);
System.out.println("Send msg"+msg);
}
}

5.Create a mockup rest service to send the message
@RestController
public class WebController {

@Autowired
Producer producer;
@RequestMapping("/send")
public String sendMsg(@RequestParam("msg")String msg)
{
producer.produceMsg(msg);
return "Done";
}
}

6. Create a Consumer to consume the message
@Component
public class Consumer {
@RabbitListener(queues="${jsa.rabbitmq.queue}")
public void receivedMessage(String msg)
{
System.out.println("Recieved Msg"+msg);
}
}

Monday, February 19, 2018

Producer Consumer Queue Setup on RabbitMQ

1. Login into the RabbitMQ portal using guest credentials

http://localhost:15672/#/exchanges

2. Add a new exchange

  • Put Name jsa.direct
  • Leave all other fields remain same and click on Add Exchange
3. Click on the newly created exchange jsa.direct
4. Go to the queue tab:

http://localhost:15672/#/queues

5.Click on Add Queue
  • Put Name: jsa.queue
  • Leave all other fields remain same and click on Add Queue
6. Click on the newly created queue jsa.queue
7.Go the binding tab in the jsa.queue
  • Put the From Exchange: jsa.direct
  • Put the Routing Key: jsa.routingkey
  • Leave other fields blank and click on bind button


Installing RabbitMQ Server on Local Machine

  1. Install the Erlang(http://www.erlang.org/downloads)

2. Install the Rabbit MQ
https://www.rabbitmq.com

3. Open the Command Prompt
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.3\sbin

4. Run the following command
rabbitmq-plugins.bat enable rabbitmq_management

5.Restart the RabbitMQ Services
6. Open browser
http://localhost:15672

7. Use guest as username and password