Permalink
Browse files

Exceptions and closing in finally

  • Loading branch information...
1 parent 53f47da commit 228b850a7cf5cf05bce3a758218e23f21789a6d2 Ann Witbrock committed Apr 28, 2011
Showing with 65 additions and 37 deletions.
  1. +26 −13 java/EmitLogTopic.java
  2. +39 −24 java/ReceiveLogsTopic.java
View
39 java/EmitLogTopic.java
@@ -6,23 +6,36 @@
private static final String EXCHANGE_NAME = "topic_logs";
- public static void main(String[] argv) throws Exception {
-
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
+ public static void main(String[] argv) {
+ Connection connection = null;
+ Channel channel = null;
+ try {
+ ConnectionFactory factory = new ConnectionFactory();
+ factory.setHost("localhost");
+
+ connection = factory.newConnection();
+ channel = connection.createChannel();
- channel.exchangeDeclare(EXCHANGE_NAME, "topic");
+ channel.exchangeDeclare(EXCHANGE_NAME, "topic");
- String routingKey = getRouting(argv);
- String message = getMessage(argv);
+ String routingKey = getRouting(argv);
+ String message = getMessage(argv);
- channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
- System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
+ channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
+ System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
- channel.close();
- connection.close();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ if (connection != null) {
+ try {
+ connection.close();
+ }
+ catch (Exception ignore) {}
+ }
+ }
}
private static String getRouting(String[] strings){
View
63 java/ReceiveLogsTopic.java
@@ -7,36 +7,51 @@
private static final String EXCHANGE_NAME = "topic_logs";
- public static void main(String[] argv) throws Exception {
-
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
-
- channel.exchangeDeclare(EXCHANGE_NAME, "topic");
- String queueName = channel.queueDeclare().getQueue();
+ public static void main(String[] argv) {
+ Connection connection = null;
+ Channel channel = null;
+ try {
+ ConnectionFactory factory = new ConnectionFactory();
+ factory.setHost("localhost");
+
+ connection = factory.newConnection();
+ channel = connection.createChannel();
+
+ channel.exchangeDeclare(EXCHANGE_NAME, "topic");
+ String queueName = channel.queueDeclare().getQueue();
- if (argv.length < 1){
- System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
- System.exit(1);
- }
+ if (argv.length < 1){
+ System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
+ System.exit(1);
+ }
- for(String bindingKey : argv){
- channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
- }
+ for(String bindingKey : argv){
+ channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
+ }
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
+ System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- QueueingConsumer consumer = new QueueingConsumer(channel);
- channel.basicConsume(queueName, true, consumer);
+ QueueingConsumer consumer = new QueueingConsumer(channel);
+ channel.basicConsume(queueName, true, consumer);
- while (true) {
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- String message = new String(delivery.getBody());
- String routingKey = delivery.getEnvelope().getRoutingKey();
+ while (true) {
+ QueueingConsumer.Delivery delivery = consumer.nextDelivery();
+ String message = new String(delivery.getBody());
+ String routingKey = delivery.getEnvelope().getRoutingKey();
- System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
+ System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ if (connection != null) {
+ try {
+ connection.close();
+ }
+ catch (Exception ignore) {}
+ }
}
}
}

0 comments on commit 228b850

Please sign in to comment.