Skip to content

Commit cb591b0

Browse files
author
Ann Witbrock
committed
QA fixes: removed inner class
1 parent 782ffda commit cb591b0

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed

java/RPCClient.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,53 @@
77

88
public class RPCClient {
99

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;
1615

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();
2221

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+
}
2726

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();
3230

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);
3634

37-
channel.basicPublish("", requestQueueName, props, message.getBytes());
35+
channel.basicPublish("", requestQueueName, props, message.getBytes());
3836

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;
4542
}
46-
47-
return response;
4843
}
44+
45+
return response;
46+
}
4947

50-
public void close() throws Exception {
51-
channel.close();
52-
connection.close();
53-
}
48+
public void close() throws Exception {
49+
connection.close();
5450
}
5551

5652
public static void main(String[] argv) {
57-
RPCClient.FibonacciRpcClient fibonacciRpc = null;
53+
RPCClient fibonacciRpc = null;
5854
String response = null;
5955
try {
60-
fibonacciRpc = new RPCClient.FibonacciRpcClient();
56+
fibonacciRpc = new RPCClient();
6157

6258
System.out.println(" [x] Requesting fib(30)");
6359
response = fibonacciRpc.call("30");

java/RPCServer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ public class RPCServer {
88

99
private static final String RPC_QUEUE_NAME = "rpc_queue";
1010

11-
private static int fib(int n) throws Exception {
11+
private static int fib(int n) {
1212
if (n > 1) return fib(n-1) + fib(n-2);
1313
else return n;
1414
}
1515

1616
public static void main(String[] argv) {
1717
Connection connection = null;
18+
Channel channel = null;
1819
try {
1920
ConnectionFactory factory = new ConnectionFactory();
2021
factory.setHost("localhost");
2122

2223
connection = factory.newConnection();
23-
Channel channel = connection.createChannel();
24+
channel = connection.createChannel();
2425

2526
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
2627

@@ -41,7 +42,7 @@ public static void main(String[] argv) {
4142
replyProps.setCorrelationId(props.getCorrelationId());
4243

4344
try {
44-
String message = new String(delivery.getBody());
45+
String message = new String(delivery.getBody(),"UTF-8");
4546
int n = Integer.parseInt(message);
4647

4748
System.out.println(" [.] fib(" + message + ")");
@@ -52,7 +53,7 @@ public static void main(String[] argv) {
5253
response = "";
5354
}
5455
finally {
55-
channel.basicPublish( "", props.getReplyTo(), replyProps, response.getBytes());
56+
channel.basicPublish( "", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
5657

5758
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
5859
}

0 commit comments

Comments
 (0)