@@ -14,53 +14,36 @@ public static void main(String[] argv) throws Exception {
14
14
ConnectionFactory factory = new ConnectionFactory ();
15
15
factory .setHost ("localhost" );
16
16
17
- try (Connection connection = factory .newConnection ();
18
- Channel channel = connection .createChannel ()) {
19
- channel .queueDeclare (RPC_QUEUE_NAME , false , false , false , null );
20
- channel .queuePurge (RPC_QUEUE_NAME );
21
-
22
- channel .basicQos (1 );
23
-
24
- System .out .println (" [x] Awaiting RPC requests" );
25
-
26
- Object monitor = new Object ();
27
- DeliverCallback deliverCallback = (consumerTag , delivery ) -> {
28
- AMQP .BasicProperties replyProps = new AMQP .BasicProperties
29
- .Builder ()
30
- .correlationId (delivery .getProperties ().getCorrelationId ())
31
- .build ();
32
-
33
- String response = "" ;
34
-
35
- try {
36
- String message = new String (delivery .getBody (), "UTF-8" );
37
- int n = Integer .parseInt (message );
38
-
39
- System .out .println (" [.] fib(" + message + ")" );
40
- response += fib (n );
41
- } catch (RuntimeException e ) {
42
- System .out .println (" [.] " + e .toString ());
43
- } finally {
44
- channel .basicPublish ("" , delivery .getProperties ().getReplyTo (), replyProps , response .getBytes ("UTF-8" ));
45
- channel .basicAck (delivery .getEnvelope ().getDeliveryTag (), false );
46
- // RabbitMq consumer worker thread notifies the RPC server owner thread
47
- synchronized (monitor ) {
48
- monitor .notify ();
49
- }
50
- }
51
- };
52
-
53
- channel .basicConsume (RPC_QUEUE_NAME , false , deliverCallback , (consumerTag -> { }));
54
- // Wait and be prepared to consume the message from RPC client.
55
- while (true ) {
56
- synchronized (monitor ) {
57
- try {
58
- monitor .wait ();
59
- } catch (InterruptedException e ) {
60
- e .printStackTrace ();
61
- }
62
- }
17
+ Connection connection = factory .newConnection ();
18
+ Channel channel = connection .createChannel ();
19
+ channel .queueDeclare (RPC_QUEUE_NAME , false , false , false , null );
20
+ channel .queuePurge (RPC_QUEUE_NAME );
21
+
22
+ channel .basicQos (1 );
23
+
24
+ System .out .println (" [x] Awaiting RPC requests" );
25
+
26
+ DeliverCallback deliverCallback = (consumerTag , delivery ) -> {
27
+ AMQP .BasicProperties replyProps = new AMQP .BasicProperties
28
+ .Builder ()
29
+ .correlationId (delivery .getProperties ().getCorrelationId ())
30
+ .build ();
31
+
32
+ String response = "" ;
33
+ try {
34
+ String message = new String (delivery .getBody (), "UTF-8" );
35
+ int n = Integer .parseInt (message );
36
+
37
+ System .out .println (" [.] fib(" + message + ")" );
38
+ response += fib (n );
39
+ } catch (RuntimeException e ) {
40
+ System .out .println (" [.] " + e );
41
+ } finally {
42
+ channel .basicPublish ("" , delivery .getProperties ().getReplyTo (), replyProps , response .getBytes ("UTF-8" ));
43
+ channel .basicAck (delivery .getEnvelope ().getDeliveryTag (), false );
63
44
}
64
- }
45
+ };
46
+
47
+ channel .basicConsume (RPC_QUEUE_NAME , false , deliverCallback , (consumerTag -> {}));
65
48
}
66
49
}
0 commit comments