public String send(VisitorErrorLogEntity centerDto, String tag) {
LOGGER.info("rocketMq待send:centerDto[{}],tag[{}]", JSON.toJSONString(centerDto),tag);
SendResult sendResult;
synchronized (sync) {
//搞复杂了 转成字节应该有更简单的办法
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream stream = null;
try {
//ObjectOutputStream的writeObject不是线程安全的,多线程时有概率最后stream null、sendResult null--2021年8月25日调用时加个synchronized
stream = new ObjectOutputStream(outputStream);
stream.writeObject(centerDto);
} catch (IOException e) {
e.printStackTrace();
}
byte[] msg = outputStream.toByteArray();
sendResult = null;
try {
Message message = new Message(topics, tag, msg);
LOGGER.info("rocketMq-Message:", message);
sendResult = producer.send(message);
} catch (Exception e) {
e.printStackTrace();
}
LOGGER.info("rocketMq生产者(tag:{})-发送结果:{}",tag,sendResult);
}
return sendResult==null?null:"{\"MsgId\":\"" + sendResult.getMsgId() + "\"}";
}
超过4M