How to implement a Spring XD sink?

China☆狼群 提交于 2019-12-10 11:05:59

问题


So far I have implemented Spring XD processors, e.g. like this:

@MessageEndpoint
public class MyTransformer 
{

   @Transformer( inputChannel = "input", outputChannel = "output" )
   public String transform( String payload )
   {
      ...
   }
};

However, I am stuck at implementing a custom sink now. The current documentation is not very helpful, since it simply configures something "magically" via XML:

<beans ...>

    <int:channel id="input" />

    <int-redis:store-outbound-channel-adapter
        id="redisListAdapter" collection-type="LIST" channel="input" key="${collection}" auto-startup="false"/>

    <beans:bean id="redisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <beans:property name="hostName" value="${host}" />
        <beans:property name="port" value="${port}" />
    </beans:bean>

</beans>

This will use the redis store-outbound-channel-adapter as a sink. However, the documentation does not tell me how to create a simple, generic sink that simply has one input channel and consumes a message.

So can anyone provide me with a minimal working example?


回答1:


A sink is just like a processor but without an output channel; use a @ServiceActivator to invoke your code (which should have a void return).

@MessageEndpoint
public class MyService  
{

    @ServiceActivator( inputChannel = "input")
    public void handle( String payload )
    {
        ...
    }

};

EDIT

For sources, there are two types:

Polled (messages are pulled from the source):

@InboundChannelAdapter(value = "output", 
        poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "1"))
public String next() {
    return "foo";
}

Message-driven (where the source pushes messages):

@Bean
public MySource source() {
    // return my subclass of MessageProducer that has outputChannel injected
    // and calls sendMessage
    // or use a simple POJO that uses MessagingTemplate.convertAndSend(foo)
}


来源:https://stackoverflow.com/questions/35578688/how-to-implement-a-spring-xd-sink

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