|
7 | 7 |
|
8 | 8 | public class RPCClient { |
9 | 9 |
|
10 | | - static class FibonacciRpcClient { |
11 | | - private Connection connection; |
12 | | - private Channel channel; |
13 | | - private String requestQueueName = "rpc_queue"; |
14 | | - private String replyQueueName; |
15 | | - private QueueingConsumer consumer; |
| 10 | + private Connection connection; |
| 11 | + private Channel channel; |
| 12 | + private String requestQueueName = "rpc_queue"; |
| 13 | + private String replyQueueName; |
| 14 | + private QueueingConsumer consumer; |
16 | 15 |
|
17 | | - public FibonacciRpcClient() throws Exception { |
18 | | - ConnectionFactory factory = new ConnectionFactory(); |
19 | | - factory.setHost("localhost"); |
20 | | - connection = factory.newConnection(); |
21 | | - channel = connection.createChannel(); |
| 16 | + public RPCClient() throws Exception { |
| 17 | + ConnectionFactory factory = new ConnectionFactory(); |
| 18 | + factory.setHost("localhost"); |
| 19 | + connection = factory.newConnection(); |
| 20 | + channel = connection.createChannel(); |
22 | 21 |
|
23 | | - replyQueueName = channel.queueDeclare().getQueue(); |
24 | | - consumer = new QueueingConsumer(channel); |
25 | | - channel.basicConsume(replyQueueName, true, consumer); |
26 | | - } |
| 22 | + replyQueueName = channel.queueDeclare().getQueue(); |
| 23 | + consumer = new QueueingConsumer(channel); |
| 24 | + channel.basicConsume(replyQueueName, true, consumer); |
| 25 | + } |
27 | 26 |
|
28 | | - public String call(String message) throws Exception { |
29 | | - String response = null; |
30 | | - boolean replied = false; |
31 | | - String corrId = UUID.randomUUID().toString(); |
| 27 | + public String call(String message) throws Exception { |
| 28 | + String response = null; |
| 29 | + String corrId = UUID.randomUUID().toString(); |
32 | 30 |
|
33 | | - BasicProperties props = new BasicProperties(); |
34 | | - props.setReplyTo(replyQueueName); |
35 | | - props.setCorrelationId(corrId); |
| 31 | + BasicProperties props = new BasicProperties(); |
| 32 | + props.setReplyTo(replyQueueName); |
| 33 | + props.setCorrelationId(corrId); |
36 | 34 |
|
37 | | - channel.basicPublish("", requestQueueName, props, message.getBytes()); |
| 35 | + channel.basicPublish("", requestQueueName, props, message.getBytes()); |
38 | 36 |
|
39 | | - while (replied == false) { |
40 | | - QueueingConsumer.Delivery delivery = consumer.nextDelivery(); |
41 | | - if (delivery.getProperties().getCorrelationId().compareTo(corrId) == 0) { |
42 | | - response = new String(delivery.getBody()); |
43 | | - replied = true; |
44 | | - } |
| 37 | + while (true) { |
| 38 | + QueueingConsumer.Delivery delivery = consumer.nextDelivery(); |
| 39 | + if (delivery.getProperties().getCorrelationId().equals(corrId)) { |
| 40 | + response = new String(delivery.getBody(),"UTF-8"); |
| 41 | + break; |
45 | 42 | } |
46 | | - |
47 | | - return response; |
48 | 43 | } |
| 44 | + |
| 45 | + return response; |
| 46 | + } |
49 | 47 |
|
50 | | - public void close() throws Exception { |
51 | | - channel.close(); |
52 | | - connection.close(); |
53 | | - } |
| 48 | + public void close() throws Exception { |
| 49 | + connection.close(); |
54 | 50 | } |
55 | 51 |
|
56 | 52 | public static void main(String[] argv) { |
57 | | - RPCClient.FibonacciRpcClient fibonacciRpc = null; |
| 53 | + RPCClient fibonacciRpc = null; |
58 | 54 | String response = null; |
59 | 55 | try { |
60 | | - fibonacciRpc = new RPCClient.FibonacciRpcClient(); |
| 56 | + fibonacciRpc = new RPCClient(); |
61 | 57 |
|
62 | 58 | System.out.println(" [x] Requesting fib(30)"); |
63 | 59 | response = fibonacciRpc.call("30"); |
|
0 commit comments