Skip to content

Commit f283572

Browse files
Merge pull request rabbitmq#57 from Pliner/master
Named parameter version of the API rabbitmq#20
2 parents f39351d + c8e9c34 commit f283572

15 files changed

+214
-224
lines changed

dotnet-visual-studio/1_Receive/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void Main()
1111
using( var connection = factory.CreateConnection() )
1212
using( var channel = connection.CreateModel() )
1313
{
14-
channel.QueueDeclare( queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
14+
channel.QueueDeclare( queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null );
1515

1616
var consumer = new QueueingBasicConsumer( channel );
1717
channel.BasicConsume( queue: "hello", noAck: true, consumer: consumer );

dotnet-visual-studio/2_NewTask/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Main( string[] args )
1818
var properties = channel.CreateBasicProperties();
1919
properties.SetPersistent( true );
2020

21-
channel.BasicPublish(exchange: "", routingKey: "task_queue", basicProperties: properties, body: body );
21+
channel.BasicPublish( exchange: "", routingKey: "task_queue", basicProperties: properties, body: body );
2222
Console.WriteLine( " [x] Sent {0}", message );
2323
}
2424

dotnet-visual-studio/5_EmitLogTopic/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void Main( string[] args )
1616
var routingKey = ( args.Length > 0 ) ? args[0] : "anonymous.info";
1717
var message = ( args.Length > 1 ) ? string.Join( " ", args.Skip( 1 ).ToArray() ) : "Hello World!";
1818
var body = Encoding.UTF8.GetBytes( message );
19-
channel.BasicPublish( "topic_logs", routingKey, null, body );
19+
channel.BasicPublish( exchange: "topic_logs", routingKey: routingKey, basicProperties: null, body: body );
2020
Console.WriteLine( " [x] Sent '{0}':'{1}'", routingKey, message );
2121
}
2222
}

dotnet/EmitLog.cs

+13-10
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44

55
class EmitLog
66
{
7-
public static void Main(string[] args)
7+
public static void Main( string[] args )
88
{
99
var factory = new ConnectionFactory() { HostName = "localhost" };
10-
using (var connection = factory.CreateConnection())
11-
using (var channel = connection.CreateModel())
10+
using( var connection = factory.CreateConnection() )
11+
using( var channel = connection.CreateModel() )
1212
{
13-
channel.ExchangeDeclare("logs", "fanout");
13+
channel.ExchangeDeclare( exchange: "logs", type: "fanout" );
1414

15-
var message = GetMessage(args);
16-
var body = Encoding.UTF8.GetBytes(message);
17-
channel.BasicPublish("logs", "", null, body);
18-
Console.WriteLine(" [x] Sent {0}", message);
15+
var message = GetMessage( args );
16+
var body = Encoding.UTF8.GetBytes( message );
17+
channel.BasicPublish( exchange: "logs", routingKey: "", basicProperties: null, body: body );
18+
Console.WriteLine( " [x] Sent {0}", message );
1919
}
20+
21+
Console.WriteLine( " Press [enter] to exit." );
22+
Console.ReadLine();
2023
}
2124

22-
private static string GetMessage(string[] args)
25+
private static string GetMessage( string[] args )
2326
{
24-
return ((args.Length > 0) ? string.Join(" ", args) : "info: Hello World!");
27+
return ( ( args.Length > 0 ) ? string.Join( " ", args ) : "info: Hello World!" );
2528
}
2629
}

dotnet/EmitLogDirect.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55

66
class EmitLogDirect
77
{
8-
public static void Main(string[] args)
8+
public static void Main( string[] args )
99
{
1010
var factory = new ConnectionFactory() { HostName = "localhost" };
11-
using (var connection = factory.CreateConnection())
11+
using( var connection = factory.CreateConnection() )
12+
using( var channel = connection.CreateModel() )
1213
{
13-
using (var channel = connection.CreateModel())
14-
{
15-
channel.ExchangeDeclare("direct_logs", "direct");
14+
channel.ExchangeDeclare( exchange: "direct_logs", type: "direct" );
1615

17-
var severity = (args.Length > 0) ? args[0] : "info";
18-
var message = (args.Length > 1) ? string.Join(" ", args.Skip(1)
19-
.ToArray())
20-
: "Hello World!";
21-
var body = Encoding.UTF8.GetBytes(message);
22-
channel.BasicPublish("direct_logs", severity, null, body);
23-
Console.WriteLine(" [x] Sent '{0}':'{1}'", severity, message);
24-
}
16+
var severity = ( args.Length > 0 ) ? args[0] : "info";
17+
var message = ( args.Length > 1 ) ? string.Join( " ", args.Skip( 1 ).ToArray() ) : "Hello World!";
18+
var body = Encoding.UTF8.GetBytes( message );
19+
channel.BasicPublish( exchange: "direct_logs", routingKey: severity, basicProperties: null, body: body );
20+
Console.WriteLine( " [x] Sent '{0}':'{1}'", severity, message );
2521
}
22+
23+
Console.WriteLine( " Press [enter] to exit." );
24+
Console.ReadLine();
2625
}
2726
}

dotnet/EmitLogTopic.cs

+9-13
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55

66
class EmitLogTopic
77
{
8-
public static void Main(string[] args)
8+
public static void Main( string[] args )
99
{
1010
var factory = new ConnectionFactory() { HostName = "localhost" };
11-
using (var connection = factory.CreateConnection())
11+
using( var connection = factory.CreateConnection() )
12+
using( var channel = connection.CreateModel() )
1213
{
13-
using (var channel = connection.CreateModel())
14-
{
15-
channel.ExchangeDeclare("topic_logs", "topic");
14+
channel.ExchangeDeclare( exchange: "topic_logs", type: "topic" );
1615

17-
var routingKey = (args.Length > 0) ? args[0] : "anonymous.info";
18-
var message = (args.Length > 1) ? string.Join(" ", args.Skip(1)
19-
.ToArray())
20-
: "Hello World!";
21-
var body = Encoding.UTF8.GetBytes(message);
22-
channel.BasicPublish("topic_logs", routingKey, null, body);
23-
Console.WriteLine(" [x] Sent '{0}':'{1}'", routingKey, message);
24-
}
16+
var routingKey = ( args.Length > 0 ) ? args[0] : "anonymous.info";
17+
var message = ( args.Length > 1 ) ? string.Join( " ", args.Skip( 1 ).ToArray() ) : "Hello World!";
18+
var body = Encoding.UTF8.GetBytes( message );
19+
channel.BasicPublish( exchange: "topic_logs", routingKey: routingKey, basicProperties: null, body: body );
20+
Console.WriteLine( " [x] Sent '{0}':'{1}'", routingKey, message );
2521
}
2622
}
2723
}

dotnet/NewTask.cs

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,30 @@
44

55
class NewTask
66
{
7-
public static void Main(string[] args)
7+
public static void Main( string[] args )
88
{
99
var factory = new ConnectionFactory() { HostName = "localhost" };
10-
using (var connection = factory.CreateConnection())
10+
using( var connection = factory.CreateConnection() )
11+
using( var channel = connection.CreateModel() )
1112
{
12-
using (var channel = connection.CreateModel())
13-
{
14-
channel.QueueDeclare("task_queue", true, false, false, null);
13+
channel.QueueDeclare( queue: "task_queue", durable: true, exclusive: false, autoDelete: false, arguments: null );
1514

16-
var message = GetMessage(args);
17-
var body = Encoding.UTF8.GetBytes(message);
15+
var message = GetMessage( args );
16+
var body = Encoding.UTF8.GetBytes( message );
1817

19-
var properties = channel.CreateBasicProperties();
20-
properties.SetPersistent(true);
18+
var properties = channel.CreateBasicProperties();
19+
properties.SetPersistent( true );
2120

22-
channel.BasicPublish("", "task_queue", properties, body);
23-
Console.WriteLine(" [x] Sent {0}", message);
24-
}
21+
channel.BasicPublish( exchange: "", routingKey: "task_queue", basicProperties: properties, body: body );
22+
Console.WriteLine( " [x] Sent {0}", message );
2523
}
24+
25+
Console.WriteLine( " Press [enter] to exit." );
26+
Console.ReadLine();
2627
}
2728

28-
private static string GetMessage(string[] args)
29+
private static string GetMessage( string[] args )
2930
{
30-
return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!");
31+
return ( ( args.Length > 0 ) ? string.Join( " ", args ) : "Hello World!" );
3132
}
3233
}

dotnet/RPCClient.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
26
using RabbitMQ.Client;
37
using RabbitMQ.Client.Events;
4-
using System.Text;
58

69
class RPCClient
710
{
@@ -16,26 +19,26 @@ public RPCClient()
1619
connection = factory.CreateConnection();
1720
channel = connection.CreateModel();
1821
replyQueueName = channel.QueueDeclare().QueueName;
19-
consumer = new QueueingBasicConsumer(channel);
20-
channel.BasicConsume(replyQueueName, true, consumer);
22+
consumer = new QueueingBasicConsumer( channel );
23+
channel.BasicConsume( queue: replyQueueName, noAck: true, consumer: consumer );
2124
}
2225

23-
public string Call(string message)
26+
public string Call( string message )
2427
{
2528
var corrId = Guid.NewGuid().ToString();
2629
var props = channel.CreateBasicProperties();
2730
props.ReplyTo = replyQueueName;
2831
props.CorrelationId = corrId;
2932

30-
var messageBytes = Encoding.UTF8.GetBytes(message);
31-
channel.BasicPublish("", "rpc_queue", props, messageBytes);
33+
var messageBytes = Encoding.UTF8.GetBytes( message );
34+
channel.BasicPublish( exchange: "", routingKey: "rpc_queue", basicProperties: props, body: messageBytes );
3235

33-
while (true)
36+
while( true )
3437
{
3538
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
36-
if (ea.BasicProperties.CorrelationId == corrId)
39+
if( ea.BasicProperties.CorrelationId == corrId )
3740
{
38-
return Encoding.UTF8.GetString(ea.Body);
41+
return Encoding.UTF8.GetString( ea.Body );
3942
}
4043
}
4144
}

dotnet/RPCServer.cs

+43-40
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,58 @@ class RPCServer
88
public static void Main()
99
{
1010
var factory = new ConnectionFactory() { HostName = "localhost" };
11-
using (var connection = factory.CreateConnection())
11+
using( var connection = factory.CreateConnection() )
12+
using( var channel = connection.CreateModel() )
1213
{
13-
using (var channel = connection.CreateModel())
14-
{
15-
channel.QueueDeclare("rpc_queue", false, false, false, null);
16-
channel.BasicQos(0, 1, false);
17-
var consumer = new QueueingBasicConsumer(channel);
18-
channel.BasicConsume("rpc_queue", false, consumer);
19-
Console.WriteLine(" [x] Awaiting RPC requests");
14+
channel.QueueDeclare( queue: "rpc_queue", durable: false, exclusive: false, autoDelete: false, arguments: null );
15+
channel.BasicQos( 0, 1, false );
16+
var consumer = new QueueingBasicConsumer( channel );
17+
channel.BasicConsume( queue: "rpc_queue", noAck: false, consumer: consumer );
18+
Console.WriteLine( " [x] Awaiting RPC requests" );
2019

21-
while (true)
22-
{
23-
string response = null;
24-
var ea =
25-
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
20+
while( true )
21+
{
22+
string response = null;
23+
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
2624

27-
var body = ea.Body;
28-
var props = ea.BasicProperties;
29-
var replyProps = channel.CreateBasicProperties();
30-
replyProps.CorrelationId = props.CorrelationId;
25+
var body = ea.Body;
26+
var props = ea.BasicProperties;
27+
var replyProps = channel.CreateBasicProperties();
28+
replyProps.CorrelationId = props.CorrelationId;
3129

32-
try
33-
{
34-
var message = Encoding.UTF8.GetString(body);
35-
int n = int.Parse(message);
36-
Console.WriteLine(" [.] fib({0})", message);
37-
response = fib(n).ToString();
38-
}
39-
catch (Exception e)
40-
{
41-
Console.WriteLine(" [.] " + e.Message);
42-
response = "";
43-
}
44-
finally
45-
{
46-
var responseBytes =
47-
Encoding.UTF8.GetBytes(response);
48-
channel.BasicPublish("", props.ReplyTo, replyProps,
49-
responseBytes);
50-
channel.BasicAck(ea.DeliveryTag, false);
51-
}
30+
try
31+
{
32+
var message = Encoding.UTF8.GetString( body );
33+
int n = int.Parse( message );
34+
Console.WriteLine( " [.] fib({0})", message );
35+
response = fib( n ).ToString();
36+
}
37+
catch( Exception e )
38+
{
39+
Console.WriteLine( " [.] " + e.Message );
40+
response = "";
41+
}
42+
finally
43+
{
44+
var responseBytes = Encoding.UTF8.GetBytes( response );
45+
channel.BasicPublish( exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes );
46+
channel.BasicAck( deliveryTag: ea.DeliveryTag, multiple: false );
5247
}
5348
}
5449
}
5550
}
5651

57-
private static int fib(int n)
52+
/// <summary>
53+
/// Assumes only valid positive integer input.
54+
/// Don't expect this one to work for big numbers, and it's probably the slowest recursive implementation possible.
55+
/// </summary>
56+
private static int fib( int n )
5857
{
59-
if (n == 0 || n == 1) return n;
60-
return fib(n - 1) + fib(n - 2);
58+
if( n == 0 || n == 1 )
59+
{
60+
return n;
61+
}
62+
63+
return fib( n - 1 ) + fib( n - 2 );
6164
}
6265
}

dotnet/Receive.cs

+12-15
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,22 @@ class Receive
88
public static void Main()
99
{
1010
var factory = new ConnectionFactory() { HostName = "localhost" };
11-
using (var connection = factory.CreateConnection())
11+
using( var connection = factory.CreateConnection() )
12+
using( var channel = connection.CreateModel() )
1213
{
13-
using (var channel = connection.CreateModel())
14-
{
15-
channel.QueueDeclare("hello", false, false, false, null);
14+
channel.QueueDeclare( queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null );
1615

17-
var consumer = new QueueingBasicConsumer(channel);
18-
channel.BasicConsume("hello", true, consumer);
16+
var consumer = new QueueingBasicConsumer( channel );
17+
channel.BasicConsume( queue: "hello", noAck: true, consumer: consumer );
1918

20-
Console.WriteLine(" [*] Waiting for messages." +
21-
"To exit press CTRL+C");
22-
while (true)
23-
{
24-
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
19+
Console.WriteLine( " [*] Waiting for messages. To exit press CTRL+C" );
20+
while( true )
21+
{
22+
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
2523

26-
var body = ea.Body;
27-
var message = Encoding.UTF8.GetString(body);
28-
Console.WriteLine(" [x] Received {0}", message);
29-
}
24+
var body = ea.Body;
25+
var message = Encoding.UTF8.GetString( body );
26+
Console.WriteLine( " [x] Received {0}", message );
3027
}
3128
}
3229
}

0 commit comments

Comments
 (0)