rabbitmqspring-amqpspring-rabbit

How do I implement x-consistent-hash-exchange using Spring XML


All other exchange types are declared in http://www.springframework.org/schema/rabbit schema, but couldn't find x-consistent-hash-exchange in it. Where is it defined?

I tried implementing consistent hash exchange in XML, however, this is the exception I got after trying to use if from rabbit schema:

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 64 in XML document from URL [file:/conf/spring/mq-admin.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 64; columnNumber: 84; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'rabbit:x-consistent-hash-exchange'


Solution

  • That's true, there is no custom XML tag for the ConsistentHashExchange.

    All those headers in the end declare some bean definitions.

    For this one it is simply enough to do like this:

    <beans:bean id="myConsistentHashExchange" class="org.springframework.amqp.core.ConsistentHashExchange">
        <beans:constructor-arg name="name" value="myConsistentHashExchange"/>
        <beans:property name="hashHeader" value="myHashHeader"/>
    </beans:bean>
    

    We don't have plans to extend XML support.

    UPDATE

    For versions before 3.2, we have to use a CustomExchange for bean definition:

    <beans:bean id="myConsistentHashExchange" class="org.springframework.amqp.core.CustomExchange">
        <beans:constructor-arg name="name" value="myConsistentHashExchange"/>
        <beans:constructor-arg name="type" value="x-consistent-hash"/>
        <beans:constructor-arg name="durable" value="true"/>
        <beans:constructor-arg name="autoDelete" value="true"/>
        <beans:constructor-arg name="arguments">
            <beans:map>
                <beans:entry key="hash-header" value="myHashHeader"/>
            </beans:map>
        </beans:constructor-arg>
    </beans:bean>
    

    And the result is like this:

    enter image description here

    NOTE: The rabbitmq_consistent_hash_exchange plugin has to be enabled on the broker: https://github.com/rabbitmq/rabbitmq-server/tree/main/deps/rabbitmq_consistent_hash_exchange