简体   繁体   English

使用spring在Ibm Websphere MQ中实现重试逻辑

[英]Implementing retry logic in Ibm Websphere MQ with spring

I am working on below configuration for messaging using Spring and Webphere MQ. 我正在使用Spring和Webphere MQ处理以下配置以进行消息传递。

I have a requirement of implementing retrying logic for a scenario, where I receive a message from the queue and put the message data on to a Elastic search server (search server is non transactional), if the search server is down I have to rollback the message on to the queue again and process the message after some interval of time(for example: 30 seconds) . 我需要为场景实现重试逻辑,我从队列接收消息并将消息数据放到Elastic搜索服务器上(搜索服务器是非事务性的),如果搜索服务器关闭,我必须回滚再次向队列发送消息,并在一段时间间隔后处理该消息(例如:30秒)。 this retry has to be done for 5 times. 这个重试必须完成5次。 after 5 times the message has to be put on a Dead letter queue .we are using Tomcat as the server. 5次之后,消息必须放在死信队列中。我们使用Tomcat作为服务器。

we are using spring integration jms:message-driven-channel-adapter for message receiving 我们使用spring integration jms:message-driven-channel-adapter进行消息接收

How can I implement this behavior with spring and Websphere MQ? 如何使用spring和Websphere MQ实现此行为?

I have crawled across many sites, and I could find support for Active MQ but not for IBM MQ. 我已遍历了许多站点,我可以找到对Active MQ的支持,但不支持IBM MQ。

<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
        <property name="hostName">
            <value>${queue_hostname}</value>
        </property>
        <property name="port">
            <value>${queue_port}</value>
        </property>
        <property name="queueManager">
            <value>${queue_manager}</value>
        </property>
        <property name="transportType">
            <value>1</value>
        </property>
    </bean>

    <!-- JMS Queue Connection Factory -->
    <bean id="jmsQueueConnectionFactory"
        class="org.springframework.jms.connection.SingleConnectionFactory102">
        <property name="targetConnectionFactory">
            <ref bean="mqConnectionFactory" />
        </property>
        <property name="pubSubDomain">
            <value>false</value>
        </property>
    </bean>

    <!-- JMS Destination Resolver -->
    <bean id="jmsDestinationResolver"
        class="org.springframework.jms.support.destination.DynamicDestinationResolver">
    </bean>

    <!-- JMS Queue Template -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate102">
        <property name="connectionFactory">
            <ref bean="jmsQueueConnectionFactory" />
        </property>
        <property name="destinationResolver">
            <ref bean="jmsDestinationResolver" />
        </property>
        <property name="pubSubDomain">
            <value>false</value>
        </property>
        <property name="receiveTimeout">
            <value>20000</value>
        </property>
    </bean>

There is nothing in the JMS spec about delayed redeliveries. JMS规范中没有关于延迟重新传递的内容。 Some brokers have a custom mechanism/policy to implement it; 一些经纪人有自定义机制/政策来实施它; you'll have to look at the broker documentation. 你必须看看经纪人文件。

As has been said before, you can use the delivery count header to give up after some number of retries. 如前所述,您可以使用传递计数标题在经过一些重试后放弃。

EDIT 编辑

In response to your comment below... 回应下面的评论......

Only if you are using a version of MQ that supports JMS 2.0 - it looks like you are using a very old version requiring JmsTemplate102 . 仅当您使用的是支持JMS 2.0的MQ版本时 - 看起来您使用的是需要JmsTemplate102旧版本。 1.0.2 is ancient; 1.0.2是古老的; 1.1 has been out for years; 1.1已经出了多年; Spring has supported JMS 2.0 for a nearly 3 years. Spring已经支持JMS 2.0近3年了。 If you have a JMS 2.0 broker (and client library), set the deliveryDelay on a JmsTemplate bean . 如果您有JMS 2.0代理(和客户端库),请在JmsTemplate bean上设置deliveryDelay Then, configure the outbound channel adapter to use that template via the jms-template property. 然后,配置出站通道适配器以通过jms-template属性使用该模板。

In order to get visibility to the redelivery count, either pass the whole message into your service or, with a POJO method, configure it to get that header... 为了获得重新传递计数的可见性,要么将整个消息传递到您的服务中,要么使用POJO方法将其配置为获取该标头...

public MyReply process(@Payload MyObject foo,
              @Header("JMSXRedeliveryCount") int redeliveryCOunt) {
    ...
}

Again, this is a JMS 2.0 feature (the header) although some brokers provide it in 1.1. 同样,这是一个JMS 2.0功能(标题),尽管一些代理在1.1中提供它。

IBM MQ doesn't have the delay redelivery but one option would be to consider using the JMS2.0 concept of Delayed Delivery . IBM MQ没有延迟重新传递,但一种选择是考虑使用延迟传递的JMS2.0概念。 This doesn't set the redelivered flag so won't engage any backout logic but it would implemented the timed behaviour. 这不会设置redelivered标志,因此不会使用任何退出逻辑,但它会实现定时行为。

For example when the message couldn't be processed it could be requeued by the application with a delay. 例如,当无法处理消息时,应用程序可能会延迟重新排队。 This won't be then seen again for say 5 minutes. 5分钟后再也不会再看到这一点。 The application would probably have to mark the message with a counter. 应用程序可能必须使用计数器标记消息。 Both could be done under transacted session to achieve 两者都可以在交易会话下完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM