Announcement Announcement Module
No announcement yet.
Fanout exchange queue bindings Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fanout exchange queue bindings

    I'm setting up a fanout exchange that publishes events to be picked up by any number of subscribers. I'm doing this with AMQP and RabbitMQ using XML configuration. From what I understand reading through forum posts, in order to have multiple recipients of a fanout message, I need to create a single physical queue for each recipient. Initially I thought that meant I needed to also create a channel for each recipient as well, but it appears that I can have a single channel with multiple queues associated with it bound to a single exchange.

    So here is my (partial) setup:
    HTML Code:
        <!-- Queues -->
        <rabbit:queue id="queue1" name=""/>
        <rabbit:queue id="queue2" name=""/>
        <!-- Exchanges -->
        <rabbit:fanout-exchange name="">
                <rabbit:binding queue="queue1"/>
                <rabbit:binding queue="queue2"/>
        <int:publish-subscribe-channel id="fanoutChannel"/>
        <int-amqp:outbound-channel-adapter channel="fanoutChannel"
    		amqp-template="amqpTemplate" exchange-name=""/>
        <int:gateway id="eventGateway" service-interface=""
        <int-amqp:inbound-channel-adapter channel="fanoutChannel"
                                          connection-factory="connectionFactory" />
        <bean id="eventListener" class=""/>
        <int:service-activator input-channel="fanoutChannel" ref="eventListener" method="handleMessage"/>
    Now when I send a message on the fanout channel, if I have two listeners, the message handler will be called twice. This is all an experiment to make sure the fanout exchange is working to deliver to multiple recipients. So now I need to ask some questions about distribution.

    My fanout exchange is to deliver generic events, but I want to deliver it to multiple handlers, not just the eventListener I've specified above. I want to say that for queue1, the handler is x, and for queue2, the handler is y. Is that possible? Maybe I'm answering my own question here, but perhaps then I need multiple channels, one for each service activator?

    The second question is about the exchange bindings. I want to dynamically provide the names of the bound queues to a particular exchange. Right now I've coded for queue1 and queue2, but I want to be able to include this configuration from a parent configuration and be able to alter which queues are included.

    How I have been doing similar overrides is to put variable names in configuration java bean, as such:

    Parent config:
    HTML Code:
        <bean id="parentConfig" class="" factory-method="getInstance">
            <property name="host" value=""/>
        <import resource="child-config.xml"/>
    Child config:
    HTML Code:
        <bean id="config" class="" factory-method="getInstance"/>
        <rabbit:connection-factory id="connectionFactory" host="#{}"
        		username="#{config.user}" password="#{config.password}"
    The trick here is that I'm using the same instance of Config in both cases but giving the bean a different name. The parent overrides any default settings in the configuration bean. Now I can use the same spring configuration for multiple clients but have different settings for each. I'm not sure if this is the Spring way, and it works, but I'm not sure if this is how it is supposed to be done.

    Now to my question. I'm trying to do the same for the queues bound to the fanout exchange. By default there may be one or even none. The clients that use the system can specify queues and I'd like to provide them as a list of queue names/references that I can provide to the definitions of the queues, fanout-exhange and inbound-channel-adapter.

    Does that make sense? Perhaps I'm trying to be too generic? What is the recommended approach for this?