Create MessageSource for Redis in Spring Integration

守給你的承諾、 提交于 2019-12-12 08:58:53

问题


I want to configure InboundChannelAdapter so that it should pop message from a redis queue and pass it to a ServiceActivator in Java based annotations(only, prefer to avoid XML). I found the code from Spring documentation:

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

But what I do not understand here is, how can I return MessageSource by poping the data from redis queue using redisConnectionFactory?

In other words, how can I do this in java based annotations?

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

回答1:


Let's start from here: https://docs.spring.io/spring-integration/docs/5.0.9.RELEASE/reference/html/overview.html#programming-tips

With XML configuration and Spring Integration Namespace support, the XML Parsers hide how target beans are declared and wired together. For Java & Annotation Configuration, it is important to understand the Framework API for target end-user applications.

Then we open an XSD for that <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>

So, it sounds like a int-redis:queue-inbound-channel-adapter is not a MessageSource. Therefore @InboundChannelAdapter is dead end. I agree that the name of the XML element is wrong then, but it's already too late to rename it.

From here we also have figured out that we need to deal with the RedisQueueMessageDrivenEndpoint. And since it is a message-driven, self-managed we don't need any special annotation for this. There is just enough to declare it as a bean like this:

@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;
}


来源:https://stackoverflow.com/questions/53407160/create-messagesource-for-redis-in-spring-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!