Skip to end of metadata
Go to start of metadata

Overview

MessagePack for Java is one implementation of MessagePack libraries in pure Java.  See jvm-serializers, which is one of well-known benchmarks for comparing Java libraries of data serialization.

API Reference

Install

Install from Maven2 repository

MessagePack for Java is released on Maven central repository.  You can configure your pom.xml as follows to use it:

Replace ${msgpack.version} with the current version of MessagePack for Java, which you can find on http://repo1.maven.org/maven2/org/msgpack/msgpack/.

Install from git repository

You can get latest source code using git.  

$ git clone git@github.com:msgpack/msgpack-java.git
$ cd msgpack-java
$ mvn package

Then you'll get the msgpack jar file in msgpack-java/target directory.  

QuickStart

Make a messagepackable class

@Message enables you to serialize "public" fields in objects of your own classes like this.  

If you want to serialize multiple objects sequentially, you can use Packer and Unpacker objects.  This is because MessagePack.write(Object) and read(byte[]) method invocations create Packer and Unpacker objects every times.  To use Packer and Unpacker objects, call createPacker(OutputStream) and createUnpacker(InputStream).  

Various types of values serialization/deserialization

Packer/Unpacker allows serializing/deserializing values of various types as follows.  They enable serializing/deserializing values of various types like values of primitive types, values of primitive wrapper classes, String objects, byte[] objects, ByteBuffer objects and so on.  As mentioned above, they also enable serializing/deserizing objects of your own classes annotated by @Message.  

write methods provided by Packer allows serializing various types of data.  

Unpacker provides deserialization methods for deserializing binary to primitive values.   For example, if you want to deserialize binary to value of boolean (or int) type, you can use readBoolean (or readInt) method in Unpacker.  

Unpacker also provides read methods for reference values.  Its methods allow deserializing binary to values of references which types you specified as parameters.   For example, if you want to deserialize binary to String (or byte[]) object, you have to describe a call of read(String.class) (or read(byte[].class)) method.  

List, Map objects serialization/deserialization

To serialize generic container objects like List and Map objects, you can use Template. Template objects are pairs of serializer and deserializer.   For example, to serialize a List object that has Integer objects as elements, you create the Template object by the following way.  

tList, TInteger are static method and field in Templates.  A simple example of List and Map objects is shown in the following.  

Without annotations

If you cannot append @Message to classes representing objects that you want to serialize, register method enables you to serialize the objects of the classes.  

For example, if MyMessage2 class is included in external library, you cannot easily modify the class declaration and append @Message to it.   register method allows to generate a pair of serializer and deserializer for MyMessage2 class automatically.   You can serialize objects of MyMessage2 class after executing the method.  

Optional fields

You can add new fields maintaining the compatibility.  Use the @Optional in the new fields.

If you try to deserialize the old version data, optional fields will be ignored.  

Dynamic typing

As Java is a static typing language, the MessagePack has achieved dynamic typing with Value.  Value has methods that checks its own type (isIntegerType(), isArrayType(), etc ...) and also converts to its own type (asStringValue(), convert(Template)).  

MessagePack-RPC for Java

API Reference

JavaDoc of RPC

Install

Install from Maven2 repository

The official Maven2 repository for MessagePack Java is located here.  

You could use the following pom.xml.  

Install from git repository

You can get latest source code using git.  

$ git clone git@github.com:msgpack/msgpack-rpc.git
$ cd msgpack-rpc/java/
$ mvn package

Then you'll get the msgpack jar file in msgpack/java/target directory.  

Dependency

MessagePack-RPC for Java depends on JBoss netty, for event-driven network I/O.  

It is automatically downloaded when you install via Maven2.  

QuickStart

First server program

This is a simple server program, which exposes the function named "hello". The function takes one integer argument, and returns it to the client.

First client program

This is a simple client program, to communicate with the above server program. The program remotely calls "hello" function.

Throw an error

You can send error to client using org.msgpack.rpc.Request.
If "hello" function is called, the method that has org.msgpack.rpc.Request for the first args is called inside.

Handle an error

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.