读取canal binlog,rabbitmq 接收消息 全是数字问题

  1. 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 转换异常");
        }
        
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值