File: QueueEvents.java

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (44 lines) | stat: -rw-r--r-- 1,878 bytes parent folder | download | duplicates (6)
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
33
34
35
36
37
38
39
40
41
42
43
44

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class QueueEvents {
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        Connection c = f.newConnection();
        Channel ch = c.createChannel();
        String q = ch.queueDeclare().getQueue();
        ch.queueBind(q, "amq.rabbitmq.event", "queue.*");

        Consumer consumer = new DefaultConsumer(ch) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String event = envelope.getRoutingKey();
                Map<String, Object> headers = properties.getHeaders();
                String name = headers.get("name").toString();
                String vhost = headers.get("vhost").toString();

                if (event.equals("queue.created")) {
                    boolean durable = (Boolean) headers.get("durable");
                    String durableString = durable ? " (durable)" : " (transient)";
                    System.out.println("Created: " + name + " in " + vhost + durableString);
                }
                else /* queue.deleted is the only other possibility */ {
                    System.out.println("Deleted: " + name + " in " + vhost);
                }
            }
        };
        ch.basicConsume(q, true, consumer);
        System.out.println("QUEUE EVENTS");
        System.out.println("============\n");
    }
}