Spring Integration multiple UDP inbound/outbound channels

爱⌒轻易说出口 提交于 2019-12-03 21:10:41

You have inbound and outbound reversed.

Here's an example that should provide you with what you need; it uses a pub/sub channel to broadcast...

@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);
    }

}

result:

$ nc -u -l 1234 &
[1] 56730
$ nc -u -l 1235 &
[2] 56739
$ jobs
[1]-  Running                 nc -u -l 1234 &
[2]+  Running                 nc -u -l 1235 &
$ foo
foo

You can't change parameters at runtime, you would have to create new ones.

EDIT

In response to your comments below...

You can't mix and match spring integration jars (2.1.x and 5.0.x); they must all be with the same version. My example above used Boot 2.0.0.M7 (boot 2 is scheduled to be released next month).

The Udp factory class was added to spring-integration-ip in 5.0.0.

Here is a similar example (which also adds receiving adapters) for boot 1.5.9 and 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);
    }

}

result:

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