0% found this document useful (0 votes)
13 views12 pages

RabbitMQ With C# - Code-Adda - Cara Instalasi

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

RabbitMQ With C# - Code-Adda - Cara Instalasi

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

RabbitMQ with C# - Code-Adda https://code-adda.

com/2019/01/rabbitmq-with-c/

CODE-ADDA
LIFE BETWEEN CODE AND COFFEE

HOME DOT NET  JAVA  WEB API SQL SERVER TIPS/TRICKS

INTERVIEW QUESTIONS

RabbitMQ with C# Select Language


Powered by Translate

 January 27, 2019  Ravi Ranjan Kumar  C# 0


SEARCH

SEARCH …

TOP POSTS

How to deploy spring


boot war in jboss

JWT Authentication with


ASP.NET WEB API

Deploy Angular App On


RabbitMQ Tutorials - Free Download Tomcat Server
Get started with RabbitMQ. Download our free ebook to get all the tips you need!
How to solve - Could
In this article, you will learn how to write a basic program using
not �nd a part of the
RabbitMQ and C# like how to create connections, how to publish a
path bin roslyn csc.exe
message in the queue and how to receive that message from a
error
queue. Before writing code, I assume you have already had some
idea about what is RabbitMQ and why we should use RabbitMQ. If Upload �le on server
not no worry here I am going to tell you some basic feature of using ASP.NET WEB API
RabbitMQ.
How to secure ASP.NET
Web API using Token
A Basic Introduction of RabbitMQ
Based Authentication

1 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

It is quite popular, free and open Source message broker that How To Enable SSL or
implements AMQP(Advanced Message Queuing Protocol). HTTPS In Apache
It is written in Erlang programming language. You don’t need Tomcat 8
to learn Erlang to go through with RabbitMQ. Erlang is just a
Validating UIDAI
prerequisite because it is written in Erlang and it needs that
Aadhaar number using
to run in your system.
Verhoe� Algorithm
Provides client library in di�erent programming languages
(Java, C#, Python, Javascript, etc) for Interactions with Creating your �rst WEB
RabbitMQ server. API Project

It allows you not to worry about programming Consuming RESTful API


Interoperability. Publisher and Receiver are independent to to get Bank Details
choose their own programming language as per their using IFSC code in C#
interest.
It is easy to use and can run on many di�erent OS (Operating
System) CATEGORIES

It supports various messaging patterns.


ANGULAR

ASP.NET

Awards

AWS

C#

Dot Net

General

INTERVIEW QUESTIONS
Producer: Producer is a component that sends or can say
Java
push data to message broker in Queue.
Java 8
Exchange: Exchange is a component which comes in action
when a producer creates a message that will not directly be Learning
sent to a queue, instead �rst the message will be sent to
Maven
exchanges, then after that a routing agent reads and sends it
to the appropriate queue with help of header attributes, PYTHON
bindings, and routing keys.
Spring Boot
Queue: Queue is a component which acts like message
bu�er which can hold a large amount of data. It works on the SQL SERVER
basis of FIFO (First in First Out
TIPS/TRICKS
Consumer: Consumer is a component which basically

2 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

consuming or can say receiving data stored in Queue WEB API

Note :
SUBSCRIBE TO BLOG VIA
Many producers can send messages that go to one EMAIL
queue, and many consumers can try to receive data
from one queue Enter your email address to
subscribe to this blog and
receive noti�cations of new
For more Information, I suggest you read their o�cial
posts by email.
documentation at https://www.rabbitmq.com/getstarted.html
E mail A ddress

Installation and Setup Subscribe

It is quite easy for the installation of RabbitMQ application on your


system. Please follow the given steps in order to install RabbitMQ in
your system.

1. Download Erlang from their o�cial Website.


http://www.erlang.org/downloads
2. Install Erlang installer in your system.
3. Download RabbitMQ installer from their o�cial website.
https://www.rabbitmq.com/install-windows.html
4. Install RabbitMQ installer in your System.
ADVERTISEMENT
Great you have successfully installed RabbitMQ in your system. You
might be excited to open RabbitMQ in your browser to see how its
User Interface looks and what things you can do. Still, one step is
left and that is to enable RabbitMQ management.

Fine! but how to do?

Open RabbitMQ Command Prompt as an administrator mode.

Navigate to the sbin directory of the RabbitMQ Server


installation directory. Probable path is C:\Program Files
(x86)\RabbitMQ Server\rabbitmq_server-3.3.4\sbin
Run the following command to enable the plugin

rabbitmq-plugins.bat enable rabbitmq_management

3 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

To check if everything worked as expected:

Navigate to http://localhost:15672. You will be prompted for


username and password. The default credentials are: guest for
username and guest for the password as well

Have a look at below images to see step by step process in order to


install and run RabbitMQ in your system.

Installing Erlang installer in your system

Installing RabbitMQ Server 3.7.10 Setup

4 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

Enabling RabbitMQ Management

RabbitMQ Localhost Url – localhost:15672

RabbitMQ Management UI

Programming in RabbitMQ using C#

As you have already installed RabbitMQ in your system and have


checked it is working �ne. Now, this is time to write a program and
publish a message in Queue and Receive that message from
Queue. In order to perform all these tasks, you need RabbitMQ
client in your visual studio to connect RabbitMQ. You can easily
install RabbitMQ Client by using Package Manager Console.

5 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

Install-Package RabbitMQ.Client -Version 5.1.0

Code to Publish Message in Queue

You have to use RabbitMQ.Client as the namespace in order to use


all available classes and functions to write code for creating a
connection with RabbitMQ, create exchange, queue and publish
message. ConnectionFactory is the main entry point to the
RabbitMQ.NET AMQP client. It constructs the IConnection
instances. De�ne all required property of ConnectionFactory like
HostName, UserName, Password, Port. CreateConnection is used
to create a connection to the one of the endpoint. CreateModel is
used to create and return fresh channel, session, and
model. ExchangeDeclare is used to declare an exchange. Here we
are using AMQP fanout exchanges. In the later article, we will
discuss the di�erent type of exchange type. QueueDeclare is used
to declare a Queue. QueBind is used to bind a queue with the
exchange. BasicPublish is used to publish your message to Queue.

1 using RabbitMQ.Client;
2 using System;
3 using System.Text;
4
5 namespace HelloWorld_RabbitMQ
6 {
7 class Producer
8 {
9 private const string exchangeName = "HelloWorld_RabbitMQ
10 private const string queueName = "HelloQueue";
11 public static void Main(string[] args)
12 {
13 ConnectionFactory factory = new ConnectionFactory
14
15 using (IConnection connection = factory.CreateConnection
16 {
17 using (IModel channel = connection.CreateModel
18 {
19 channel.ExchangeDeclare(exchangeName, ExchangeType
20 channel.QueueDeclare(queue: queueName,

6 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

21 durable: false,
22 exclusive: false,
23 autoDelete: false,
24 arguments: null);
25
26 string message = "Hello World. This is my first RabbitMQ M
27 var body = Encoding.UTF8.GetBytes(message
28 channel.QueueBind(queueName, exchangeName
29
30 channel.BasicPublish(exchange:exchangeName
31 routingKey: string.
32 basicProperties: null
33 body: body);
34 Console.WriteLine("Message Sent Successfully - {0}
35
36 }
37 }
38 Console.ReadLine();
39 }
40 }
41 }
42

HelloWorld_RabbitMQ_Producer.cs hosted with by GitHub view raw

Once we write code to publish a message in Queue. Run the


program and see its e�ect on RabbitMQ management. Navigate
to http://localhost:15672 and you can see there are one extra
exchange have been created named “HelloWorld_RabbitMQ”

You can also see a new entry of Queue named “HelloQueue” having
one message to read.

7 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

When you click on HelloQueue and it will navigate to HelloQueue


page where you can see what message you have published.

Code to Consume Message from Queue

As you can see, we are going to write the same code as we have
written in the Producer class in order to create a connection,
declare exchange and queue.

Note:

It is good practice to declare Queue before consuming


message that we can be sure the de�ned queue already exists
before we try to get messages from it.

At last, we are going to tell the server to deliver the messages from

8 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

the queue what we have published. Since it will push us messages


asynchronously, we provide a callback. That is
what EventingBasicConsumer.Received event handler does.

1 using System;
2 using System.Text;
3 using RabbitMQ.Client;
4 using RabbitMQ.Client.Events;
5
6 namespace HelloWorld_RabbitMQ
7 {
8 class Consumer
9 {
10 private const string exchangeName = "HelloWorld_RabbitMQ
11 private const string queueName = "HelloQueue";
12
13 private static void Main(string[] args)
14 {
15 ConnectionFactory factory = new ConnectionFactory
16
17 using (IConnection connection = factory.CreateConnection
18 {
19 using (IModel channel = connection.CreateModel
20 {
21 channel.ExchangeDeclare(exchangeName, ExchangeType
22
23 channel.QueueDeclare(queue: queueName,
24 durable
25 exclusive
26 autoDelete
27 arguments
28

9 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

29 channel.QueueBind(queueName, exchangeName
30
31 Console.WriteLine("Waiting for messages"
32
33 var consumer = new EventingBasicConsumer
34 consumer.Received += (model, ea) =>
35 {
36 var body = ea.Body;
37 var message = Encoding.UTF8.GetString
38 Console.WriteLine("Received - {0}",
39 };
40 channel.BasicConsume(queue: queueName,
41 autoAck: true,
42 consumer: consumer);
43
44 Console.ReadLine();
45
46 }
47 }
48 }
49 }
50 }

HelloWorld_RabbitMQ_Consumer.cs hosted with by GitHub view raw

Once you run the program you can see the message you have
published in the Queue is being displayed on your screen.

Note:

The producer, consumer, and broker usually do not have to


reside on the same host as I have shown in this article.
Sometime you may be a consumer or sometime you may be a
producer.

Just to demonstrate RabbitMQ I have used the same host to send

10 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

and receive the message. Usually, it doesn’t happen. In Later article,


we will cover some more interesting topic of RabbitMQ till then
keep learning. Happy Coding

Share Knowledge :

     

Related

 .NET MICROSERVICES C# PUBLISH IN RABBITMQ

RABBITMQ RABBITMQ QUEUE

About Ravi Ranjan Kumar  30 Articles


An Indian who Living, Loving & Learning
Technology with di�erent tastes and willing to
share knowledge and thoughts.

 PREVIOUS NEXT 
Custom Banners In Insert File Into
Spring Boot MySQL Database In
Java

BE THE FIRST TO COMMENT

Leave a Reply

11 of 12 6/28/2019, 9:17 AM
RabbitMQ with C# - Code-Adda https://code-adda.com/2019/01/rabbitmq-with-c/

INFORMATION FOLLOW US CATEGORIES

About ANGULAR ASP.NET

Contact
AWARDS AWS C#
Privacy Policy
DOT NET GENERAL
Sitemap
INTERVIEW QUESTIONS

BLOG STATS JAVA JAVA 8 LEARNING

48,964 hits MAVEN PYTHON

SPRING BOOT SQL SERVER

TIPS/TRICKS WEB API

Copyright © 2019 Code-Adda | WordPress Theme by MH Themes

12 of 12 6/28/2019, 9:17 AM

You might also like