Skip to content

Commit 0cd19cb

Browse files
committed
Don't handle exceptions in java example
1 parent b6778b3 commit 0cd19cb

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

java/Recv.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,23 @@
44
import com.rabbitmq.client.QueueingConsumer;
55

66
public class Recv {
7-
public static void main(String[] argv) {
8-
try {
9-
Connection conn = null;
10-
try {
11-
ConnectionFactory factory = new ConnectionFactory();
12-
factory.setHost("localhost");
13-
conn = factory.newConnection();
14-
Channel chan = conn.createChannel();
15-
chan.queueDeclare("hello", false, false, false, null);
7+
public static void main(String[] argv)
8+
throws java.io.IOException,
9+
java.lang.InterruptedException {
10+
Connection conn = null;
11+
ConnectionFactory factory = new ConnectionFactory();
12+
factory.setHost("localhost");
13+
conn = factory.newConnection();
14+
Channel chan = conn.createChannel();
1615

17-
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
18-
QueueingConsumer consumer = new QueueingConsumer(chan);
19-
chan.basicConsume("hello", true, consumer);
20-
while (true) {
21-
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
22-
System.out.println(" [x] Received " + new String(delivery.getBody()));
23-
}
24-
}
25-
finally {
26-
if (conn != null) conn.close();
27-
}
28-
}
29-
catch (Exception e) {
30-
System.err.println("Exception while consuming");
31-
e.printStackTrace();
16+
chan.queueDeclare("hello", false, false, false, null);
17+
18+
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
19+
QueueingConsumer consumer = new QueueingConsumer(chan);
20+
chan.basicConsume("hello", true, consumer);
21+
while (true) {
22+
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
23+
System.out.println(" [x] Received " + new String(delivery.getBody()));
3224
}
3325
}
3426
}

java/Send.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@
44
import java.io.IOException;
55

66
public class Send {
7-
public static void main(String[] argv) {
8-
try {
9-
Connection conn = null;
10-
try {
11-
ConnectionFactory factory = new ConnectionFactory();
12-
factory.setHost("localhost");
13-
conn = factory.newConnection();
14-
Channel chan = conn.createChannel();
15-
chan.queueDeclare("hello", false, false, false, null);
16-
chan.basicPublish("", "hello", null, "Hello World!".getBytes());
17-
System.out.println(" [x] Sent 'Hello World!'");
18-
}
19-
finally {
20-
if (conn != null) conn.close();
21-
}
22-
}
23-
catch (Exception e) {
24-
System.err.println("Exception while publishing");
25-
e.printStackTrace();
26-
}
7+
public static void main(String[] argv)
8+
throws java.io.IOException {
9+
Connection conn = null;
10+
ConnectionFactory factory = new ConnectionFactory();
11+
factory.setHost("localhost");
12+
conn = factory.newConnection();
13+
Channel chan = conn.createChannel();
14+
chan.queueDeclare("hello", false, false, false, null);
15+
chan.basicPublish("", "hello", null, "Hello World!".getBytes());
16+
System.out.println(" [x] Sent 'Hello World!'");
17+
conn.close();
2718
}
2819
}

0 commit comments

Comments
 (0)