|
| 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