B3 SBE Sample
Description
This sample illustrates adjustment os the SBE XML template to satisfy SBE requirements. In particular the sample shows how to remove unexpected “sinceVersion” attributes.
Source Code
import biz.onixs.sbe.*;
import biz.onixs.sbe.b3.B3Composites;
import biz.onixs.sbe.b3.OutboundBusinessHeader;
import biz.onixs.sbe.def.FramingHeaderSpecification;
import biz.onixs.sbe.def.MessageSchema;
import net.sf.practicalxml.DomUtil;
import net.sf.practicalxml.ParseUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;
/**
* Main sample class.
*/
public class B3Sample {
public B3Sample() {
}
static Document adjustB3XmlSchema(Document doc) {
// Composite nodes can not contain 'sinceVersion' attribute.
for (Element typesItem : DomUtil.getChildren(doc.getDocumentElement(), "types")) {
for (Element typeNode : DomUtil.getChildren(typesItem)) {
if ("composite".equals(typeNode.getTagName())) {
for (Element memberNode : DomUtil.getChildren(typeNode)) {
if (memberNode.hasAttribute("sinceVersion")) {
memberNode.removeAttribute("sinceVersion");
}
}
}
}
}
return doc;
}
private void run() throws Exception {
final File testsXmlFolder = new File("config").getAbsoluteFile();
final Document xmlSchema = adjustB3XmlSchema(ParseUtil.parse(new File(testsXmlFolder, "b3-entrypoint-messages-8.3.2.xml")));
final MessageSchema msgSchema = new MessageSchema(xmlSchema, FramingHeaderPolicy.getCustomPolicy(FramingHeaderSpecification.B3_FRAMING_HEADER));
final ByteDecoder codec = new ByteDecoderFactory().create(msgSchema, B3Composites.getCustomComposites());
final byte[] buffer = new byte[65536];
{
IMessage msg = codec.encode(buffer, 0, buffer.length, 702);
OutboundBusinessHeader header = codec.createEmptyCustomComposite(OutboundBusinessHeader.class);
SbeTimestamp sendingTime = new SbeTimestamp();
codec.createEmptyComposite("UTCTimestampNanosOptional", sendingTime);
sendingTime.setValue(System.currentTimeMillis() * 1000000); // Convert to nanos
header.setSessionID(1);
header.setMsgSeqNum(123);
header.setSendingTime(sendingTime);
header.setEventIndicator((byte)0);
msg.setCustomComposite(35524, header);
msg.setByte(1373, 3);
msg.setByte(1374, 6);
msg.setInt(11, 999777333);
msg.setInt(1369, 180180);
SbeTimestamp transactTime = (SbeTimestamp) codec.createEmptyComposite("UTCTimestampNanos", null);
transactTime.setValue(System.currentTimeMillis() * 1000000);
msg.setTimestamp(60, transactTime);
msg.setChar(1375, '1');
// Keep empty the massActionRejectReason field.
msg.setVarData(58, "Report text".getBytes());
}
{
IMessage msg = codec.decode(buffer, 0, buffer.length);
System.out.println(msg.toFixString('|'));
// Obtain custom composite
OutboundBusinessHeader header = codec.createEmptyCustomComposite(OutboundBusinessHeader.class);
msg.getCustomComposite(35524, header);
// Examine header fields
System.out.println("Header content: {"
+ "SessionID: " + header.getSessionID()
+ ", MsgSeqNum:" + header.getMsgSeqNum()
+ ", SendingTime: " + header.getSendingTime()
+ ", PossResend: " + ((header.getEventIndicator() & 0x1) > 0 ? 1 : 0)
+ "}");
}
}
/**
* Program entry point.
* @param args arguments.
*/
public static void main(String[] args) {
try {
B3Sample sample = new B3Sample();
sample.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Usage
- Run the sample:
- win: runB3Sample.bat
- linux: runB3Sample.sh
- Clean everything:
- win: clean.bat
- linux: clean.sh
Java SBE Decoder