Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/rabbitmq/rabbitmq-tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
emile committed Jun 19, 2011
2 parents 1ae0ea2 + 6b92089 commit 5ed8ece
Show file tree
Hide file tree
Showing 34 changed files with 921 additions and 120 deletions.
19 changes: 19 additions & 0 deletions dotnet/EmitLog.cs
@@ -0,0 +1,19 @@
using System;
using RabbitMQ.Client;

class EmitLog {
public static void Main(string[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.ExchangeDeclare("logs", "fanout");

string message = (args.Length > 0) ? string.Join(" ", args)
: "info: Hello World!";
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
channel.BasicPublish("logs", "", null, body);
Console.WriteLine(" [x] Sent {0}", message);
}
}
}
22 changes: 22 additions & 0 deletions dotnet/EmitLogDirect.cs
@@ -0,0 +1,22 @@
using System;
using System.Linq;
using RabbitMQ.Client;

class EmitLogDirect {
public static void Main(string[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.ExchangeDeclare("direct_logs", "direct");

string severity = (args.Length > 0) ? args[0] : "info";
string message = (args.Length > 1) ? string.Join(" ", args.Skip(1)
.ToArray())
: "Hello World!";
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
channel.BasicPublish("direct_logs", severity, null, body);
Console.WriteLine(" [x] Sent '{0}':'{1}'", severity, message);
}
}
}
22 changes: 22 additions & 0 deletions dotnet/EmitLogTopic.cs
@@ -0,0 +1,22 @@
using System;
using System.Linq;
using RabbitMQ.Client;

class EmitLogTopic {
public static void Main(string[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.ExchangeDeclare("topic_logs", "topic");

string routingKey = (args.Length > 0) ? args[0] : "anonymous.info";
string message = (args.Length > 1) ? string.Join(" ", args.Skip(1)
.ToArray())
: "Hello World!";
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
channel.BasicPublish("topic_logs", routingKey, null, body);
Console.WriteLine(" [x] Sent '{0}':'{1}'", routingKey, message);
}
}
}
23 changes: 23 additions & 0 deletions dotnet/NewTask.cs
@@ -0,0 +1,23 @@
using System;
using RabbitMQ.Client;

class NewTask {
public static void Main(string[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.QueueDeclare("task_queue", true, false, false, null);

string message = (args.Length > 0) ? string.Join(" ", args)
: "Hello World!";
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);

IBasicProperties properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;

channel.BasicPublish("", "task_queue", properties, body);
Console.WriteLine(" [x] Sent {0}", message);
}
}
}
156 changes: 110 additions & 46 deletions dotnet/README.md
@@ -1,84 +1,148 @@
# Dotnet C# code for RabbitMQ tutorials

Here you can find C# code examples for [RabbitMQ
Here you can find the 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).

To successfully use the examples you will need a running RabbitMQ server.

## 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 ensure 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 using 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 we prefer the command line.

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

##### Windows

csc /r:"RabbitMQ.Client.dll" Send.cs
csc /r:"RabbitMQ.Client.dll" Receive.cs

Send.exe
Receive.exe

### Compile and run the C# examples using Mono on Linux.
##### 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 Send.exe
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:
#### [Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-python.html)

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
csc /r:"RabbitMQ.Client.dll" NewTask.cs
csc /r:"RabbitMQ.Client.dll" Worker.cs

or you could use MS Visual Studio.
NewTask.exe
Worker.exe

##### Linux

### Run the example programs on Windows
gmcs -r:lib/bin/RabbitMQ.Client.dll NewTask.cs
gmcs -r:lib/bin/RabbitMQ.Client.dll Worker.cs

Open 3 Command Prompt windows Start > Run... cmd
MONO_PATH=lib/bin mono NewTask.exe
MONO_PATH=lib/bin mono Worker.exe

Use `rabbitmqctl status` to check the server is running,
and `rabbitmqctl list_queues` to inspect the queue.
#### [Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html)

In the other two windows, navigate to your working directory to run the example client programs.
##### Windows

In another cmd window, send a message:

Send.exe
csc /r:"RabbitMQ.Client.dll" ReceiveLogs.cs
csc /r:"RabbitMQ.Client.dll" EmitLog.cs

Check queue identified as "hello" has 1 message.
In the final cmd window, set the listener going:
ReceiveLogs.exe
EmitLog.exe

Receive.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

#### [Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html)

##### Windows

csc /r:"RabbitMQ.Client.dll" ReceiveLogsDirect.cs
csc /r:"RabbitMQ.Client.dll" EmitLogDirect.cs

ReceiveLogsDirect.exe
EmitLogDirect.exe

##### Linux

gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogsDirect.cs
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLogDirect.cs

MONO_PATH=lib/bin mono ReceiveLogsDirect.exe
MONO_PATH=lib/bin mono EmitLogDirect.exe

#### [Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html)

##### Windows

csc /r:"RabbitMQ.Client.dll" ReceiveLogsTopic.cs
csc /r:"RabbitMQ.Client.dll" EmitLogTopic.cs

ReceiveLogsTopic.exe
EmitLogTopic.exe

##### Linux

gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogsTopic.cs
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLogTopic.cs

MONO_PATH=lib/bin mono ReceiveLogsTopic.exe
MONO_PATH=lib/bin mono EmitLogTopic.exe

#### [Tutorial six: RPC](http://www.rabbitmq.com/tutorial-six-python.html)

##### Windows

csc /r:"RabbitMQ.Client.dll" RPCServer.cs
csc /r:"RabbitMQ.Client.dll" RPCClient.cs

RPCServer.exe
RPCClient.exe

##### Linux

gmcs -r:lib/bin/RabbitMQ.Client.dll RPCServer.cs
gmcs -r:lib/bin/RabbitMQ.Client.dll RPCClient.cs

MONO_PATH=lib/bin mono RPCServer.exe
MONO_PATH=lib/bin mono RPCClient.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.
54 changes: 54 additions & 0 deletions dotnet/RPCClient.cs
@@ -0,0 +1,54 @@
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

class RPCClient : IDisposable {
private IConnection connection;
private IModel channel;
private string replyQueueName;
private QueueingBasicConsumer consumer;

public RPCClient() {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
connection = factory.CreateConnection();
channel = connection.CreateModel();
replyQueueName = channel.QueueDeclare();
consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(replyQueueName, false, consumer);
}

public string Call(string message) {
string response = null;
string corrId = Guid.NewGuid().ToString();
IBasicProperties props = channel.CreateBasicProperties();
props.ReplyTo = replyQueueName;
props.CorrelationId = corrId;

byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "rpc_queue", props, messageBytes);

while (true) {
BasicDeliverEventArgs ea =
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
if (ea.BasicProperties.CorrelationId == corrId) {
byte[] body = ea.Body;
response = System.Text.Encoding.UTF8.GetString(body);
channel.BasicCancel(consumer.ConsumerTag);
break;
}
}
return response;
}
public void Dispose() {
connection.Close();
}

public static void Main() {
Console.WriteLine(" [x] Requesting fib(30)");
using (RPCClient rpcClient = new RPCClient()) {
string response = rpcClient.Call("30");
Console.WriteLine(" [.] Got '{0}'", response);
}
}
}
50 changes: 50 additions & 0 deletions dotnet/RPCServer.cs
@@ -0,0 +1,50 @@
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

class RPCServer {
public static void Main() {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.QueueDeclare("rpc_queue", false, false, false, null);
channel.BasicQos(0, 1, false);
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("rpc_queue", false, consumer);
Console.WriteLine(" [x] Awaiting RPC requests");

while(true) {
string response = null;
BasicDeliverEventArgs ea =
(BasicDeliverEventArgs)consumer.Queue.Dequeue();

byte[] body = ea.Body;
IBasicProperties props = ea.BasicProperties;
IBasicProperties replyProps = channel.CreateBasicProperties();
replyProps.CorrelationId = props.CorrelationId;

try {
string message = System.Text.Encoding.UTF8.GetString(body);
int n = int.Parse(message);
Console.WriteLine(" [.] fib({0})", message);
response = fib(n).ToString();
} catch (Exception e) {
Console.WriteLine(" [.] " + e);
response = "";
} finally {
byte[] responseBytes =
System.Text.Encoding.UTF8.GetBytes(response);
channel.BasicPublish("", props.ReplyTo, replyProps,
responseBytes);
channel.BasicAck(ea.DeliveryTag, false);
}
}
}
}

private static int fib(int n) {
if (n == 0 || n == 1) return n;
return fib(n - 1) + fib(n - 2);
}
}

0 comments on commit 5ed8ece

Please sign in to comment.