diff --git a/java/Recv.java b/java/Recv.java index e4ea6b4c..c31cb230 100644 --- a/java/Recv.java +++ b/java/Recv.java @@ -4,31 +4,23 @@ import com.rabbitmq.client.QueueingConsumer; public class Recv { - public static void main(String[] argv) { - try { - Connection conn = null; - try { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - conn = factory.newConnection(); - Channel chan = conn.createChannel(); - chan.queueDeclare("hello", false, false, false, null); + public static void main(String[] argv) + throws java.io.IOException, + java.lang.InterruptedException { + Connection conn = null; + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + conn = factory.newConnection(); + Channel chan = conn.createChannel(); - System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); - QueueingConsumer consumer = new QueueingConsumer(chan); - chan.basicConsume("hello", true, consumer); - while (true) { - QueueingConsumer.Delivery delivery = consumer.nextDelivery(); - System.out.println(" [x] Received " + new String(delivery.getBody())); - } - } - finally { - if (conn != null) conn.close(); - } - } - catch (Exception e) { - System.err.println("Exception while consuming"); - e.printStackTrace(); + chan.queueDeclare("hello", false, false, false, null); + + System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); + QueueingConsumer consumer = new QueueingConsumer(chan); + chan.basicConsume("hello", true, consumer); + while (true) { + QueueingConsumer.Delivery delivery = consumer.nextDelivery(); + System.out.println(" [x] Received " + new String(delivery.getBody())); } } } diff --git a/java/Send.java b/java/Send.java index 112376bb..090ddb77 100644 --- a/java/Send.java +++ b/java/Send.java @@ -4,25 +4,16 @@ import java.io.IOException; public class Send { - public static void main(String[] argv) { - try { - Connection conn = null; - try { - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost("localhost"); - conn = factory.newConnection(); - Channel chan = conn.createChannel(); - chan.queueDeclare("hello", false, false, false, null); - chan.basicPublish("", "hello", null, "Hello World!".getBytes()); - System.out.println(" [x] Sent 'Hello World!'"); - } - finally { - if (conn != null) conn.close(); - } - } - catch (Exception e) { - System.err.println("Exception while publishing"); - e.printStackTrace(); - } + public static void main(String[] argv) + throws java.io.IOException { + Connection conn = null; + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + conn = factory.newConnection(); + Channel chan = conn.createChannel(); + chan.queueDeclare("hello", false, false, false, null); + chan.basicPublish("", "hello", null, "Hello World!".getBytes()); + System.out.println(" [x] Sent 'Hello World!'"); + conn.close(); } }