Rewrite .NET RPC Server to use EventingBasicConsumer

This commit is contained in:
Oleg Shevchenko 2016-02-11 17:10:29 +03:00
parent 5f4af53774
commit 2049e4c286
2 changed files with 20 additions and 16 deletions

View File

@ -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;
@ -34,7 +33,7 @@ class Program
Console.WriteLine(" [.] fib({0})", message);
response = fib(n).ToString();
}
catch(Exception e)
catch (Exception e)
{
Console.WriteLine(" [.] " + e.Message);
response = "";
@ -45,7 +44,10 @@ class Program
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();
}
}
@ -55,7 +57,7 @@ class Program
/// </summary>
private static int fib(int n)
{
if(n == 0 || n == 1)
if (n == 0 || n == 1)
{
return n;
}

View File

@ -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;
@ -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 = "";
@ -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();
}
}
@ -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;
}