/
hello_world_producer.cs
61 lines (49 loc) · 1.92 KB
/
hello_world_producer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*##############################################
# RabbitMQ in Action
# Appendix A- Hello World Producer (.NET)
#
# Requires:
# * RabbitMQ.Client >= 2.7.0
#
# Author: Jason J. W. Williams
# (C)2011
##############################################*/
using System;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace HelloWorld {
class Producer {
public static void Main(string[] args) {
//#/(hwpdn.0) Collect server and message
if(args.Length < 2) {
Console.WriteLine("Must supply hostname and " +
"message text.");
Environment.Exit(-1);
}
var conn_factory = new ConnectionFactory();
conn_factory.HostName = args[0];
conn_factory.UserName = "guest";
conn_factory.Password = "guest";
//#/(hwpdn.1) Establish connection to broker
IConnection conn = conn_factory.CreateConnection();
IModel chan = conn.CreateModel(); //#/(hwpdn.2) Obtain channel
//#/(hwpdn.3) Declare the exchange
chan.ExchangeDeclare("hello-exchange",
ExchangeType.Direct,
true,
false,
null);
//#/(hwpdn.4) Create a plaintext message
var msg_body = args[1];
IBasicProperties msg_props = chan.CreateBasicProperties();
msg_props.ContentType = "text/plain";
//#/(hwpdn.5) Publish the message
chan.BasicPublish("hello-exchange",
"hola",
msg_props,
Encoding.ASCII.GetBytes(msg_body));
Environment.Exit(0);
}
}
}