Skip to content

Commit ffad5a9

Browse files
author
Ann
committed
Code tidied wrt Steve's QA bug23639
1 parent 4b100ce commit ffad5a9

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

java/Recv.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1+
import java.io.IOException;
12
import com.rabbitmq.client.ConnectionFactory;
23
import com.rabbitmq.client.Connection;
34
import com.rabbitmq.client.Channel;
45
import com.rabbitmq.client.QueueingConsumer;
56

67
public class Recv {
8+
9+
private final static String QUEUE_NAME = "hello";
10+
711
public static void main(String[] argv)
812
throws java.io.IOException,
913
java.lang.InterruptedException {
10-
Connection conn = null;
14+
1115
ConnectionFactory factory = new ConnectionFactory();
1216
factory.setHost("localhost");
13-
conn = factory.newConnection();
14-
Channel chan = conn.createChannel();
15-
16-
chan.queueDeclare("hello", false, false, false, null);
17+
Connection connection = factory.newConnection();
18+
Channel channel = connection.createChannel();
1719

20+
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
1821
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
19-
QueueingConsumer consumer = new QueueingConsumer(chan);
20-
chan.basicConsume("hello", true, consumer);
22+
23+
QueueingConsumer consumer = new QueueingConsumer(channel);
24+
channel.basicConsume(QUEUE_NAME, true, consumer);
25+
2126
while (true) {
2227
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
23-
System.out.println(" [x] Received " + new String(delivery.getBody()));
28+
String message = new String(delivery.getBody());
29+
System.out.println(" [x] Received '" + message + "'");
2430
}
2531
}
26-
}
32+
}

java/Send.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
import java.io.IOException;
12
import com.rabbitmq.client.ConnectionFactory;
23
import com.rabbitmq.client.Connection;
34
import com.rabbitmq.client.Channel;
45

56
public class Send {
7+
8+
private final static String QUEUE_NAME = "hello";
9+
610
public static void main(String[] argv)
711
throws java.io.IOException {
8-
Connection conn = null;
12+
913
ConnectionFactory factory = new ConnectionFactory();
1014
factory.setHost("localhost");
11-
conn = factory.newConnection();
12-
Channel chan = conn.createChannel();
13-
14-
chan.queueDeclare("hello", false, false, false, null);
15+
Connection connection = factory.newConnection();
16+
Channel channel = connection.createChannel();
1517

16-
chan.basicPublish("", "hello", null, "Hello World!".getBytes());
17-
System.out.println(" [x] Sent 'Hello World!'");
18-
chan.close();
19-
conn.close();
18+
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
19+
String message = "Hello World!";
20+
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
21+
System.out.println(" [x] Sent '" + message + "'");
22+
23+
channel.close();
24+
connection.close();
2025
}
21-
}
26+
}

java/Worker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public static void main(String[] argv)
2727

2828
while (true) {
2929
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
30-
String body = new String(delivery.getBody());
30+
String message = new String(delivery.getBody());
3131

32-
System.out.println(" [x] Received '" + body + "'");
33-
doWork(body);
32+
System.out.println(" [x] Received '" + message + "'");
33+
doWork(message);
3434
System.out.println(" [x] Done");
3535

3636
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

0 commit comments

Comments
 (0)