Skip to content

Commit bfc62e1

Browse files
Merge pull request #146 from 2sbsbsb/master
Request to pull with minor improvement
2 parents 12028bd + b705c87 commit bfc62e1

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

java/RPCServer.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] argv) {
2626
Connection connection = null;
2727
try {
2828
connection = factory.newConnection();
29-
Channel channel = connection.createChannel();
29+
final Channel channel = connection.createChannel();
3030

3131
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
3232

@@ -56,19 +56,25 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
5656
}
5757
finally {
5858
channel.basicPublish( "", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
59-
6059
channel.basicAck(envelope.getDeliveryTag(), false);
60+
// RabbitMq consumer worker thread notifies the RPC server owner thread
61+
synchronized(this) {
62+
this.notify();
63+
}
6164
}
6265
}
6366
};
6467

6568
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
66-
67-
//loop to prevent reaching finally block
68-
while(true) {
69-
try {
70-
Thread.sleep(100);
71-
} catch (InterruptedException _ignore) {}
69+
// Wait and be prepared to consume the message from RPC client.
70+
while (true) {
71+
synchronized(consumer) {
72+
try {
73+
consumer.wait();
74+
} catch (InterruptedException e) {
75+
e.printStackTrace();
76+
}
77+
}
7278
}
7379
} catch (IOException | TimeoutException e) {
7480
e.printStackTrace();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy