【问题标题】:Create MessageSource for Redis in Spring Integration在 Spring 集成中为 Redis 创建 MessageSource
【发布时间】:2018-11-21 07:30:03
【问题描述】:

我想配置 InboundChannelAdapter 以便它应该从 redis 队列中弹出消息并将其传递给基于 Java 的注释中的 ServiceActivator(仅限,更喜欢避免使用 XML)。我从 Spring 文档中找到了代码:

@Bean("someAdapter.source")
@EndpointId("someAdapter")
@InboundChannelAdapter(channel = "channel3", poller = @Poller(fixedDelay = "5000"))
public MessageSource<?> source() {
    return () -> {
        ...
    };
}

但我在这里不明白的是,如何通过使用redisConnectionFactory从redis队列中弹出数据来返回MessageSource?

换句话说,如何在基于 java 的注解中做到这一点?

  <int-redis:queue-inbound-channel-adapter id="postPublicationInboundAdapter"
                                             connection-factory="redisConnectionFactory"
                                             channel="postPublicationChannel"
                                             error-channel="postPublicationLoggingChannel"
                                             receive-timeout="5000"
                                             queue="archive.post.publication.queue"
                                             serializer="postPublicationJsonRedisSerializer"/>

【问题讨论】:

    标签: java redis spring-integration


    【解决方案1】:

    让我们从这里开始:https://docs.spring.io/spring-integration/docs/5.0.9.RELEASE/reference/html/overview.html#programming-tips

    借助 XML 配置和 Spring 集成命名空间支持,XML 解析器隐藏了目标 bean 的声明和连接方式。对于 Java 和注释配置,了解目标最终用户应用程序的框架 API 非常重要。

    然后我们为那个&lt;int-redis:queue-inbound-channel-adapter&gt;打开一个XSD:

     <xsd:element name="queue-inbound-channel-adapter">
        <xsd:annotation>
            <xsd:documentation>
                Defines a Message Producing Endpoint for the
                'org.springframework.integration.redis.inbound.RedisQueueMessageDrivenEndpoint' for listening a Redis
                queue.
            </xsd:documentation>
        </xsd:annotation>
    

    所以,听起来int-redis:queue-inbound-channel-adapter 不是MessageSource。因此@InboundChannelAdapter 是死胡同。我同意XML元素的名称是错误的,但是重命名已经太迟了。

    从这里我们也知道我们需要处理RedisQueueMessageDrivenEndpoint。因为它是一个消息驱动的,自我管理,我们不需要任何特殊的注释。将其声明为这样的 bean 就足够了:

    @Bean
    RedisQueueMessageDrivenEndpoint redisQueueMessageDrivenEndpoint(RedisConnectionFactory redisConnectionFactory, RedisSerializer<?> serializer) {
        RedisQueueMessageDrivenEndpoint endpoint =
                    new RedisQueueMessageDrivenEndpoint("archive.post.publication.queue", redisConnectionFactory);
        endpoint.setOutputChannelName("postPublicationChannel");
        endpoint.setErrorChannelName("postPublicationLoggingChannel");
        endpoint.setReceiveTimeout(5000);
        endpoint.setSerializer(serializer);
        return endpoint;
    }
    

    【讨论】:

    • 解释得很好。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    相关资源
    最近更新 更多