【问题标题】:Spring Integration Redis RPOPLPUSHSpring 集成 Redis RPOPLPUSH
【发布时间】:2019-08-27 09:26:07
【问题描述】:

我是 Spring Integration 和 Redis 的新手,所以如果我犯了一个幼稚的错误,我深表歉意。

我的要求如下-

  1. 需要实现一个消息队列。用于根据某些事件向用户分配资金。
  2. 队列不应该是易变的并且保证原子性。如果我重新启动服务器或它崩溃了,它不应该丢失事件消息。这也包括当前正在处理的消息。
  3. 队列应传递一次且仅一次授予的消息。这将是一个多线程(工作者)和多服务器环境。

到目前为止,我的进展是 - 在我的 spring 项目中配置了 Spring Integration 和 Spring Integration Redis。我的 Spring 集成配置如下 -

    <int-redis:queue-outbound-channel-adapter
           id="event-outbound-channel-adapter"
           channel="eventChannelJson"
           serializer="serializer"
           auto-startup="true" connection-factory="redisConnectionFactory"
           queue="my-event-queue" />

    <int:gateway id="eventChannelGateway"
                service-interface="com.test.RedisChannelGateway"
                error-channel="errorChannel" default-request-channel="eventChannel">
       <int:default-header name="topic" value="queue"/>
    </int:gateway>

    <int:channel id="eventChannelJson"/>
    <int:channel id="eventChannel">
       <int:queue/>
    </int:channel>


    <bean id="serializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    <int:object-to-json-transformer input-channel="eventChannel"
                                   output-channel="eventChannelJson"/>


    <int-redis:queue-inbound-channel-adapter id="event-inbound-channel-adapter"
                                            channel="eventChannelJson" queue="my-event-queue"
                                            serializer="serializer" auto-startup="true"
                                            connection-factory="redisConnectionFactory"/>

    <bean id="serializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    <int:json-to-object-transformer input-channel="eventChannelJson"
                                   output-channel="eventChannel"
                                   type="com.test.PostPublishedEvent"/>

    <int:service-activator input-channel="eventChannel" ref="RedisEventProcessingService"
                          method="process">
       <int:poller fixed-delay="10" time-unit="SECONDS" max-messages-per-poll="500"/>
    </int:service-activator> 

我阅读了一篇关于类似主题的文章,他们为此目的使用了 redis RPOPLPUSH。但我也无法弄清楚如何在 Spring Integration 中做到这一点。 文章链接是-https://redis.io/commands/RPOPLPUSH

请建议我重新考虑这一点。我真的会感谢你的帮助。

【问题讨论】:

    标签: spring redis spring-integration spring-data-redis


    【解决方案1】:

    Spring 集成没有使用该操作的组件。

    要使用它,您应该将RedisTemplate 包装在&lt;int:service-activator/&gt; 中并调用其中一个rightPopAndLeftPush() 方法。

    【讨论】:

    • 根据可靠队列的描述,我们应该考虑利用这样的特性来重试/重新发送以防出错。不过,您可以自己执行此操作,仍然使用 &lt;int-redis:queue-inbound-channel-adapter&gt;,但在返回队列的 error-channelrightPush() 消息上添加额外的逻辑。
    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 2017-09-22
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多