Generating Java Code with SBE¶
The Simple Binary Encoding (SBE) tool generates Java code from an XML schema. The SBE tool is a standalone JAR file that can be run from the command line or integrated into a build system.
Gradle Sample¶
A simple, fixed-length message type is defined in the following XML schema.
Assume that it, along with some additional messages and some top level metadata,
is saved in a file named schema-01.xml:
schema-01.xml
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
package="com.shaunlaurens.pa"
id="1000"
version="1"
semanticVersion="pa0.1"
description="Schema 1 for the PA samples, version 0.1">
<types>
<composite name="messageHeader"
description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
</types>
<sbe:message name="MessageType1" id="1"
description="A simple, fixed length message type">
<field name="field1" id="1" type="int64"/>
<field name="field2" id="2" type="int32"/>
<field name="field3" id="3" type="int64"/>
</sbe:message>
</sbe:messageSchema>
We can then use the SBE tool to generate Java code for this schema. The following Gradle file (note, the sample uses Kotlin Script) includes the core components to generate the Java code and compile it:
build.gradle.kts
plugins {
`java-library`
}
@Suppress("DEPRECATION")
val generatedDir = file("${buildDir}/generated/src/main/java") //1(1)
val codecGeneration = configurations.create("codecGeneration") //2(2)
dependencies {
"codecGeneration"(libs.sbe) //3(3)
implementation(libs.agrona)
testImplementation(libs.bundles.testing)
}
sourceSets {
main {
java.srcDir(generatedDir) //4(4)
}
}
tasks {
task("generateCodecs", JavaExec::class) {
group = "sbe"
val codecsFile = "src/main/resources/schema-01.xml" //5(5)
val xsdFile = "src/main/resources/fpl/sbe.xsd" //6(6)
mainClass.set("uk.co.real_logic.sbe.SbeTool")
args = listOf(codecsFile)
inputs.files(codecsFile, xsdFile)
outputs.dir(generatedDir) //7(7)
classpath = codecGeneration
systemProperties["sbe.output.dir"] = generatedDir //8 (8)
systemProperties["sbe.validation.xsd"] = xsdFile
systemProperties["sbe.validation.stop.on.error"] = "true"
systemProperties["sbe.target.language"] = "Java"
}
compileJava {
dependsOn("generateCodecs")
}
}
- This is the directory to which the
generated codeis written to. - This creates a code configuration in Gradle for our generated code
- Our codec generation task will depend on the SBE library
- We must tell gradle in to include the generated code in the source set
- The location of the schema file
- The location of the SBE XSD file, as used for validation
- The generated code is written to the
generatedDir; this informs the Gradle task of the output directory - The SBE tool is additionally configured to output Java code to
generatedDir
Please see the complete example on GitHub for more details.