简体   繁体   中英

Is it possible use custom header in Spring Integration TCP?

I've been trying to create a simple example of Spring Integration TCP whose uses a custom UUID header in the Message and send this message thought the TCP channel. After that I recover the same message in other service, but the header isn't going to the server.

This is how I created the message:

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

This is my gateway class:

@MessagingGateway
public interface IntegrationGateway {


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

This is my "listener" in the other service:

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

    byte[] payload = message.getPayload();

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

}

But when I recover the message in the another service, this header is null .

Is it possible recover my custom header anyhow in the server?

TCP is streaming protocol; it has no concept of headers and payload.

The framework does provide a mechanism to map headers into the stream, using JSON, for example.

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.

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM