Mule Persistent ActiveMQ RedeliveryPolicy

时间:2017-04-26 06:41:17

标签: java mule activemq retry-logic

我使用Mule作为ESB解决方案。我有一个队列,从那里我收到消息并试图使http请求服务一直失败。

我在ActiveMQ上配置了RedeliveryPolicy,如下所示:

    <spring:bean id="retryRedeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"
        name="retryRedeliveryPolicy">
        <spring:property name="maximumRedeliveries" value="76" />
        <spring:property name="initialRedeliveryDelay" value="300000" />
        <spring:property name="maximumRedeliveryDelay" value="3600000" />
        <spring:property name="useExponentialBackOff" value="true" />
        <spring:property name="backOffMultiplier" value="2" />
        <spring:property name="queue" value="*" />
    </spring:bean>

5分钟后重试。然后10分钟,20,40,60,60,60 ......大概~3天

问题是,重试逻辑是非持久性。

让我们说,消息正在重试2天。我已经部署了新版本的mule应用程序,或重新启动的服务器......在这种情况下,重试逻辑将从5分钟,10分钟重新开始....因为重试状态由客户端保存在RAM内存中。

热门使RedeliveryPolicy 持续?在2天后重启服务器后,它必须再继续重试1天。

我认为可能有一个解决方案是将timeToLive设置为72小时的消息。但即便如此,重启服务器后。它不会从一开始就每小时重试一次。它将从5分钟开始......

3 个答案:

答案 0 :(得分:1)

您可以使用JMS消息的JMSXDeliveryCount属性来检查已经重试的次数。重试时间间隔逻辑应该依赖于JMSXDeliveryCount变量。

message.getIntProperty("JMSXDeliveryCount ")

http://activemq.apache.org/activemq-message-properties.html

答案 1 :(得分:1)

ActiveMQ有一种方法可以执行持久重新传递,但它并不是使用ConnectionFactory上的RedeliveryPolicy构建的,它用于在失败之前进行短期重新传递。

可以使用ActiveMQ调度程序和延迟消息构建持久重新传递。有点手册,但可行。确保打开schedulerSupport =&#34; true&#34;在尝试此之前在ActiveMQ中。

JMS属性:&#34;交付&#34;跟踪重试次数,如果出现问题,捕获异常策略会多次重新传递消息。实际的延迟由ActiveMQ处理,因此这个流程可以处理几小时,几天等的延迟,并且可以承受ActiveMQ和Mule的重启。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
    xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata"
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">

    <spring:beans>
        <spring:bean name="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
            <spring:property name="maximumRedeliveries" value="0" />
        </spring:bean>

        <spring:bean name="cf"
            class="org.apache.activemq.ActiveMQConnectionFactory">
            <spring:property name="brokerURL" value="tcp://localhost:61616" />
            <spring:property name="redeliveryPolicy" ref="redeliveryPolicy" />
        </spring:bean>
    </spring:beans>

    <jms:activemq-connector name="Active_MQ"
        specification="1.1" brokerURL="tcp://localhost:61616"
        validateConnections="true" doc:name="Active MQ" numberOfConsumers="1"
        maxRedelivery="-1" connectionFactory-ref="cf" />

    <flow name="testFlow">
        <jms:inbound-endpoint queue="IN" connector-ref="Active_MQ"
            doc:name="JMS">
            <jms:transaction action="ALWAYS_BEGIN" />
        </jms:inbound-endpoint>

        <set-property propertyName="delivery"
            value="#[message.inboundProperties['delivery'] or 0]" doc:name="Property" />

        <scripting:component doc:name="Throw Error">
            <scripting:script engine="Groovy"><![CDATA[throw new java.lang.RuntimeException("Error in processing")]]></scripting:script>
        </scripting:component>

        <choice-exception-strategy doc:name="Choice Exception Strategy">
            <catch-exception-strategy doc:name="Catch Exception Strategy"
                when="#[message.outboundProperties['delivery'] &lt; 5]">
                <logger level="ERROR"
                    message="Retry once more count #[message.outboundProperties['delivery']]"
                    doc:name="Logger" />

                <set-property propertyName="AMQ_SCHEDULED_DELAY" value="3000"
                    doc:name="Property" />
                <set-property propertyName="delivery"
                    value="#[message.outboundProperties['delivery'] + 1]" doc:name="Property" />
                <jms:outbound-endpoint queue="IN"
                    connector-ref="Active_MQ" doc:name="JMS">
                    <jms:transaction action="ALWAYS_JOIN" />
                </jms:outbound-endpoint>

            </catch-exception-strategy>

            <rollback-exception-strategy doc:name="Rollback Exception Strategy">
                <logger level="ERROR" message="Giving up retry. Do whatever needed here." doc:name="Logger" />
            </rollback-exception-strategy>
        </choice-exception-strategy>
    </flow>
</mule>

答案 2 :(得分:0)

这无法使RedeliveryPolicy持久化 - 它由连接工厂控制,并且在重新启动服务器时工厂将重置。配置它的方式,它将继续您看到的行为,因为您将useExponentialBackOff设置为true。您可以将此设置为false以使延迟成为常规数量,但这是唯一要做的更改。

我认为你有正确的想法设置消息TTL,至少这将在指定时间后删除消息。 JMSXDeliveryCount非常棒,但如果增加重试之间的延迟,它会在数次尝试后删除,而不是在定义的时间段内删除。