Skip to content

Commit d9fde2f

Browse files
author
Ann Witbrock
committed
new ReceiveLogsDirect.java
1 parent bb3d9e6 commit d9fde2f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

java/ReceiveLogsDirect.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.IOException;
2+
import com.rabbitmq.client.ConnectionFactory;
3+
import com.rabbitmq.client.Connection;
4+
import com.rabbitmq.client.Channel;
5+
import com.rabbitmq.client.QueueingConsumer;
6+
7+
public class ReceiveLogsDirect {
8+
9+
private static final String EXCHANGE_NAME = "direct_logs";
10+
11+
public static void main(String[] argv)
12+
throws java.io.IOException,
13+
java.lang.InterruptedException {
14+
15+
ConnectionFactory factory = new ConnectionFactory();
16+
factory.setHost("localhost");
17+
Connection connection = factory.newConnection();
18+
Channel channel = connection.createChannel();
19+
20+
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
21+
String queueName = channel.queueDeclare().getQueue();
22+
23+
if (argv.length < 1){
24+
System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
25+
System.exit(1);
26+
}
27+
28+
for(String severity : argv){
29+
channel.queueBind(queueName, EXCHANGE_NAME, severity);
30+
}
31+
32+
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
33+
34+
QueueingConsumer consumer = new QueueingConsumer(channel);
35+
channel.basicConsume(queueName, true, consumer);
36+
37+
while (true) {
38+
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
39+
String message = new String(delivery.getBody());
40+
String routingKey = delivery.getEnvelope().getRoutingKey();
41+
42+
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)