Skip to content

Commit

Permalink
Rewrite .NET RPC Server to use EventingBasicConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
olsh committed Feb 11, 2016
1 parent 5f4af53 commit 2049e4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
18 changes: 10 additions & 8 deletions dotnet-visual-studio/6_RPCServer/Program.cs
Expand Up @@ -8,19 +8,18 @@ class Program
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "rpc_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
channel.BasicQos(0, 1, false);
var consumer = new QueueingBasicConsumer(channel);
var consumer = new EventingBasicConsumer(channel);
channel.BasicConsume(queue: "rpc_queue", noAck: false, consumer: consumer);
Console.WriteLine(" [x] Awaiting RPC requests");

while(true)
consumer.Received += (model, ea) =>
{
string response = null;
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var props = ea.BasicProperties;
Expand All @@ -34,7 +33,7 @@ public static void Main()
Console.WriteLine(" [.] fib({0})", message);
response = fib(n).ToString();
}
catch(Exception e)
catch (Exception e)
{
Console.WriteLine(" [.] " + e.Message);
response = "";
Expand All @@ -45,7 +44,10 @@ public static void Main()
channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
}
}
};

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}

Expand All @@ -55,7 +57,7 @@ public static void Main()
/// </summary>
private static int fib(int n)
{
if(n == 0 || n == 1)
if (n == 0 || n == 1)
{
return n;
}
Expand Down
18 changes: 10 additions & 8 deletions dotnet/RPCServer.cs
Expand Up @@ -8,19 +8,18 @@ class RPCServer
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "rpc_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
channel.BasicQos(0, 1, false);
var consumer = new QueueingBasicConsumer(channel);
var consumer = new EventingBasicConsumer(channel);
channel.BasicConsume(queue: "rpc_queue", noAck: false, consumer: consumer);
Console.WriteLine(" [x] Awaiting RPC requests");

while(true)
consumer.Received += (model, ea) =>
{
string response = null;
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var props = ea.BasicProperties;
Expand All @@ -34,7 +33,7 @@ public static void Main()
Console.WriteLine(" [.] fib({0})", message);
response = fib(n).ToString();
}
catch(Exception e)
catch (Exception e)
{
Console.WriteLine(" [.] " + e.Message);
response = "";
Expand All @@ -45,7 +44,10 @@ public static void Main()
channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
}
}
};

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}

Expand All @@ -55,7 +57,7 @@ public static void Main()
/// </summary>
private static int fib(int n)
{
if(n == 0 || n == 1)
if (n == 0 || n == 1)
{
return n;
}
Expand Down

0 comments on commit 2049e4c

Please sign in to comment.