spring集成多通道

oalqel3c  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(253)

我希望使用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中添加config,并且我有如下通道

private PollableChannel[] requestChannels

private Map<String, PollableChannel> requestChannels

这样我就不用经常更新代码了。这在spring集成中是可能的吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题