1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
2
+ using System . Collections . Concurrent ;
4
3
using System . Text ;
5
- using System . Threading . Tasks ;
6
4
using RabbitMQ . Client ;
7
5
using RabbitMQ . Client . Events ;
8
6
9
- class RPCClient
7
+ public class RpcClient
10
8
{
11
- private IConnection connection ;
12
- private IModel channel ;
13
- private string replyQueueName ;
14
- private QueueingBasicConsumer consumer ;
9
+ private readonly IConnection connection ;
10
+ private readonly IModel channel ;
11
+ private readonly string replyQueueName ;
12
+ private readonly EventingBasicConsumer consumer ;
13
+ private readonly BlockingCollection < string > respQueue = new BlockingCollection < string > ( ) ;
14
+ private readonly IBasicProperties props ;
15
15
16
- public RPCClient ( )
16
+ public RpcClient ( )
17
17
{
18
18
var factory = new ConnectionFactory ( ) { HostName = "localhost" } ;
19
+
19
20
connection = factory . CreateConnection ( ) ;
20
21
channel = connection . CreateModel ( ) ;
21
22
replyQueueName = channel . QueueDeclare ( ) . QueueName ;
22
- consumer = new QueueingBasicConsumer ( channel ) ;
23
- channel . BasicConsume ( queue : replyQueueName , autoAck : true , consumer : consumer ) ;
23
+ consumer = new EventingBasicConsumer ( channel ) ;
24
+
25
+ props = channel . CreateBasicProperties ( ) ;
26
+ var correlationId = Guid . NewGuid ( ) . ToString ( ) ;
27
+ props . CorrelationId = correlationId ;
28
+ props . ReplyTo = replyQueueName ;
29
+
30
+ consumer . Received += ( model , ea ) =>
31
+ {
32
+ var body = ea . Body ;
33
+ var response = Encoding . UTF8 . GetString ( body ) ;
34
+ if ( ea . BasicProperties . CorrelationId == correlationId )
35
+ {
36
+ respQueue . Add ( response ) ;
37
+ }
38
+ } ;
24
39
}
25
40
26
41
public string Call ( string message )
27
42
{
28
- var corrId = Guid . NewGuid ( ) . ToString ( ) ;
29
- var props = channel . CreateBasicProperties ( ) ;
30
- props . ReplyTo = replyQueueName ;
31
- props . CorrelationId = corrId ;
32
43
33
44
var messageBytes = Encoding . UTF8 . GetBytes ( message ) ;
34
- channel . BasicPublish ( exchange : "" , routingKey : "rpc_queue" , basicProperties : props , body : messageBytes ) ;
45
+ channel . BasicPublish (
46
+ exchange : "" ,
47
+ routingKey : "rpc_queue" ,
48
+ basicProperties : props ,
49
+ body : messageBytes ) ;
35
50
36
- while ( true )
37
- {
38
- var ea = ( BasicDeliverEventArgs ) consumer . Queue . Dequeue ( ) ;
39
- if ( ea . BasicProperties . CorrelationId == corrId )
40
- {
41
- return Encoding . UTF8 . GetString ( ea . Body ) ;
42
- }
43
- }
51
+
52
+ channel . BasicConsume (
53
+ consumer : consumer ,
54
+ queue : replyQueueName ,
55
+ autoAck : true ) ;
56
+
57
+ return respQueue . Take ( ) ; ;
44
58
}
45
59
46
60
public void Close ( )
@@ -49,16 +63,17 @@ public void Close()
49
63
}
50
64
}
51
65
52
- class RPC
66
+ public class Rpc
53
67
{
54
68
public static void Main ( )
55
69
{
56
- var rpcClient = new RPCClient ( ) ;
70
+ var rpcClient = new RpcClient ( ) ;
57
71
58
72
Console . WriteLine ( " [x] Requesting fib(30)" ) ;
59
73
var response = rpcClient . Call ( "30" ) ;
60
- Console . WriteLine ( " [.] Got '{0}'" , response ) ;
61
74
75
+ Console . WriteLine ( " [.] Got '{0}'" , response ) ;
62
76
rpcClient . Close ( ) ;
63
77
}
64
78
}
79
+
0 commit comments