Skip to content

Commit 228b850

Browse files
author
Ann Witbrock
committed
Exceptions and closing in finally
1 parent 53f47da commit 228b850

File tree

2 files changed

+65
-37
lines changed

2 files changed

+65
-37
lines changed

java/EmitLogTopic.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,36 @@ public class EmitLogTopic {
66

77
private static final String EXCHANGE_NAME = "topic_logs";
88

9-
public static void main(String[] argv) throws Exception {
10-
11-
ConnectionFactory factory = new ConnectionFactory();
12-
factory.setHost("localhost");
13-
Connection connection = factory.newConnection();
14-
Channel channel = connection.createChannel();
9+
public static void main(String[] argv) {
10+
Connection connection = null;
11+
Channel channel = null;
12+
try {
13+
ConnectionFactory factory = new ConnectionFactory();
14+
factory.setHost("localhost");
15+
16+
connection = factory.newConnection();
17+
channel = connection.createChannel();
1518

16-
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
19+
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
1720

18-
String routingKey = getRouting(argv);
19-
String message = getMessage(argv);
21+
String routingKey = getRouting(argv);
22+
String message = getMessage(argv);
2023

21-
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
22-
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
24+
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
25+
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
2326

24-
channel.close();
25-
connection.close();
27+
}
28+
catch (Exception e) {
29+
e.printStackTrace();
30+
}
31+
finally {
32+
if (connection != null) {
33+
try {
34+
connection.close();
35+
}
36+
catch (Exception ignore) {}
37+
}
38+
}
2639
}
2740

2841
private static String getRouting(String[] strings){

java/ReceiveLogsTopic.java

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,51 @@ public class ReceiveLogsTopic {
77

88
private static final String EXCHANGE_NAME = "topic_logs";
99

10-
public static void main(String[] argv) throws Exception {
11-
12-
ConnectionFactory factory = new ConnectionFactory();
13-
factory.setHost("localhost");
14-
Connection connection = factory.newConnection();
15-
Channel channel = connection.createChannel();
16-
17-
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
18-
String queueName = channel.queueDeclare().getQueue();
10+
public static void main(String[] argv) {
11+
Connection connection = null;
12+
Channel channel = null;
13+
try {
14+
ConnectionFactory factory = new ConnectionFactory();
15+
factory.setHost("localhost");
16+
17+
connection = factory.newConnection();
18+
channel = connection.createChannel();
19+
20+
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
21+
String queueName = channel.queueDeclare().getQueue();
1922

20-
if (argv.length < 1){
21-
System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
22-
System.exit(1);
23-
}
23+
if (argv.length < 1){
24+
System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
25+
System.exit(1);
26+
}
2427

25-
for(String bindingKey : argv){
26-
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
27-
}
28+
for(String bindingKey : argv){
29+
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
30+
}
2831

29-
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
32+
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
3033

31-
QueueingConsumer consumer = new QueueingConsumer(channel);
32-
channel.basicConsume(queueName, true, consumer);
34+
QueueingConsumer consumer = new QueueingConsumer(channel);
35+
channel.basicConsume(queueName, true, consumer);
3336

34-
while (true) {
35-
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
36-
String message = new String(delivery.getBody());
37-
String routingKey = delivery.getEnvelope().getRoutingKey();
37+
while (true) {
38+
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
39+
String message = new String(delivery.getBody());
40+
String routingKey = delivery.getEnvelope().getRoutingKey();
3841

39-
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
42+
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
43+
}
44+
}
45+
catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
finally {
49+
if (connection != null) {
50+
try {
51+
connection.close();
52+
}
53+
catch (Exception ignore) {}
54+
}
4055
}
4156
}
4257
}

0 commit comments

Comments
 (0)