Create MessageSource for Redis in Spring Integration

sureshhewabi :

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"/>
Artem Bilan :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107218&siteId=1
Recommended