Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring集成多个UDP入站/出站通道_Java_Spring_Udp_Spring Integration - Fatal编程技术网

Java Spring集成多个UDP入站/出站通道

Java Spring集成多个UDP入站/出站通道,java,spring,udp,spring-integration,Java,Spring,Udp,Spring Integration,我正在尝试使用Spring boot构建一个要部署在多个节点上的模块。由于特定应用程序的时间限制,我不得不使用UDP,不能依赖Spring提供的更易于使用的REST工具 我必须能够向一组可能随时间变化的节点发送数据报(即,该组节点可能会增长或收缩,或者一些节点可能会移动到新的ip/端口“坐标”)。通信必须是单播的 我一直在阅读有关TCP和UDP支持的官方文档,但它相当。。。紧凑,不透明。关于这一点,org.springframework.integration类上的javadocs也相当简短。

我正在尝试使用Spring boot构建一个要部署在多个节点上的模块。由于特定应用程序的时间限制,我不得不使用UDP,不能依赖Spring提供的更易于使用的REST工具

我必须能够向一组可能随时间变化的节点发送数据报(即,该组节点可能会增长或收缩,或者一些节点可能会移动到新的ip/端口“坐标”)。通信必须是单播的

我一直在阅读有关TCP和UDP支持的官方文档,但它相当。。。紧凑,不透明。关于这一点,org.springframework.integration类上的javadocs也相当简短。 据我所知,“入站”通道用于发送数据包,而出站通道用于接收数据包

到目前为止,我还无法找到以下入站问题的答案(即“发送”频道,如果我理解得很好): -如何在运行时创建更多通道,以便将数据包发送到多个目的地? -如果主机被移动,我应该销毁通道并设置一个新的通道,还是可以在运行时更改通道的参数(目标ip/端口)

对于出站频道(“接收”频道,如果我理解得很好),我有与上述类似的问题,如: -如何在运行时设置多个通道? -如何在运行时更改现有频道的目的地,而不必将其拆下并重新设置?
-我应该只打开/关闭“原始”UDP套接字吗?

您已经反转了入站和出站

这里有一个例子,可以为你提供你所需要的;它使用一个酒吧/子频道来播放

@SpringBootApplication
public class So48213450Application {

    private final Map<Integer, IntegrationFlowRegistration> registrations = new HashMap<>();

    public static void main(String[] args) {
        SpringApplication.run(So48213450Application.class, args);
    }

    @Bean
    public PublishSubscribeChannel channel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public ApplicationRunner runner(PublishSubscribeChannel channel) {
        return args -> {
            makeANewUdpAdapter(1234);
            makeANewUdpAdapter(1235);
            channel.send(MessageBuilder.withPayload("foo\n").build());
            registrations.values().forEach(r -> {
                r.stop();
                r.destroy();
            });
        };
    }

    @Autowired
    private IntegrationFlowContext flowContext;

    public void makeANewUdpAdapter(int port) {
        System.out.println("Creating an adapter to send to port " + port);
        IntegrationFlow flow = IntegrationFlows.from(channel())
                .handle(Udp.outboundAdapter("localhost", port))
                .get();
        IntegrationFlowRegistration registration = flowContext.registration(flow).register();
        registrations.put(port, registration);
    }

}
您不能在运行时更改参数,您必须创建新的参数

编辑

以下是对您的评论的回应

您不能混合搭配spring集成jar(2.1.x和5.0.x);它们必须具有相同的版本。我上面的例子使用了boot2.0.0.M7(boot2计划在下个月发布)

Udp工厂类在5.0.0中添加到spring集成ip中

下面是boot 1.5.9和spring integration 4.3.13的一个类似示例(还添加了接收适配器)

@SpringBootApplication
public class So482134501Application {

    private final Map<Integer, IntegrationFlowRegistration> registrations = new HashMap<>();

    @Autowired
    private IntegrationFlowContext flowContext;

    public static void main(String[] args) {
        SpringApplication.run(So482134501Application.class, args);
    }

    @Bean
    public PublishSubscribeChannel channel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public ApplicationRunner runner(PublishSubscribeChannel channel) {
        return args -> {
            makeANewUdpInbound(1234);
            makeANewUdpInbound(1235);
            makeANewUdpOutbound(1234);
            makeANewUdpOutbound(1235);
            Thread.sleep(5_000);
            channel.send(MessageBuilder.withPayload("foo\n").build());
            this.registrations.values().forEach(r -> {
                r.stop();
                r.destroy();
            });
            this.registrations.clear();
        };
    }

    public void makeANewUdpOutbound(int port) {
        System.out.println("Creating an adapter to send to port " + port);
        IntegrationFlow flow = IntegrationFlows.from(channel())
                .handle(new UnicastSendingMessageHandler("localhost", port))
                .get();
        IntegrationFlowRegistration registration = flowContext.registration(flow).register();
        registrations.put(port, registration);
    }

    public void makeANewUdpInbound(int port) {
        System.out.println("Creating an adapter to receive from port " + port);
        IntegrationFlow flow = IntegrationFlows.from(new UnicastReceivingChannelAdapter(port))
                .<byte[], String>transform(String::new)
                .handle(System.out::println)
                .get();
        IntegrationFlowRegistration registration = flowContext.registration(flow).register();
        registrations.put(port, registration);
    }

}

非常感谢你。我还不能实际使用Udp.outboundAdapter(…)。我想我的pom文件可能有一些问题(这是一个maven项目)。父工件是SpringBootStarter父(1.5.9版本),对于集成,我有SpringIntegrationCore(5.0.0版本)和SpringIntegrationIP(2.1.3版本)。我正在使用Java8。我需要什么来解决这种依赖关系?在s-I-ip 5.0中添加了
Udp
工厂。有关boot 1.5.9版本,请参阅对我答案的编辑。我找到了丢失的软件包,现在我设法让它正常工作。再次感谢你。标记为正确答案的。
@SpringBootApplication
public class So482134501Application {

    private final Map<Integer, IntegrationFlowRegistration> registrations = new HashMap<>();

    @Autowired
    private IntegrationFlowContext flowContext;

    public static void main(String[] args) {
        SpringApplication.run(So482134501Application.class, args);
    }

    @Bean
    public PublishSubscribeChannel channel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public ApplicationRunner runner(PublishSubscribeChannel channel) {
        return args -> {
            makeANewUdpInbound(1234);
            makeANewUdpInbound(1235);
            makeANewUdpOutbound(1234);
            makeANewUdpOutbound(1235);
            Thread.sleep(5_000);
            channel.send(MessageBuilder.withPayload("foo\n").build());
            this.registrations.values().forEach(r -> {
                r.stop();
                r.destroy();
            });
            this.registrations.clear();
        };
    }

    public void makeANewUdpOutbound(int port) {
        System.out.println("Creating an adapter to send to port " + port);
        IntegrationFlow flow = IntegrationFlows.from(channel())
                .handle(new UnicastSendingMessageHandler("localhost", port))
                .get();
        IntegrationFlowRegistration registration = flowContext.registration(flow).register();
        registrations.put(port, registration);
    }

    public void makeANewUdpInbound(int port) {
        System.out.println("Creating an adapter to receive from port " + port);
        IntegrationFlow flow = IntegrationFlows.from(new UnicastReceivingChannelAdapter(port))
                .<byte[], String>transform(String::new)
                .handle(System.out::println)
                .get();
        IntegrationFlowRegistration registration = flowContext.registration(flow).register();
        registrations.put(port, registration);
    }

}
GenericMessage [payload=foo
, headers={ip_packetAddress=localhost/127.0.0.1:54881, ip_address=127.0.0.1, id=db7dae61-078c-5eb6-dde4-f83fc6c591d1, ip_port=54881, ip_hostname=localhost, timestamp=1515764556722}]
GenericMessage [payload=foo
, headers={ip_packetAddress=localhost/127.0.0.1:54880, ip_address=127.0.0.1, id=d1f79e79-569b-637b-57c5-549051f1b031, ip_port=54880, ip_hostname=localhost, timestamp=1515764556722}]