小编典典

在Spring Integration中为Redis创建MessageSource

redis

我想配置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"/>

阅读 812

收藏
2020-06-20

共1个答案

小编典典

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

借助XML配置和Spring
Integration命名空间的支持,XML解析器隐藏了如何声明目标bean并将它们连接在一起。对于Java和注释配置,了解用于目标最终用户应用程序的Framework
API非常重要。

然后我们为此打开一个XSD <int-redis:queue-inbound-channel-adapter>

 <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>

因此,听起来a 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;
}
2020-06-20