uw.task.util.redis.msgpack.MsgpackSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of uw-task Show documentation
Show all versions of uw-task Show documentation
uw-task包是一个分布式任务框架,通过uw-task可以快速构建基于docker的分布式任务体系,同时支持基于influx的任务运维监控。
The newest version!
package uw.task.util.redis.msgpack;
import java.io.IOException;
import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.SerializationUtils;
/**
* 基于msgpack的序列化支持。 使用msgpack可以大大的减少内存占用,并提高解析速度。
*
* @author axeon
*
* @param
*/
public class MsgpackSerializer implements RedisSerializer {
private static final Logger logger = LoggerFactory.getLogger(MsgpackSerializer.class);
private Class extends MsgpackData> cls = null;
public MsgpackSerializer(Class extends MsgpackData> type) {
this.cls = type;
}
@Override
public byte[] serialize(T msg) throws SerializationException {
if (msg == null) {
return new byte[0];
}
MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
try {
msg.pack(packer);
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
try {
packer.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
return packer.toByteArray();
}
@SuppressWarnings("unchecked")
@Override
public T deserialize(byte[] data) throws SerializationException {
MsgpackData msg = null;
if (data == null || data.length == 0)
return null;
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(data);
if (unpacker != null) {
try {
msg = cls.newInstance();
msg.unpack(unpacker);
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
try {
unpacker.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return (T) msg;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy