Spring集成入站适配器自动端口

时间:2018-01-29 10:48:15

标签: java spring udp spring-integration

Gary Russell友好地回答了关于Spring Integration udp流的previous question of mine。从那里开始,我偶然发现了端口问题。

Spring Integration documentation表示可以将0添加到入站通道适配器端口,操作系统将为适配器选择一个可用端口,可以在运行时调用 >适配器对象上的getPort()。问题是在运行时我只是得到0 如果我尝试以编程方式检索端口。 这是“我的”代码(即Russel对我之前使用的Spring Integration 4.3.12的上一个问题的回答的略微修改版本。)

@SpringBootApplication
public class  TestApp  {

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

@Autowired
private IntegrationFlowContext flowContext;

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

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

@Bean
public TestData test() {
    return new TestData();
}

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

         makeANewUdpInbound(1235);
         makeANewUdpOutbound(1235);
         Thread.sleep(5_000);
         channel().send(MessageBuilder.withPayload("bar\n").build());
         this.registrations.values().forEach(r -> {
           r.stop();
           r.destroy();
         });
         this.registrations.clear();
    };
}

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

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

我读的输出是

Creating an adapter to receive from port 0
Creating an adapter to send to port 0
Creating an adapter to receive from port 1235
Creating an adapter to send to port 1235
GenericMessage [payload=bar, headers={ip_packetAddress=127.0.0.1/127.0.0.1:54374, ip_address=127.0.0.1, id=c95d6255-e63a-433d-3723-c389fe66b060, ip_port=54374, ip_hostname=127.0.0.1, timestamp=1517220716983}]

我怀疑该库确实在OS选择的空闲端口上创建了适配器,但我无法检索分配的端口。

1 个答案:

答案 0 :(得分:0)

端口是异步分配的;你需要等到实际分配端口。有点像...

int n = 0;
while (n++ < 100 && ! source.isListening()) {
    Thread.sleep(100;
}
if (!source.isListening()) {
    // failed to start in 10 seconds.
}

我们应该增强适配器以在端口准备好时发出事件。随意打开一个改进&#39; JIRA Issue