使用 Java DSL 与 Spring 集成的动态 TCP 服务器

Dynamic TCP Server with Spring Integration using Java DSL

我正在尝试通过读取包含连接详细信息的 属性 文件来创建 TCP 服务器和客户端。 我在以下 参考文档的帮助下使用动态和运行时集成流( 9.20 动态和运行时集成流)

代码在创建客户端时工作正常但是当我使用相同的代码创建服务器时代码更改如下:

    IntegrationFlow flow = f -> f
            .handle(Tcp.inboundAdapter(Tcp.netServer(2221)
                    .serializer(TcpCodecs.crlf())
                    .deserializer(TcpCodecs.lengthHeader1())
                    .id("server")))
            .transform(Transformers.objectToString());

    IntegrationFlowRegistration theFlow = this.flowContext.registration(flow).register();

我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.String] for method match: [public java.lang.Class<?> org.springframework.integration.dsl.IntegrationComponentSpec.getObjectType(), public S org.springframework.integration.dsl.MessageProducerSpec.outputChannel(java.lang.String), public S org.springframework.integration.dsl.MessageProducerSpec.outputChannel(org.springframework.messaging.MessageChannel), public org.springframework.integration.ip.dsl.TcpInboundChannelAdapterSpec org.springframework.integration.ip.dsl.TcpInboundChannelAdapterSpec.taskScheduler(org.springframework.scheduling.TaskScheduler), public S org.springframework.integration.dsl.MessageProducerSpec.errorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy), public S org.springframework.integration.dsl.MessageProducerSpec.phase(int), public S org.springframework.integration.dsl.MessageProducerSpec.autoStartup(boolean), public S org.springframework.integration.dsl.MessageProducerSpec.sendTimeout(long)]
    at org.springframework.util.Assert.isNull(Assert.java:155)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:843)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:362)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:231)
    at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:225)
    at org.springframework.integration.handler.MethodInvokingMessageProcessor.<init>(MethodInvokingMessageProcessor.java:60)
    at org.springframework.integration.handler.ServiceActivatingHandler.<init>(ServiceActivatingHandler.java:38)
    at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:924)
    at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:904)
    at org.springframework.integration.dsl.IntegrationFlowDefinition.handle(IntegrationFlowDefinition.java:891)
    at org.springframework.integration.samples.dynamictcp.DynamicTcpClientApplication.lambda(DynamicTcpClientApplication.java:194)
    at org.springframework.integration.config.dsl.IntegrationFlowBeanPostProcessor.processIntegrationFlowImpl(IntegrationFlowBeanPostProcessor.java:268)
    at org.springframework.integration.config.dsl.IntegrationFlowBeanPostProcessor.postProcessBeforeInitialization(IntegrationFlowBeanPostProcessor.java:96)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:423)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1702)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
    ... 16 common frames omitted

请帮我解决以上问题。 我还找到了 dynamic tcp client 的代码,但没有动态 tcp 服务器的代码(任何资源或 link,我可以在其中考虑创建动态服务器)。

你在混淆责任。 Tcp.inboundAdapter() 必须是 IntegrationFlow 链中的第一个。考虑改用这个:

IntegrationFlow flow =  
   IntegrationFlows.from(Tcp.inboundAdapter(Tcp.netServer(2221)
                .serializer(TcpCodecs.crlf())
                .deserializer(TcpCodecs.lengthHeader1())
                .id("server")))
        .transform(Transformers.objectToString())
        .get();