rabbitmq-consistent-hash-exchange with java-client

2019-08-14 01:47发布

问题:

I would like to use the capabilities of rabbitmq-consistent-hash-exchange from java client or preferably by using the spring abstraction spring-amqp. Unfortunately I failed to found an example that explain the java usage and reference a jar dependency to include, please advice.

回答1:

There is no specific tag for "x-" exchanges. Use CustomExchange for it:

   <bean id="requestHashExchangeTest" class="org.springframework.amqp.core.CustomExchange">
        <constructor-arg name="name" value="test.hash.RequestExchange"/>
        <constructor-arg name="type" value="x-consistent-hash"/>
        <constructor-arg name="durable" value="true"/>
        <constructor-arg name="autoDelete" value="false"/>
        <property name="adminsThatShouldDeclare">
            <list>
                <ref bean="rabbitAdminConnectionFactory" />
            </list>
        </property>
    </bean>

    <bean name="binding"  class="org.springframework.amqp.rabbit.config.BindingFactoryBean">
        <property name="exchange" value="test.random.RequestExchange" />
        <property name="destinationQueue" ref="request.queue" />
        <property name="shouldDeclare" value="true" />
        <property name="adminsThatShouldDeclare">
            <list>
                <ref bean="rabbitAdminConnectionFactory" />
            </list>
        </property>
        <property name="routingKey" value="10" />
    </bean>


回答2:

There's no support for provisioning (declaring) plugin exchanges with Spring AMQP but you can send messages to any exchange type that is configured on the server.

Just add the exchange directly to rabbitmq and then send messages to it like you would to any exchange.

See the reference documentation for how to use Spring AMQP; it has links to samples etc.