-
canal 读取mysql binlog 推送rabbitmq时,未指定contentType,AbstractJackson2MessageConverter解析数据时,需判断contentType,因为MessageProperties 为null,content也为null,未转码直接返回byte[]数组。
;源码如下public Object fromMessage(Message message, @Nullable Object conversionHint) throws MessageConversionException { Object content = null; MessageProperties properties = message.getMessageProperties(); if (properties != null) { String contentType = properties.getContentType(); if (contentType != null && contentType.contains(this.supportedContentType.getSubtype())) { String encoding = properties.getContentEncoding(); if (encoding == null) { encoding = this.getDefaultCharset(); } try { if (conversionHint instanceof ParameterizedTypeReference) { content = this.convertBytesToObject(message.getBody(), encoding, this.objectMapper.getTypeFactory().constructType(((ParameterizedTypeReference)conversionHint).getType())); } else if (this.getClassMapper() == null) { JavaType targetJavaType = this.getJavaTypeMapper().toJavaType(message.getMessageProperties()); content = this.convertBytesToObject(message.getBody(), encoding, targetJavaType); } else { Class<?> targetClass = this.getClassMapper().toClass(message.getMessageProperties()); content = this.convertBytesToObject(message.getBody(), encoding, targetClass); } } catch (IOException var8) { throw new MessageConversionException("Failed to convert Message content", var8); } } else if (this.log.isWarnEnabled()) { this.log.warn("Could not convert incoming message with content-type [" + contentType + "], '" + this.supportedContentType.getSubtype() + "' keyword missing."); } } if (content == null) { content = message.getBody(); } return content; }
2、解决方案, 通过String 转换byte[] 数组,指定字符编码即可,看源码方便解决!
@RabbitListener(queues = "xxxqueue")
public void recieveSyncMessage(byte[] message) {
String realMsg = null;
try {
realMsg = new String(message, CHARSET);
} catch (Exception e) {
log.error("接收canal同步es 转换异常 数据: {}", message);
throw new SyncException("接收canal同步es 转换异常");
}
}