Spring 集成 TCP 不发送消息

Spring Integration TCP doesn't send messages

我一直在尝试使用 Spring 集成 TCP 创建消费者和生产者。我在监听部分取得了一些成功(我正常收到了 telnet 发送的消息),但是当我尝试将同样的消息发送到终端客户端时,没有任何反应。

这些是我的 类:

@EnableIntegration
@IntegrationComponentScan
@Configuration
public class TcpIntegration {

  @Value("${tcp.port}")
  private Integer port;

  @MessagingGateway(defaultRequestChannel="toTcp")
  public interface Gateway {
    String viaTcp(String in);
  }

  @Bean
  @ServiceActivator(inputChannel="toTcp")
  public TcpSendingMessageHandler tcpOutGate(AbstractClientConnectionFactory connectionFactory) {
    TcpSendingMessageHandler gate = new TcpSendingMessageHandler();
    gate.setConnectionFactory(connectionFactory);
    return gate;
  }

  @Bean
  public TcpReceivingChannelAdapter tcpInGate(AbstractServerConnectionFactory connectionFactory)  {
    TcpReceivingChannelAdapter inGate = new TcpReceivingChannelAdapter();
    inGate.setConnectionFactory(connectionFactory);
    inGate.setOutputChannel(fromTcp());
    return inGate;
  }

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

  @Bean
  public AbstractClientConnectionFactory clientCF() {
    return new TcpNetClientConnectionFactory("localhost", this.port);
  }

  @Bean
  public AbstractServerConnectionFactory serverCF() {
    return new TcpNetServerConnectionFactory(this.port);
  }
}

TcpListener

@MessageEndpoint
@AllArgsConstructor
public class TcpListener {

  private final Gateway gateway;

  @ServiceActivator(inputChannel = "fromTcp")
  public void convert(String payload) {
    System.out.println(payload);
    gateway.viaTcp(payload);
  }
}

为什么不起作用?

您需要通过设置 ip_connnetionId header 来告诉适配器在哪个套接字上发送消息。

  @ServiceActivator(inputChannel = "fromTcp")
  public void convert(String payload, @Header(IpHeaders.CONNECTION_ID String cid) {
    System.out.println(payload);
    gateway.viaTcp(payload, cid);
  }
  @MessagingGateway(defaultRequestChannel="toTcp")
  public interface Gateway {
    @Gateway(@GatewayHeader(IPHeaders.CONNECTION_ID, "#args[1]"))
    String viaTcp(String in, String cid);
  }

编辑

我的语法有点不对;这是一个工作示例...

@SpringBootApplication
public class So65597331Application {

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

    @MessagingGateway(defaultRequestChannel = "toTcp")
    public interface Gate {
        @Gateway(payloadExpression = "#args[0]",
                headers = @GatewayHeader(name = IpHeaders.CONNECTION_ID, expression = "#args[1]"))
        String viaTcp(String in, String cid);
    }

    @Bean
    @ServiceActivator(inputChannel = "toTcp")
    public TcpSendingMessageHandler tcpOutGate(AbstractServerConnectionFactory connectionFactory) {
        TcpSendingMessageHandler gate = new TcpSendingMessageHandler();
        gate.setConnectionFactory(connectionFactory);
        return gate;
    }

    @Autowired
    private Gate gateway;

    @ServiceActivator(inputChannel = "fromTcp")
    public void convert(String payload, @Header(IpHeaders.CONNECTION_ID) String cid) {
        System.out.println(payload);
        gateway.viaTcp(payload, cid);
    }

    @Bean
    public TcpReceivingChannelAdapter tcpInGate(AbstractServerConnectionFactory connectionFactory) {
        TcpReceivingChannelAdapter inGate = new TcpReceivingChannelAdapter();
        inGate.setConnectionFactory(connectionFactory);
        inGate.setOutputChannel(fromTcp());
        return inGate;
    }

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

    @Bean
    public AbstractServerConnectionFactory serverCF() {
        return new TcpNetServerConnectionFactory(1234);
    }

}
$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
foo
foo
Connection closed by foreign host.