Meta-Programming == code that writes code (creating Java code from XML and XSD)

Following on from Why I love Apache Velocity where we discussed the use of meta-programming, code that writes code...

This was originally due to be entitled "MQTT 5.0 analytics platform, greenfield project build, part four" but again I thought I would go for a snappier title.

Github has some repositories from the FIX Trading Community - one of them includes an example of three FIX messages:

  • BusinessMessageReject
  • ExecutionReport
  • NewOrderSingle

All three expressed in XML for use with Simple Binary Encoding. Not included within the FIX GitHub repository is a way to render that XML into something useful, such as a NewOrderSingle.java class definition file. In this implementation Apache Velocity takes three inputs:

Examples.XML 

sbe.XSD

class.vm

As the Apache Velocity website states:

Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.

When Velocity is used for web development, Web designers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that web page designers can focus solely on creating a site that looks good, and programmers can focus solely on writing top-notch code. Velocity separates Java code from the web pages, making the web site more maintainable over its lifespan and providing a viable alternative to Java Server Pages (JSPs) or PHP.

Velocity's capabilities reach well beyond the realm of the web; for example, it can be used to generate SQL, PostScript and XML from templates. It can be used either as a standalone utility for generating source code and reports, or as an integrated component of other systems. For instance, Velocity provides template services for various web frameworks, enabling them with a view engine facilitating development of web applications according to a true MVC model.

At AlignmentSystems/CodeGen you can find a Java project that uses the XML and XSD files from the FIX Trading Community to naively create three Java class definition files.  The use of the word naive is accurate, in the sense that if you download the code and run it** you see that the datatypes from SBE are not accurately mapped to sensible options to create the correct bytes.  This is a demonstration of proof-of-concept, rather than production ready.

In future posts this will be extended and enhanced to show a polyglot approach - to create binary messages and the kdb+/Shakti database tables to store that binary data.

Why is this useful?

This CodeGen project was built for MQTT.  The MQTT implementation is proprietary, but this SBE implementation is free and open-source software.  The underlying concepts and design patterns are entirely relevant for any Electronic Trading project - whether using old fashioned FIXT session layer and tag=value or a FIXP-SBE-SOFH paradigm.

As an example:

public ${className}($foo){

#foreach($property in $properties )
#if( $foreach.hasNext )
this.$property.fieldName$space$equals$space$property.getFieldNameLowercaseFirstLetter()$space$comma
#else
this.$property.fieldName$space$equals$space$property.getFieldNameLowercaseFirstLetter()
#end
#end 
}

Is the code from the class.vm that creates the constructor: 

public NewOrderSingle(String clOrdId , String account , String symbol , sideEnum side , timestampEncoding transactTime , qtyEncoding orderQty , ordTypeEnum ordType , optionalDecimalEncoding price , optionalDecimalEncoding stopPx){

this.ClOrdId = clOrdId ,
this.Account = account ,
this.Symbol = symbol ,
this.Side = side ,
this.TransactTime = transactTime ,
this.OrderQty = orderQty ,
this.OrdType = ordType ,
this.Price = price ,
this.StopPx = stopPx
}


The entire set of class definition files are available on GitHub

Along with all of the source code.






**

It's a Gradle project using Java 10

All dependencies are packaged into a "FatJar" and can be downloaded by Gradle from mavenCentral.

Dependencies are logback, Apache Velocity and sl4j

There's a batch file to run called MQTT_ClassGen_Start.bat


See also:

Comments