1

How to use the RedeliveryPolicy in case of failed messages in outbound->inbound->httpoutboundgateway(messages failing here)->external service

the failed messages are not getting retried based on the RedeliveryPolicy mainly because the message is already dequeued before the exception occurs.

<int:channel id="jmsOutChannel" />
    <jms:outbound-channel-adapter id="outboundJMSAdaptor" jms-template="jmsTemplate"
            channel="jmsOutChannel"
            destination-name="#{somebean.queueName}"/>

    <int:channel id="jmsInChannel" />
    <jms:message-driven-channel-adapter
            channel="jmsInChannel" destination-name="#{somebean.queueName}"
            connection-factory="jmsConnectionFactory" message-converter="jmsMessageConverter"/>

    <int:header-enricher input-channel="jmsInChannel" output-channel="outbound_gateway_channel">
        <int:header name="addressId" expression="payload.getId()"/>
        <int:header name="Accept-Language" value="en_GB"/>
        <int:header name="X-Source-CountryCode" value="GB"/>
        <int:header name="X-Source-Operator" value="Enterprise"/>
        <int:header name="X-Source-Division" value="CustomerManagement"/>
        <int:header name="X-Source-System" value="${sapwebservices.http.header.source.system}"/>
        <int:header name="X-Source-Timestamp" expression="new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new java.util.Date())"/>
        <int:header name="Accept" value="application/json"/>
        <int:header name="Content-Type" value="application/json;charset=UTF-8"/>
    </int:header-enricher>
    <bean id="httpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
        <property name="connectTimeout" value="${sapwebservices.http.rest.timeout}"/>
        <property name="readTimeout" value="${sapwebservices.http.rest.timeout}"/>
        <property name="httpClient" ref="httpClient"/>
    </bean>
    <int:object-to-json-transformer input-channel="outbound_gateway_channel"
            output-channel="outbound_gateway_with_json"
            object-mapper="nonNullObjectMapper"/>
    <http:outbound-gateway mapped-request-headers="Accept*, Content-Type, X-*, HTTP_REQUEST_HEADERS"
            request-channel="outbound_gateway_with_json"
            reply-channel="print_payload"
            url="${sapwebservices.ws.uri.updatecustomershippingaddress}"
            http-method="PUT"
            expected-response-type="java.lang.String"
            charset="UTF-8"
            request-factory="httpRequestFactory">
        <http:uri-variable name="id" expression="headers['addressId']"/>
    </http:outbound-gateway>

And for the redelivery policy I have,

  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="messageConverter" ref="jmsMessageConverter" />
        <property name="connectionFactory">
            <bean class="org.springframework.jms.connection.SingleConnectionFactory">
                <property name="targetConnectionFactory">
                    <ref local="jmsConnectionFactory" />
                </property>
            </bean>
        </property>
    </bean>

    <bean id="jmsConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
        <property name="userName" value="admin"/>
        <property name="password" value="admin"/>
        <property name="redeliveryPolicy" ref="redeliveryPolicy"/>
        <property name="nonBlockingRedelivery" value="true"/>
    </bean>

    <bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
        <property name="maximumRedeliveries" value="${sapwebservice.redeliveryPolicy.maximumRedeliveries}"/>
        <property name="initialRedeliveryDelay" value="${sapwebservice.redeliveryPolicy.initialRedeliveryDelay}"/>
        <property name="backOffMultiplier" value="${sapwebservice.redeliveryPolicy.backOffMultiplier}"/>
        <property name="useExponentialBackOff" value="${sapwebservice.redeliveryPolicy.useExponentialBackOff}"/>
        <property name="redeliveryDelay" value="${sapwebservice.redeliveryPolicy.redeliveryDelay}"/>
    </bean>

    <bean id="demoQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="demo.queue"/>
    </bean>

    <bean id="jmsMessageConverter" class="sap.converter.JMSMessageConverter"/>

    <amq:broker useJmx="true" persistent="false">
        <amq:destinationPolicy>
            <amq:policyMap>
                <amq:defaultEntry>
                    <amq:policyEntry queue=">">
                        <amq:deadLetterStrategy>
                            <amq:individualDeadLetterStrategy queuePrefix="dlq." useQueueForQueueMessages="true"/>
                        </amq:deadLetterStrategy>
                    </amq:policyEntry>
                </amq:defaultEntry>
            </amq:policyMap>
        </amq:destinationPolicy>
        <amq:transportConnectors>
            <amq:transportConnector uri="tcp://localhost:0" />
        </amq:transportConnectors>
    </amq:broker>

Expected result is to retry based in the RedeliveryPolicy and finally end up in dlq if all the retrials are failed.

rowen
  • 37
  • 5
  • What version of Spring Integration are you using? The container thread runs in a transaction by default ([since version 4.2](https://jira.spring.io/browse/INT-3784) so the transaction (including the dequeue) will roll back when an exception is thrown. – Gary Russell Jul 24 '19 at 18:13
  • the application is running on spring-integration v2.1 – rowen Jul 25 '19 at 06:09
  • Really? Why so old? The most recent 2.1.x release is from 2013. If you are really using such an old version, set `acknowledge="transacted"` on the message-driven adapter. But I really recommend you get more up-to-date. – Gary Russell Jul 25 '19 at 13:20
  • it comes with a product and since we are using older version of that product in a support project so can't change spring version.Anyways, thanks, the solution provided worked. – rowen Jul 28 '19 at 13:55

1 Answers1

0

Set acknowledge="transacted" on the message-driven channel adapter.

Spring Integration versions 4.2 and later (the current version is 5.1.7) set it to that by default; for earlier versions you have to set it in your configuration so the flow runs in a transaction and the dequeue is rolled back after an exception is thrown.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179