|
4 | 4 | import com.rabbitmq.client.QueueingConsumer; |
5 | 5 | import com.rabbitmq.client.AMQP.BasicProperties; |
6 | 6 | import java.util.UUID; |
7 | | - |
| 7 | + |
8 | 8 | public class RPCClient { |
9 | | - |
10 | | - private static final String RPC_QUEUE_NAME = "rpc_queue"; |
11 | | - |
12 | | - static class FibonacciClient { |
| 9 | + |
| 10 | + static class FibonacciRpcClient { |
| 11 | + private Connection connection; |
13 | 12 | private Channel channel; |
14 | | - private String queue; |
| 13 | + private String requestQueueName = "rpc_queue"; |
| 14 | + private String replyQueueName; |
| 15 | + private QueueingConsumer consumer; |
15 | 16 |
|
16 | | - public FibonacciClient(Channel chann, String queueName){ |
17 | | - channel = chann; |
18 | | - queue = queueName; |
| 17 | + public FibonacciRpcClient() throws Exception { |
| 18 | + ConnectionFactory factory = new ConnectionFactory(); |
| 19 | + factory.setHost("localhost"); |
| 20 | + connection = factory.newConnection(); |
| 21 | + channel = connection.createChannel(); |
| 22 | + |
| 23 | + replyQueueName = channel.queueDeclare().getQueue(); |
| 24 | + consumer = new QueueingConsumer(channel); |
| 25 | + channel.basicConsume(replyQueueName, true, consumer); |
19 | 26 | } |
20 | | - |
21 | | - public String call(String message) throws Exception { |
22 | | - String replyQueueName = channel.queueDeclare().getQueue(); |
| 27 | + |
| 28 | + public String call(String message) throws Exception { |
| 29 | + String response = null; |
| 30 | + boolean replied = false; |
23 | 31 | String corrId = UUID.randomUUID().toString(); |
| 32 | + |
24 | 33 | BasicProperties props = new BasicProperties(); |
25 | 34 | props.setReplyTo(replyQueueName); |
26 | 35 | props.setCorrelationId(corrId); |
27 | | - |
28 | | - QueueingConsumer consumer = new QueueingConsumer(channel); |
29 | | - channel.basicConsume(replyQueueName, true, consumer); |
30 | | - |
31 | | - channel.basicPublish("", queue, props, message.getBytes()); |
32 | | - |
33 | | - String response = ""; |
34 | | - |
35 | | - while (!validResponse(response)){ |
36 | | - QueueingConsumer.Delivery delivery = consumer.nextDelivery(); |
37 | | - response = new String(delivery.getBody()); |
| 36 | + |
| 37 | + channel.basicPublish("", requestQueueName, props, message.getBytes()); |
| 38 | + |
| 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 | + } |
38 | 45 | } |
39 | | - return response; |
| 46 | + |
| 47 | + return response; |
40 | 48 | } |
41 | 49 |
|
42 | | - private Boolean validResponse(String response){ |
43 | | - return (response.length() > 0); |
| 50 | + public void close() throws Exception { |
| 51 | + channel.close(); |
| 52 | + connection.close(); |
44 | 53 | } |
45 | 54 | } |
46 | 55 |
|
47 | | - private static Connection defaultConnection() throws Exception { |
48 | | - ConnectionFactory factory = new ConnectionFactory(); |
49 | | - factory.setHost("localhost"); |
50 | | - return factory.newConnection(); |
51 | | - } |
| 56 | + public static void main(String[] argv) { |
| 57 | + RPCClient.FibonacciRpcClient fibonacciRpc = null; |
| 58 | + String response = null; |
| 59 | + try { |
| 60 | + fibonacciRpc = new RPCClient.FibonacciRpcClient(); |
52 | 61 |
|
53 | | - public static void main(String[] argv) throws Exception { |
54 | | - |
55 | | - Connection connection = defaultConnection(); |
56 | | - Channel channel = connection.createChannel(); |
57 | | - |
58 | | - RPCClient.FibonacciClient rpc = new RPCClient.FibonacciClient(channel, RPC_QUEUE_NAME); |
59 | | - |
60 | | - System.out.println(" [x] Requesting fib(30)"); |
61 | | - String response = rpc.call("30"); |
62 | | - |
63 | | - System.out.println(" [.] Got '" + response + "'"); |
64 | | - |
65 | | - channel.close(); |
66 | | - connection.close(); |
67 | | - } |
| 62 | + System.out.println(" [x] Requesting fib(30)"); |
| 63 | + response = fibonacciRpc.call("30"); |
| 64 | + System.out.println(" [.] Got '" + response + "'"); |
| 65 | + System.out.println(" [x] Requesting fib(-1)"); |
| 66 | + response = fibonacciRpc.call("-1"); |
| 67 | + System.out.println(" [.] Got '" + response + "'"); |
| 68 | + System.out.println(" [x] Requesting fib(a)"); |
| 69 | + response = fibonacciRpc.call("a"); |
| 70 | + System.out.println(" [.] Got '" + response + "'"); |
| 71 | + } |
| 72 | + catch (Exception e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } |
| 75 | + finally { |
| 76 | + if (fibonacciRpc!= null) { |
| 77 | + try { |
| 78 | + fibonacciRpc.close(); |
| 79 | + } |
| 80 | + catch (Exception ignore) {} |
| 81 | + } |
| 82 | + } |
| 83 | + } |
68 | 84 | } |
69 | 85 |
|
0 commit comments