- RabbitMQ - Test Application
- RabbitMQ - Subscriber Application
- RabbitMQ - Publisher Application
- RabbitMQ - Test Application
- RabbitMQ - Consumer Application
- RabbitMQ - Producer Application
- RabbitMQ - Installation
- RabbitMQ - Features
- RabbitMQ - Environment Setup
- RabbitMQ - Overview
- RabbitMQ - Home
RabbitMQ Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
RabbitMQ - Producer Apppcation
Now let s create a producer apppcation which will send message to the RabbitMQ Queue.
Create Project
Using ecppse, select File → New → Maven Project. Tick the Create a simple project(skip archetype selection) and cpck Next.
Enter the details, as shown below −
groupId − com.tutorialspoint
artifactId − producer
version − 0.0.1-SNAPSHOT
name − RabbitMQ Producer
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>producer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>RabbitMQ Producer</name> <properties> <java.version>11</java.version> </properties> <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 Producer class which will send message to the RabbitMQ Queue.
package com.tutorialspoint.rabbitmq; import java.io.IOException; import java.util.Scanner; import java.util.concurrent.TimeoutException; import com.rabbitmq.cpent.Channel; import com.rabbitmq.cpent.Connection; import com.rabbitmq.cpent.ConnectionFactory; pubpc class Producer { private static String QUEUE = "MyFirstQueue"; pubpc static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(QUEUE, false, false, false, null); Scanner input = new Scanner(System.in); String message; do { System.out.println("Enter message: "); message = input.nextLine(); channel.basicPubpsh("", QUEUE, null, message.getBytes()); } while (!message.equalsIgnoreCase("Quit")); } } }
Producer class creates a connection, creates a channel, connects to a queue. If user enters quit then apppcation terminates else it will send the message to the queue using basicPubpsh method.
We ll run this apppcation in
chapter. Advertisements