是否可以在 Spring 集成 TCP 中使用自定义 header?

Is it possible use custom header in Spring Integration TCP?

我一直在尝试创建一个 Spring 集成 TCP 的简单示例,它在消息中使用自定义 UUID header 并通过 TCP 通道发送此消息。之后,我在其他服务中恢复了相同的消息,但是 header 不会发送到服务器。

我是这样创建消息的:

           Message<byte[]> message1 = MessageBuilder
          .withPayload(payload)
          .setHeader("traceId", traceId)
          .build();

这是我的网关class:

@MessagingGateway
public interface IntegrationGateway {


  @Gateway(requestChannel = "toTcp")
  String toOut(Message<byte[]> message);
}

这是我在其他服务中的“监听器”:

  @ServiceActivator(inputChannel = "fromTcp")
  public void convert(Message<byte[]> message) {

    byte[] payload = message.getPayload();

    UUID traceId = message.getHeaders().get("traceId", UUID.class);

}

但是当我在另一个服务中恢复消息时,这个header是null

是否可以在服务器中恢复我的自定义 header?

TCP是流媒体协议;它没有 headers 和 payload.

的概念

该框架确实提供了一种将 headers 映射到流中的机制,例如使用 JSON。

Docs here.

TCP is a streaming protocol. Serializers and Deserializers demarcate messages within the stream. Prior to 3.0, only message payloads (String or byte[]) could be transferred over TCP. Beginning with 3.0, you can transfer selected headers as well as the payload. However, “live” objects, such as the replyChannel header, cannot be serialized.

Sending header information over TCP requires some additional configuration.

The first step is to provide the ConnectionFactory with a MessageConvertingTcpMessageMapper that uses the mapper attribute. This mapper delegates to any MessageConverter implementation to convert the message to and from some object that can be serialized and deserialized by the configured serializer and deserializer.

Spring Integration provides a MapMessageConverter, which allows the specification of a list of headers that are added to a Map object, along with the payload. The generated Map has two entries: payload and headers. The headers entry is itself a Map and contains the selected headers.

...