更新代码和过期的的测试代码,并且提交代码到 GIT 库中

This commit is contained in:
YuCheng Hu 2019-08-06 11:59:01 -04:00
parent cd27777ee3
commit a869f1dded
1 changed files with 182 additions and 0 deletions

View File

@ -55,3 +55,185 @@ $ mvn package
同时你也需要 https://code.google.com/p/json-simple/ 和 https://github.com/jboss-javassist/javassist 来让 msgpack.jar 可以在项目中使用。否则你将会收到 NoClassDefFoundError 错误。 同时你也需要 https://code.google.com/p/json-simple/ 和 https://github.com/jboss-javassist/javassist 来让 msgpack.jar 可以在项目中使用。否则你将会收到 NoClassDefFoundError 错误。
== 如何使用 == 如何使用
下面是如何使用的示例代码。
=== 使用一个消息打包message-packable
使用注解 '_@Message_' 来让你可以序列化你自己类中对象的 public 字段。
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Object.java 中下载到本地后进行编译测试。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
/**
* MessagePack6Objects
*
* @author yhu
*/
public class MessagePack6Object {
final Logger logger = LoggerFactory.getLogger(MessagePack6Object.class);
/**
* MessageData Message Object
*/
@Message // Annotation
public static class MessageData {
// public fields are serialized.
public String uuid;
public String name;
public double version;
}
/**
* Test MessagePack6Objects
*/
@Test
public void testMessagePack6Objects() {
logger.debug("MessagePack6Objects for Objects");
String uuid = UUID.randomUUID().toString();
// INIT OBJ
MessageData src = new MessageData();
src.uuid = uuid;
src.name = "MessagePack6";
src.version = 0.6;
try {
MessagePack msgPack = new MessagePack();
// Serialization
logger.debug("------ Serialization ------");
byte[] bytes = msgPack.write(src);
logger.debug("Bytes Array Length: [{}]", bytes.length);
// Deserialization
logger.debug("------ Deserialization ------");
MessageData dst = msgPack.read(bytes, MessageData.class);
logger.debug("Check Object for UUID: [{}]", dst.uuid);
assertEquals(uuid, dst.uuid);
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----
如果你希望按照顺序序列化多个对象的话,你可以使用 '_Packer_' 和 '_Unpacker_' 多个对象。
这是因为 '_MessagePack.write(Object)_' 和 '_read(byte[])_' 实际上每次都调用创建了 '_Packer_' 和 '_Unpacker_' 对象。
为了使用 '_Packer_' 和 '_Unpacker_' 对象,请调用 '_createPacker(OutputStream)_' 和 '_createUnpacker(InputStream)_'。
本代码可以 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Objects.java 中查看。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.msgpack.packer.Packer;
import org.msgpack.unpacker.Unpacker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
/**
* MessagePack6Objects
*
* @author yhu
*/
public class MessagePack6Objects {
final Logger logger = LoggerFactory.getLogger(MessagePack6Objects.class);
/**
* MessageData Message Objects
*/
@Message // Annotation
public static class MessageData {
// public fields are serialized.
public String uuid;
public String name;
public double version;
}
/**
* Test MessagePack6Objects
*/
@Test
public void testMessagePack6Objects() {
logger.debug("MessagePack6Objects for Objects");
String uuid = UUID.randomUUID().toString();
// INIT OBJ
MessageData src1 = new MessageData();
src1.uuid = uuid;
src1.name = "MessagePack6-src1";
src1.version = 0.6;
MessageData src2 = new MessageData();
src2.uuid = uuid;
src2.name = "MessagePack6-src2";
src2.version = 10.6;
MessageData src3 = new MessageData();
src3.uuid = uuid;
src3.name = "MessagePack6-src3";
src3.version = 1.6;
try {
MessagePack msgPack = new MessagePack();
// Serialization
logger.debug("------ Serialization ------");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgPack.createPacker(out);
packer.write(src1);
packer.write(src2);
packer.write(src3);
byte[] bytes = out.toByteArray();
logger.debug("Bytes Array Length: [{}]", bytes.length);
// Deserialization
logger.debug("------ Deserialization ------");
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Unpacker unpacker = msgPack.createUnpacker(in);
MessageData dst1 = unpacker.read(MessageData.class);
MessageData dst2 = unpacker.read(MessageData.class);
MessageData dst3 = unpacker.read(MessageData.class);
logger.debug("Check Object for UUID: [{}]", dst1.uuid);
assertEquals(uuid, dst1.uuid);
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----