Spring 集成:TcpOutboundGateway 获取 DestinationResolutionException:无 output-channel 或 replyChannel header 可用

Spring Integration: TcpOutboundGateway getting DestinationResolutionException: no output-channel or replyChannel header available

我正在尝试使用 TcpOutboundGateway 通过 TCP 与遗留(非 spring)系统通信并处理响应,但我收到以下错误:DestinationResolutionException: no output-channel or replyChannel header可用

示例遗留侦听器的代码启动,然后启动 spring 引导应用程序。

我正在努力弄清楚我做错了什么 - 或者我是否采取了正确的方法。如有任何建议,我们将不胜感激。

(我在这里发布一些注释时遇到格式问题,所以删除了@) TY

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Test {

    private static MessageChannel sendChannel;

    @Bean
    public MessageChannel replyChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageChannel sendChannel() {
        MessageChannel directChannel = new DirectChannel();
        sendChannel = directChannel;
        return directChannel;
    }

    @Bean
    TcpNetClientConnectionFactory tcpNetClientConnectionFactory() {
        TcpNetClientConnectionFactory tcpNetClientConnectionFactory = new TcpNetClientConnectionFactory("me.me", 9003);
        return tcpNetClientConnectionFactory;
    }

    @Bean
    @ServiceActivator(inputChannel = "sendChannel", outputChannel = "replyChannel", requiresReply = "true")
    TcpOutboundGateway tcpOutboundGateway() {
        TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
        tcpOutboundGateway.setConnectionFactory(tcpNetClientConnectionFactory());
        tcpOutboundGateway.start();
        return tcpOutboundGateway;
    }


    public static void main(String args[]) {
        new LegacyApplication();

        SpringApplication.run(Test.class, args);
        Test.sendChannel.send(new GenericMessage("mgr?task=-1"));
    }
}

@MessageEndpoint
class ReplyListener {

    @ServiceActivator(inputChannel = "replyChannel")
    public void telemetryHandler(String message) {
        System.out.println(message);
    }

}

class LegacyApplication implements Runnable {

    public LegacyApplication() {
        (new Thread(this)).start();
    }

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(9003);
            Socket clientSocket = serverSocket.accept();
            System.out.println("LegacyApplication: Accepted");
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

            System.out.println("LegacyApplication: " + in.readLine());
            out.write("OK\r\n");
            out.flush();

            System.out.println("LegacyApplication: Done");
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请参阅 the documentation 中的注释。当用 @ServiceActivator 注释 @Bean 时,输出通道需要在消息处理程序(网关 bean)上进行,而不是注释。

您还应该调用start()。让上下文来做。