Updated dotnet code.
This commit is contained in:
parent
26422be9d9
commit
f2c3fb0e38
23
dotnet/EmitLog.cs
Normal file
23
dotnet/EmitLog.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace EmitLog {
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.HostName = "localhost";
|
||||
IConnection connection = factory.CreateConnection();
|
||||
IModel channel = connection.CreateModel();
|
||||
|
||||
channel.ExchangeDeclare("logs", "fanout");
|
||||
|
||||
string message = (args.Length > 0) ? System.String.Join(" ", args)
|
||||
: "info: Hello World!";
|
||||
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
|
||||
channel.BasicPublish("logs", "", null, body);
|
||||
System.Console.WriteLine(" [x] Sent {0}", message);
|
||||
|
||||
channel.Close();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
27
dotnet/NewTask.cs
Normal file
27
dotnet/NewTask.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace NewTask {
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.HostName = "localhost";
|
||||
IConnection connection = factory.CreateConnection();
|
||||
IModel channel = connection.CreateModel();
|
||||
|
||||
channel.QueueDeclare("task_queue", true, false, false, null);
|
||||
|
||||
string message = (args.Length > 0) ? System.String.Join(" ", args)
|
||||
: "Hello World!";
|
||||
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
|
||||
|
||||
var properties = channel.CreateBasicProperties();
|
||||
properties.DeliveryMode = 2;
|
||||
|
||||
channel.BasicPublish("", "task_queue", properties, body);
|
||||
System.Console.WriteLine(" [x] Sent {0}", message);
|
||||
|
||||
channel.Close();
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
124
dotnet/README.md
124
dotnet/README.md
@ -3,82 +3,88 @@
|
||||
Here you can find C# code examples for [RabbitMQ
|
||||
tutorials](http://www.rabbitmq.com/getstarted.html).
|
||||
|
||||
You'll need erlang installed, and also access to a [RabbitMQ server](http://www.rabbitmq.com/server.html).
|
||||
These are easy to [install](http://www.rabbitmq.com/install.html).
|
||||
|
||||
|
||||
## Requirements
|
||||
|
||||
### Mono on Linux
|
||||
### Requirements on Windows
|
||||
|
||||
You need the RabbitMQ dotnet client.
|
||||
|
||||
* Download [rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip](http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.4.1/rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip)
|
||||
* Extract it and copy "RabbitMQ.Client.dll" to your working folder.
|
||||
|
||||
You also need to nsure your system can find the C# compiler `csc.exe`,
|
||||
you may need to add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your
|
||||
Path.
|
||||
|
||||
We're suggesting to use the command line (start->run cmd.exe) to
|
||||
compile and run the code. Alternatively you could use Visual Studio, but
|
||||
due to the nature of examples command line is a preferred interface.
|
||||
|
||||
### Requirements on Linux
|
||||
|
||||
You need Mono and RabbitMQ dotnet client.
|
||||
|
||||
sudo apt-get install mono-devel
|
||||
mkdir lib
|
||||
cd lib
|
||||
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1/rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
|
||||
unzip rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
|
||||
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.4.1/rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip
|
||||
unzip rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip
|
||||
cd ..
|
||||
|
||||
|
||||
### Windows
|
||||
You need the RabbitMQ dotnet client.
|
||||
|
||||
Go to http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1
|
||||
Download rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
|
||||
Extract it to rabbitmq-dotnet-client-2.1.1-dotnet-3.0 in your working folder
|
||||
|
||||
|
||||
## Code
|
||||
|
||||
For background, you can refer to [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
|
||||
#### [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html)
|
||||
|
||||
|
||||
### Compile and run the C# examples using Mono on Linux.
|
||||
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
|
||||
MONO_PATH=lib/bin mono Send.exe
|
||||
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll Receive.cs
|
||||
MONO_PATH=lib/bin mono Receive.exe
|
||||
|
||||
|
||||
### Compile the C# examples on Windows
|
||||
|
||||
Ensure your system can find the c# compiler `csc.exe`
|
||||
|
||||
e.g. Add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your Path
|
||||
|
||||
If you put the whole client directory in your working directory:
|
||||
|
||||
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Send.cs
|
||||
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Receive.cs
|
||||
|
||||
or, if you just copy the RabbitMQ.Client.dll client library to your working directory:
|
||||
##### Windows
|
||||
|
||||
csc /r:"RabbitMQ.Client.dll" Send.cs
|
||||
csc /r:"RabbitMQ.Client.dll" Receive.cs
|
||||
|
||||
or you could use MS Visual Studio.
|
||||
|
||||
|
||||
### Run the example programs on Windows
|
||||
|
||||
Open 3 Command Prompt windows Start > Run... cmd
|
||||
|
||||
Use `rabbitmqctl status` to check the server is running,
|
||||
and `rabbitmqctl list_queues` to inspect the queue.
|
||||
|
||||
In the other two windows, navigate to your working directory to run the example client programs.
|
||||
|
||||
In another cmd window, send a message:
|
||||
|
||||
Send.exe
|
||||
|
||||
Check queue identified as "hello" has 1 message.
|
||||
In the final cmd window, set the listener going:
|
||||
|
||||
Receive.exe
|
||||
|
||||
This will keep listening (Ctrl-C in this window will stop it) for messages.
|
||||
You should now see the first message, and the queue should be empty.
|
||||
The Receive view should get any further messages you Send.
|
||||
##### Linux
|
||||
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll Receive.cs
|
||||
|
||||
MONO_PATH=lib/bin mono Send.exe
|
||||
MONO_PATH=lib/bin mono Receive.exe
|
||||
|
||||
|
||||
#### [Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-python.html):
|
||||
|
||||
##### Windows
|
||||
|
||||
csc /r:"RabbitMQ.Client.dll" NewTask.cs
|
||||
csc /r:"RabbitMQ.Client.dll" Worker.cs
|
||||
|
||||
NewTask.exe
|
||||
Worker.exe
|
||||
|
||||
##### Linux
|
||||
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll NewTask.cs
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll Worker.cs
|
||||
|
||||
MONO_PATH=lib/bin mono NewTask.exe
|
||||
MONO_PATH=lib/bin mono Worker.exe
|
||||
|
||||
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html)
|
||||
|
||||
##### Windows
|
||||
|
||||
csc /r:"RabbitMQ.Client.dll" ReceiveLogs.cs
|
||||
csc /r:"RabbitMQ.Client.dll" EmitLog.cs
|
||||
|
||||
ReceiveLogs.exe
|
||||
EmitLog.exe
|
||||
|
||||
##### Linux
|
||||
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogs.cs
|
||||
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLog.cs
|
||||
|
||||
MONO_PATH=lib/bin mono ReceiveLogs.exe
|
||||
MONO_PATH=lib/bin mono EmitLog.exe
|
||||
|
@ -9,10 +9,10 @@ static void Main(string[] args) {
|
||||
IConnection connection = factory.CreateConnection();
|
||||
IModel channel = connection.CreateModel();
|
||||
|
||||
channel.QueueDeclare("hello");
|
||||
channel.QueueDeclare("hello", false, false, false, null);
|
||||
|
||||
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
|
||||
channel.BasicConsume("hello", true, null, consumer);
|
||||
channel.BasicConsume("hello", true, consumer);
|
||||
|
||||
System.Console.WriteLine(" [*] Waiting for messages." +
|
||||
"To exit press CTRL+C");
|
||||
|
32
dotnet/ReceiveLogs.cs
Normal file
32
dotnet/ReceiveLogs.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
||||
namespace ReceiveLogs {
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.HostName = "localhost";
|
||||
IConnection connection = factory.CreateConnection();
|
||||
IModel channel = connection.CreateModel();
|
||||
|
||||
channel.ExchangeDeclare("logs", "fanout");
|
||||
|
||||
string queue_name = channel.QueueDeclare();
|
||||
|
||||
channel.QueueBind(queue_name, "logs", "");
|
||||
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
|
||||
channel.BasicConsume(queue_name, true, consumer);
|
||||
|
||||
System.Console.WriteLine(" [*] Waiting for logs." +
|
||||
"To exit press CTRL+C");
|
||||
while(true) {
|
||||
BasicDeliverEventArgs ea =
|
||||
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
|
||||
|
||||
byte[] body = ea.Body;
|
||||
string message = System.Text.Encoding.UTF8.GetString(body);
|
||||
System.Console.WriteLine(" [x] {0}", message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ static void Main(string[] args) {
|
||||
IConnection connection = factory.CreateConnection();
|
||||
IModel channel = connection.CreateModel();
|
||||
|
||||
channel.QueueDeclare("hello");
|
||||
channel.QueueDeclare("hello", false, false, false, null);
|
||||
|
||||
string message = "Hello World!";
|
||||
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
|
||||
|
37
dotnet/Worker.cs
Normal file
37
dotnet/Worker.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
|
||||
namespace Worker {
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.HostName = "localhost";
|
||||
IConnection connection = factory.CreateConnection();
|
||||
IModel channel = connection.CreateModel();
|
||||
|
||||
channel.QueueDeclare("task_queue", true, false, false, null);
|
||||
|
||||
channel.BasicQos(0, 1, false);
|
||||
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
|
||||
channel.BasicConsume("task_queue", false, consumer);
|
||||
|
||||
System.Console.WriteLine(" [*] Waiting for messages." +
|
||||
"To exit press CTRL+C");
|
||||
while(true) {
|
||||
BasicDeliverEventArgs ea =
|
||||
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
|
||||
|
||||
byte[] body = ea.Body;
|
||||
string message = System.Text.Encoding.UTF8.GetString(body);
|
||||
System.Console.WriteLine(" [x] Received {0}", message);
|
||||
|
||||
int dots = message.Split('.').Length - 1;
|
||||
System.Threading.Thread.Sleep(dots * 1000);
|
||||
|
||||
System.Console.WriteLine(" [x] Done");
|
||||
|
||||
channel.BasicAck(ea.DeliveryTag, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user