io.github.hpsocket.soa.starter.rocketmq.util.RocketmqHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hp-soa-starter-rocketmq Show documentation
Show all versions of hp-soa-starter-rocketmq Show documentation
hp-soa: a fully functional, easy-to-use, and highly scalable microservice framework
package io.github.hpsocket.soa.starter.rocketmq.util;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.apache.rocketmq.client.apis.message.MessageView;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
/** RocketMQ 通用辅助类 */
public class RocketmqHelper
{
/** {@linkplain MessageView} 消息内容转为 {@linkplain byte[]} */
public static final byte[] getMessageViewBody(MessageView messageView)
{
ByteBuffer byteBuffer = messageView.getBody();
byte[] body = new byte[byteBuffer.remaining()];
byteBuffer.get(body);
return body;
}
/** {@linkplain MessageView} 消息内容转为 {@linkplain String} */
public static final String getMessageViewBodyAsString(MessageView messageView)
{
return StandardCharsets.UTF_8.decode(messageView.getBody()).toString();
}
/** {@linkplain MessageView} 消息内容转为 {@linkplain JSONObject} */
public static final JSONObject getMessageViewBodyAsJsonObject(MessageView messageView)
{
return JSON.parseObject(getMessageViewBody(messageView));
}
/** {@linkplain MessageView} 消息内容转为指定类型对象 */
public static final T getMessageViewBodyAsObject(MessageView messageView, Class clazz)
{
return JSON.parseObject(getMessageViewBody(messageView), clazz);
}
}