Spring Integration: Get rid of code duplication for setting up beans

萝らか妹 提交于 2019-12-02 21:17:54

问题


For my SFTP client project, I am using spring integration. We have different clients and have to connect to different SFTP servers, but, all of the logic is same, so I have abstracted them out into AbstractSFTPEndPoint. Each client-specific class implements getClientId(), which is used by AbstractSFTPEndPoint to get client-specific details like SFTP credentials.

However, the entire logic is same for all the clients, but I am still having to implement specific classes for each client. This is mainly because we need separate "MessageSource" for each client.

How can I get rid of this duplication?

public class SFTPEndPointForClientAAAA extends AbstractSFTPEndPoint {

    public String getClientId(){
       return "clientAAAA";
    }

    @Bean(name = "channelForClientAAAA")
    public QueueChannel inputFileChannel() {
        return super.inputFileChannel();
    }

    @ServiceActivator(inputChannel = "channelForClientAAAA", poller = @Poller(fixedDelay = "500"))
    public void serviceActivator(Message message) {
        super.serviceActivator(message);
    }

    @Bean(name = "messageSourceForClientAAAA")
    @InboundChannelAdapter(value = "channelForClientAAAA",
            poller = @Poller(fixedDelay = "50", maxMessagesPerPoll = "2"))
    public MessageSource messageSource() {
        return super.messageSource();
    }
}

Basically I have a bunch of SFTP hosts to connect to and apply same logic. I want that to be done automatically without having to implement class for each SFTP host.


回答1:


See the dynamic ftp sample. It uses XML but the same techniques apply to Java configuration. It uses outbound adapters; inbound are a little more complicated because you might need to hook them into a common context. There are links in the readme for how to do that.

However, I recently answered a similar question for multiple IMAP mail adapters using Java configuration and then a follow-up question.

You should be able to use the technique used there.



来源:https://stackoverflow.com/questions/33053052/spring-integration-get-rid-of-code-duplication-for-setting-up-beans

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