Getting started

The OnixS SBE toolset has two core tasks:

  • build SBE messages in binary form;
  • decode SBE messages from binary streams.

Common steps

For both encoding and decoding you need:

  1. An SBE XML template that describes message layout.
  2. A MessageSchema built from that template.
  3. A ByteDecoder created from the schema.

The following SbeXmlTemplate.xml is a minimal template:

<sbe:messageSchema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:sbe="http://fixprotocol.io/2016/sbe"
                   package="sbe.doc" id="1" version="0" semanticVersion="1.0"
                   description="Documentation sample" byteOrder="littleEndian" xsi:schemaLocation="http://fixprotocol.io/2016/sbe sbe.xsd">
    <types>
        <composite name="messageHeader" description="Message header.">
            <type name="blockLength" primitiveType="uint16"/>
            <type name="templateId" primitiveType="uint16"/>
            <type name="schemaId" primitiveType="uint16"/>
        </composite>
    </types>

    <sbe:message name="CustomMessage" id="101" description="The message for getting started.">
        <field name="messageType" type="char" id="35" presence="constant" value="X"/>
        <field name="IntField" type="int32" id="9001"/>
    </sbe:message>
</sbe:messageSchema>

The minimum code to build and initialize ByteDecoder:

ByteDecoder decoder = null;
try {
    MessageSchema schema = new MessageSchema(ParseUtil.parse(new File("SbeXmlTemplate.xml")));
    decoder = new ByteDecoderFactory().create(schema);
} catch (LicenseException e) {
    throw new RuntimeException(e);
}

The decoder instance is reusable and can handle many messages.

SBE message encoding

Use the following flow to encode a message:

Example:

        // Choose template ID for the message encoded
        int templateID = 101;

        // Allocate buffer.
        final int LargeBufferSize = 10_000;
        byte[] buffer = new byte[LargeBufferSize];

        // Obtain IMessage instance that will handle the message with chosen template ID:
        IMessage msgEncoded = decoder.encode(buffer, 0, buffer.length, templateID);

        // Fill the message fields:
        msgEncoded.setInt(9001, 50);

        // Get the encoded data:
        byte[] dataToUse = Arrays.copyOf(msgEncoded.getBuffer(), msgEncoded.getMsgSize());

SBE message decoding

Decoding is usually simpler:

  • Receive a binary packet from network, file, or queue.
  • Call ByteDecoder.decode(...) to bind IMessage to this packet.
  • Read fields and message size from the returned object.

Example:

        // The dataFromNetwork declared somewhere as:
        //
        // byte[] dataFromNetwork;

        IMessage msgDecoded = decoder.decode(dataFromNetwork, 0, dataFromNetwork.length);

        // Number of bytes occupied by the SBE message.
        int bytesUsed = msgDecoded.getMsgSize();

        // Template ID of the message.
        int templateId = msgDecoded.getTemplateId();

        // Read message field:
        int value = msgDecoded.getInt(9001);