Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65830009/is-it-possible-use-custom-header-in-spring-integration-tcp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...