forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recv.java
32 lines (31 loc) · 1010 Bytes
/
Recv.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
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);
QueueingConsumer consumer = new QueueingConsumer(chan);
chan.basicConsume("hello", true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
System.out.println(new String(delivery.getBody()));
}
}
finally {
if (conn != null) conn.close();
}
}
catch (Exception e) {
System.err.println("Exception while consuming");
e.printStackTrace();
}
}
}