如何与许多生产者一起创建弹簧集成流程

时间:2016-12-02 16:18:22

标签: spring-integration

我读了这个SOF问题,我有一个补充问题。

我今天有一个类似于此的弹簧集成流程:

F is Y-X

我有几个(10)UDP源。

我想创建一个包含所有UDP源的流,所有这些都将数据推送到"字符串化"渠道。

我想从ArrayList获取所有UDP端口,然后遍历此列表并创建UDP源...

有可能吗?

2 个答案:

答案 0 :(得分:1)

是的,它被称为IntegrationFlowRegistrationhttps://spring.io/blog/2016/09/27/java-dsl-for-spring-integration-1-2-release-candidate-1-is-available

查看Dynamic TCP Sample的类似解决方案。

关键代码是这样的:

private MessageChannel createNewSubflow(Message<?> message) {
        String host = (String) message.getHeaders().get("host");
        Integer port = (Integer) message.getHeaders().get("port");
        Assert.state(host != null && port != null, "host and/or port header missing");
        String hostPort = host + port;

        TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory(host, port);
        TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
        handler.setConnectionFactory(cf);
        IntegrationFlow flow = f -> f.handle(handler);
        IntegrationFlowRegistration flowRegistration =
                this.flowContext.registration(flow)
                        .addBean(cf)
                        .id(hostPort + ".flow")
                        .register();
        MessageChannel inputChannel = flowRegistration.getInputChannel();
        this.subFlows.put(hostPort, inputChannel);
        return inputChannel;
    }

答案 1 :(得分:0)

以下是我如何使用Artem提供的提示来解决我的问题。

@Configuration
public class UdpSources {
    @Value("#{'${udp.nmea.listeningports}'.split(',')}")
    List<Integer> allUDPPorts;

    public static final String outChannel = "stringified";

    @Autowired
    private IntegrationFlowContext flowContext;

    @PostConstruct
    public void createAllUDPEndPoints(){
        for(int port : allUDPPorts){
            flowContext.registration(getUdpChannel(port)).autoStartup(false).id("udpSource"+port).register();
        }
    }

    private IntegrationFlow getUdpChannel(int port){
        return IntegrationFlows.from(new MulticastReceivingChannelAdapter("224.0.0.1", port)).
                transform(new ObjectToStringTransformer("UTF-8")).channel(outChannel).get();
    }
}