1
votes

Am new to Spring-rabbitMQ.I have the following question..Please correct if am wrong:

I have three instances of application namely.

Instance1 Instance2 Instance3

Note: These instances need not be fixed number.All these instances listen to the same exchange..As the number of instances is not fixed, my requirement is the same message shuld be recieved by all three/(number can ) be variable..how to achieve this using spring-amqp

If declare queue name and bind that to exchange messages will be split across the instances..but i want the rabbitmq messages(messages from exchanges) to all the queues..How can we achieve this ..I heard some thing like anonymous queues...

How to achieve this programatically in spring not through xml declaration and using SPEL to declare the queue id

Step 1: Create connection factory Step 2: Create exchange Step 3: Create a queue Step 4:Create AMQP admin and then declare that queue Step 5: Bind a queue to exchange

Also on stopping the instance the queue should get autodeleted..should theis be given in post-construct?

The present code is as simliar as follows:

CachingConnectionFactory cf = new CachingConnectionFactory("192.168.1.10", 5672);

    RabbitAdmin admin = new RabbitAdmin(cf);

    FanoutExchange testExchange = new FanoutExchange("testExchange", true, false);
    admin.declareExchange(testExchange);

    Queue testQueue = new Queue("testQueue", true, false, true);
    admin.declareQueue(testQueue);

    admin.declareBinding(BindingBuilder.bind(testQueue).to(testExchange));

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(cf);
    container.setRabbitAdmin(admin);

    container.setQueues(testQueue);
    container.setMessageListener(new MessageListenerAdapter() {
        public void handleMessage(String text) {
            System.out.println("Received : " + text);
        }
    });
    container.afterPropertiesSet();

    container.start();
1

1 Answers

2
votes

You go right way with autodeleted queues, but they might not be anonymous, although may be unique per instance.

The main feature here for your is Fanout Exchange. Exactly this Exchange is able to send the same message to all subscribed (bound) queues.

You can find more info in the RabbitMQ Tutorial.