问题
When I consume the message from kinesis stream. I get some junk chars with headers etc
@StreamListener(Processor.INPUT)
public void receive(String message) {
System.out.println("Message recieved: "+message);
throw new RuntimeException("Exception thrown");
}
@StreamListener("errorChannel")
public void transform(ErrorMessage errorMessage) throws UnsupportedEncodingException {
//original paylaod
System.out.println("Error Oiginal Message Payload"+new String((byte[])errorMessage.getOriginalMessage().getPayload(), "UTF-8"));
System.out.println("Error Original Message Stream channel"+errorMessage.getOriginalMessage().getHeaders().get("aws_receivedStream"));
}
Aplication yml
spring:
cloud:
stream:
bindings:
input:
group: abcd
destination: stream
content-type: application/json
errorChannelEnabled: true
consumer:
headerMode: raw
I get output at the both the listener and errorChannel with junk characters
I am trying to extract the original message in errorChannel . Is this the right way to convert the bytes message?
Message recieved: ?contentType "application/json"{"aa":"cc"}
回答1:
The AWS Kinesis doesn't provide any headers entity. So, to leverage such a functionality in Spring Cloud Stream, we are embedding headers into the body of the Kinesis record. For this purpose the headerMode
is embeddedHeaders
by default in the Kinesis Binder. And for symmetry between producer and consumer this option must not be changed.
The Framework provides out-of-the-box EmbeddedHeadersChannelInterceptor
for the target @StreamListener
channels and embedded headers are extracted and populated properly to the message to send.
When we handle errors in the errorChannel
, we indeed have an errorMessage.getOriginalMessage()
as non-transformed - original. And therefore the payload
of that message is a byte[]
from the record body containing embedded headers.
If you would like to parse them properly. you should use utility:
EmbeddedHeaderUtils.extractHeaders((Message<byte[]>) message, true);
来源:https://stackoverflow.com/questions/49284437/spring-cloud-stream-special-chars-in-message-received-from-kinesis