Spring整合多渠道

问题描述

我想使用xml配置创建多个入站和出站通道,但是我想保持动态。例如,我有以下xml配置-

<int:channel id='test1.reply.channel'>
    <int:queue capacity='10'/>
</int:channel>
<int:channel id='test1.request.channel'/>
<int-http:outbound-gateway id='outbound.gateway1'
                           request-channel='test1.request.channel' url='http://1.2.3.4:5555/api'
                           http-method='POST' expected-response-type='java.lang.String'
                           charset='UTF-8' reply-timeout='5000' reply-channel='test1.reply.channel'>
</int-http:outbound-gateway>

<int:channel id='test2.reply.channel'>
    <int:queue capacity='10'/>
</int:channel>
<int:channel id='test2.request.channel'/>
<int-http:outbound-gateway id='outbound.gateway2'
                           request-channel='test2.request.channel' url='http://5.6.7.8:5555/api'
                           http-method='POST' expected-response-type='java.lang.String'
                           charset='UTF-8' reply-timeout='5000' reply-channel='test2.reply.channel'>
</int-http:outbound-gateway>

我的java类看起来像-

@Component
@ImportResource("classpath:http-outbound.xml")
public class RestIntegrationHandler {

    @Autowired
    @Qualifier("test1.reply.channel")
    private PollableChannel test1ResponseChannel;

    @Autowired
    @Qualifier("test1.request.channel")
    private MessageChannel test1RequestChannel;

    @Autowired
    @Qualifier("test2.reply.channel")
    private PollableChannel test2ResponseChannel;

    @Autowired
    @Qualifier("test2.request.channel")
    private MessageChannel test2RequestChannel;

    public String executeCall(String externalSystem,String payload,Map<String,Object> headers) {
        if(externalSystem.equalsIgnoreCase("1")) {
            Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
            test1RequestChannel.send(message);
            Message<?> receivedMsg = test1ResponseChannel.receive();
            String response = (String) receivedMsg.getPayload();
            return response;
        } else if(externalSystem.equalsIgnoreCase("2")) {
            Message<?> message = MessageBuilder.withPayload(payload).copyHeaders(headers).build();
            test2RequestChannel.send(message);
            Message<?> receivedMsg = test2ResponseChannel.receive();
            String response = (String) receivedMsg.getPayload();
            return response;
        }
        return null;
    }
}

现在,如果我必须再添加一个通道,则必须添加XML格式的配置,还必须以代码形式自动装配新通道。我想要实现的是,我只想在XML中添加配置,并且我拥有频道

private PollableChannel[] requestChannels

private Map<String,PollableChannel> requestChannels

这样我就不必经常更新代码。 Spring Integration可以做到这一点吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)