【问题标题】:Spring integration inbound adapter automatic portSpring集成入站适配器自动端口
【发布时间】:2018-01-29 10:48:15
【问题描述】:

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

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}]

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

【问题讨论】:

    标签: java spring udp spring-integration


    【解决方案1】:

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

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

    我们可能应该增强适配器以在端口准备好时发出事件。随时打开“改进”JIRA Issue

    【讨论】:

    • 再次感谢 Russel 先生。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2015-03-21
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 2012-07-04
    相关资源
    最近更新 更多