8000 Merge branch 'master' of git://github.com/rabbitmq/rabbitmq-tutorials · syk-coder/rabbitmq-tutorials@5ed8ece · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ed8ece

Browse files
committed
Merge branch 'master' of git://github.com/rabbitmq/rabbitmq-tutorials
2 parents 1ae0ea2 + 6b92089 commit 5ed8ece

34 files changed

+921
-120
lines changed

dotnet/EmitLog.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using RabbitMQ.Client;
3+
4+
cla 1E0A ss EmitLog {
5+
public static void Main(string[] args) {
6+
ConnectionFactory factory = new ConnectionFactory();
7+
factory.HostName = "localhost";
8+
using (IConnection connection = factory.CreateConnection())
9+
using (IModel channel = connection.CreateModel()) {
10+
channel.ExchangeDeclare("logs", "fanout");
11+
12+
string message = (args.Length > 0) ? string.Join(" ", args)
13+
: "info: Hello World!";
14+
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
15+
channel.BasicPublish("logs", "", null, body);
16+
Console.WriteLine(" [x] Sent {0}", message);
17+
}
18+
}
19+
}

dotnet/EmitLogDirect.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Linq;
3+
using RabbitMQ.Client;
4+
5+
class EmitLogDirect {
6+
public static void Main(string[] args) {
7+
ConnectionFactory factory = new ConnectionFactory();
8+
factory.HostName = "localhost";
9+
using (IConnection connection = factory.CreateConnection())
10+
using (IModel channel = connection.CreateModel()) {
11+
channel.ExchangeDeclare("direct_logs", "direct");
12+
13+
string severity = (args.Length > 0) ? args[0] : "info";
14+
string message = (args.Length > 1) ? string.Join(" ", args.Skip(1)
15+
.ToArray())
16+
: "Hello World!";
17+
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
18+
channel.BasicPublish("direct_logs", severity, null, body);
19+
Console.WriteLine(" [x] Sent '{0}':'{1}'", severity, message);
20+
}
21+
}
22+
}

dotnet/EmitLogTopic.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Linq;
3+
using RabbitMQ.Client;
4+
5+
class EmitLogTopic {
6+
public static void Main(string[] args) {
7+
ConnectionFactory factory = new ConnectionFactory();
8+
factory.HostName = "localhost";
9+
using (IConnection connection = factory.CreateConnection())
10+
using (IModel channel = connection.CreateModel()) {
11+
channel.ExchangeDeclare("topic_logs", "topic");
12+
13+
string routingKey = (args.Length > 0) ? args[0] : "anonymous.info";
14+
string message = (args.Length > 1) ? string.Join(" ", args.Skip(1)
15+
.ToArray())
16+
: "Hello World!";
17+
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
18+
channel.BasicPublish("topic_logs", routingKey, null, body);
19+
Console.WriteLine(" [x] Sent '{0}':'{1}'", routingKey, message);
20+
}
21+
}
22+
}

dotnet/NewTask.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using RabbitMQ.Client;
3+
4+
class NewTask {
5+
public static void Main(string[] args) {
6+
ConnectionFactory factory = new ConnectionFactory();
7+
factory.HostName = "localhost";
8+
using (IConnection connection = factory.CreateConnection())
9+
using (IModel channel = connection.CreateModel()) {
10+
channel.QueueDeclare("task_queue", true, false, false, null);
11+
12+
string message = (args.Length > 0) ? string.Join(" ", args)
13+
: "Hello World!";
14+
byte[] body = 10000 System.Text.Encoding.UTF8.GetBytes(message);
15+
16+
IBasicProperties properties = channel.CreateBasicProperties();
17+
properties.DeliveryMode = 2;
18+
19+
channel.BasicPublish("", "task_queue", properties, body);
20+
Console.WriteLine(" [x] Sent {0}", message);
21+
}
22+
}
23+
}

dotnet/README.md

Lines changed: 110 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,148 @@
11
# Dotnet C# code for RabbitMQ tutorials
22

3-
Here you can find C# code examples for [RabbitMQ
3+
Here you can find the C# code examples for [RabbitMQ
44
tutorials](http://www.rabbitmq.com/getstarted.html).
55

6-
You'll need erlang installed, and also access to a [RabbitMQ server](http://www.rabbitmq.com/server.html).
7-
These are easy to [install](http://www.rabbitmq.com/install.html).
8-
6+
To successfully use the examples you will need a running RabbitMQ server.
97

108
## Requirements
119

12-
### Mono on Linux
10+
### Requirements on Windows
11+
12+
You need the RabbitMQ dotnet client.
13+
14+
* 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)
15+
* Extract it and copy "RabbitMQ.Client.dll" to your working folder.
16+
17+
You also need to ensure your system can find the C# compiler `csc.exe`,
18+
you may need to add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your
19+
Path.
20+
21+
We're using the command line (start->run cmd.exe) to
22+
compile and run the code. Alternatively you could use Visual Studio, but
23+
due to the nature of examples we prefer the command line.
24+
25+
### Requirements on Linux
26+
1327
You need Mono and RabbitMQ dotnet client.
1428

1529
sudo apt-get install mono-devel
1630
mkdir lib
1731
cd lib
18-
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1/rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
19-
unzip rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
32+
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.4.1/rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip
33+
unzip rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip
2034
cd ..
2135

22-
23-
### Windows
24-
You need the RabbitMQ dotnet client.
25-
26-
Go to http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1
27-
Download rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
28-
Extract it to rabbitmq-dotnet-client-2.1.1-dotnet-3.0 in your working folder
2936

30-
3137
## Code
3238

33-
For background, you can refer to [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
39+
#### [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html)
40+
41+
##### Windows
42+
43+
csc /r:"RabbitMQ.Client.dll" Send.cs
44+
csc /r:"RabbitMQ.Client.dll" Receive.cs
3445

46+
Send.exe
47+
Receive.exe
3548

36-
### Compile and run the C# examples using Mono on Linux.
49+
##### Linux
3750

3851
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
39-
MONO_PATH=lib/bin mono Send.exe
40-
4152
gmcs -r:lib/bin/RabbitMQ.Client.dll Receive.cs
53+
54+
MONO_PATH=lib/bin mono Send.exe
4255
MONO_PATH=lib/bin mono Receive.exe
43-
44-
45-
### Compile the C# examples on Windows
4656

47-
Ensure your system can find the c# compiler `csc.exe`
4857

49-
e.g. Add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your Path
50-
51-
If you put the whole client directory in your working directory:
58+
#### [Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-python.html)
5259

53-
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Send.cs
54-
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Receive.cs
5560

56-
or, if you just copy the RabbitMQ.Client.dll client library to your working directory:
61+
##### Windows
5762

58-
csc /r:"RabbitMQ.Client.dll" Send.cs
59-
csc /r:"RabbitMQ.Client.dll" Receive.cs
63+
csc /r:"RabbitMQ.Client.dll" NewTask.cs
64+
csc /r:"RabbitMQ.Client.dll" Worker.cs
6065

61-
or you could use MS Visual Studio.
66+
NewTask.exe
67+
Worker.exe
6268

69+
##### Linux
6370

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

66-
Open 3 Command Prompt windows Start > Run... cmd
74+
MONO_PATH=lib/bin mono NewTask.exe
75+
MONO_PATH=lib/bin mono Worker.exe
6776

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

71-
In the other two windows, navigate to your working directory to run the example client programs.
79+
##### Windows
7280

73-
In another cmd window, send a message:
74-
75-
Send.exe
81+
csc /r:"RabbitMQ.Client.dll" ReceiveLogs.cs
82+
csc /r:"RabbitMQ.Client.dll" EmitLog.cs
7683

77-
Check queue identified as "hello" has 1 message.
78-
In the final cmd window, set the listener going:
84+
ReceiveLogs.exe
85+
EmitLog.exe
7986

80-
Receive.exe
87+
##### Linux
88+
89+
gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogs.cs
90+
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLog.cs
91+
92+
MONO_PATH=lib/bin mono ReceiveLogs.exe
93+
MONO_PATH=lib/bin mono EmitLog.exe
94+
95+
#### [Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html)
96+
97+
##### Windows
98+
99+
csc /r:"RabbitMQ.Client.dll" ReceiveLogsDirect.cs
100+
csc /r:"RabbitMQ.Client.dll" EmitLogDirect.cs
101+
102+
ReceiveLogsDirect.exe
103+
EmitLogDirect.exe
104+
105+
##### Linux
106+
107+
gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogsDirect.cs
108+
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLogDirect.cs
109+
110+
MONO_PATH=lib/bin mono ReceiveLogsDirect.exe
111+
MONO_PATH=lib/bin mono EmitLogDirect.exe
112+
113+
#### [Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html)
114+
115+
##### Windows
116+
117+
csc /r:"RabbitMQ.Client.dll" ReceiveLogsTopic.cs
118+
csc /r:"RabbitMQ.Client.dll" EmitLogTopic.cs
119+
120+
ReceiveLogsTopic.exe
121+
EmitLogTopic.exe
122+
123+
##### Linux
124+
125+
gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogsTopic.cs
126+
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLogTopic.cs
127+
128+
MONO_PATH=lib/bin mono ReceiveLogsTopic.exe
129+
MONO_PATH=lib/bin mono EmitLogTopic.exe
130+
131+
#### [Tutorial six: RPC](http://www.rabbitmq.com/tutorial-six-python.html)
132+
133+
##### Windows
134+
135+
csc /r:"RabbitMQ.Client.dll" RPCServer.cs
136+
csc /r:"RabbitMQ.Client.dll" RPCClient.cs
137+
138+
RPCServer.exe
139+
RPCClient.exe
140+
141+
##### Linux
142+
143+
gmcs -r:lib/bin/RabbitMQ.Client.dll RPCServer.cs
144+
gmcs -r:lib/bin/RabbitMQ.Client.dll RPCClient.cs
145+
146+
MONO_PATH=lib/bin mono RPCServer.exe
147+
MONO_PATH=lib/bin mono RPCClient.exe
81148

82-
This will keep listening (Ctrl-C in this window will stop it) for messages.
83-
You should now see the first message, and the queue should be empty.
84-
The Receive view should get any further messages you Send.

dotnet/RPCClient.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using RabbitMQ.Client;
3+
using RabbitMQ.Client.Events;
4+
5+
class RPCClient : IDisposable {
6+
private IConnection connection;
7+
private IModel channel;
8+
private string replyQueueName;
9+
private QueueingBasicConsumer consumer;
10+
11+
public RPCClient() {
12+
ConnectionFactory factory = new ConnectionFactory();
13+
factory.HostName = "localhost";
14+
connection = factory.CreateConnection();
15+
channel = connection.CreateModel();
16+
replyQueueName = channel.QueueDeclare();
17+
consumer = new QueueingBasicConsumer(channel);
18+
channel.BasicConsume(replyQueueName, false, consumer);
19+
}
20+
21+
public string Call(string message) {
22+
string response = null;
23+
string corrId = Guid.NewGuid().ToString();
24+
IBasicProperties props = channel.CreateBasicProperties();
25+
props.ReplyTo = replyQueueName;
26+
props.CorrelationId = corrId;
27+
28+
byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(message);
29+
channel.BasicPublish("", "rpc_queue", props, messageBytes);
30+
31+
while (true) {
32+
BasicDeliverEventArgs ea =
33+
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
34+
if (ea.BasicProperties.CorrelationId == corrId) {
35+
byte[] body = ea.Body;
36+
response = System.Text.Encoding.UTF8.GetString(body);
37+
channel.BasicCancel(consumer.ConsumerTag);
38+
break;
39+
}
40+
}
41+
return response;
42+
}
43+
public void Dispose() {
44+
connection.Close();
45+
}
46+
47+
public static void Main() {
48+
Console.WriteLine(" [x] Requesting fib(30)");
49+
using (RPCClient rpcClient = new RPCClient()) {
50+
string response = rpcClient.Call("30");
51+
Console.WriteLine(" [.] Got '{0}'", response);
52+
}
53+
}
54+
}

dotnet/RPCServer.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using RabbitMQ.Client;
3+
using RabbitMQ.Client.Events;
4+
5+
class RPCServer {
6+
public static void Main() {
7+
ConnectionFactory factory = new ConnectionFactory();
8+
factory.HostName = "localhost";
9+
using (IConnection connection = factory.CreateConnection())
10+
using (IModel channel = connection.CreateModel()) {
11+
channel.QueueDeclare("rpc_queue", false, false, false, null);
12+
channel.BasicQos(0, 1, false);
13+
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
14+
channel.BasicConsume("rpc_queue", false, consumer);
15+
Console.WriteLine(" [x] Awaiting RPC requests");
16+
17+
while(true) {
18+
string response = null;
19+
BasicDeliverEventArgs ea =
20+
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
21+
22+
byte[] body = ea.Body;
23+
IBasicProperties props = ea.BasicProperties;
24+
IBasicProperties replyProps = channel.CreateBasicProperties();
25+
replyProps.CorrelationId = props.CorrelationId;
26+
27+
try {
28+
string message = System.Text.Encoding.UTF8.GetString(body);
29+
int n = int.Parse(message);
30+
Console.WriteLine(" [.] fib({0})", message);
31+
response = fib(n).ToString();
32+
} catch (Exception e) {
33+
Console.WriteLine(" [.] " + e);
34+
response = "";
35+
} finally {
36+
byte[] responseBytes =
37+
System.Text.Encoding.UTF8.GetBytes(response);
38+
channel.BasicPublish("", props.ReplyTo, replyProps,
39+
responseBytes);
40+
channel.BasicAck(ea.DeliveryTag, false);
41+
}
42+
}
43+
}
44+
}
45+
46+
private static int fib(int n) {
47+
if (n == 0 || n == 1) return n;
48+
return fib(n - 1) + fib(n - 2);
49+
}
50+
}

0 commit comments

Comments
 (0)
0