czwartek, 3 grudnia 2015

KIE Server: Extend KIE Server with additional transport

There might be some cases where existing transports in KIE Server won't be sufficient, for whatever reason

not fast enough

difficult to deal with string based data formats (JSON, XML)

you name it..so there might be a need to build a custom transport to overcome this limitation.

Use case

Add additional transport to KIE Server that allows to use Drools capabilities. For this example we will use Apache Mina as underlying transport framework and we're going to exchange string based data that will still rely on existing marshaling operations. For simplicity reason we support only JSON format.

Before you start create empty maven project (packaging jar) with following dependencies:

Implement KieServerExtension

Main part of this implementation is done by implementing org.kie.server.services.api.KieServerExtension which is KIE Server extension main interface. This interface has number of methods which implementation depends on the actual needs:

In our case we don't need to do anything when container is created or disposed as we simply extend the Drools extension and rely on complete setup in that component. For this example we are mostly interested in implementing:

init method

destroy method

in these two methods we are going to manage life cycle of the Apache Mina server.

getImplementedCapability - should instruct what kind of capability is covered by this extension, note that capability should be unique within KIE Server

getExtensionName - human readable name of this extension

getStartOrder - defined when given extension should be started, important for extensions that have dependencies to other extensions like in this case where it depends on Drools (startup order is set to 0) so our extension should start after drools one - thus set to 20

Remaining methods are left with standard implementation to fulfill interface requirements.

Here is the implementation of the KIE Server extension based on Apache Mina:

each incoming request is single line, so make sure before submitting anything to it make sure it's single line

there is a need to pass container id in this single line so this handler expects following format:

containerID|payload

response is set the way it is produced by marshaller and that can be multiple lines

handlers allows "stream mode" that allows to send commands without disconnecting from KIE Server session. to be able to quit the stream mode - send either exit or quit

Make it discoverable

Same story as for REST extension ... once we have all that needs to be implemented, it's time to make it discoverable so KIE Server can find and register this extension on runtime. Since KIE Server is based on Java SE ServiceLoader mechanism we need to add one file into our extension jar file:

META-INF/services/org.kie.server.services.api.KieServerExtension

And the content of this file is a single line that represents fully qualified class name of our custom implementation of KieServerExtension.

Last step is to build this project (which will result in jar file) and copy the result into:

kie-server.war/WEB-INF/lib

Since this extension depends on Apache Mina we need to copy mina-core-2.0.9.jar into kie-server.war/WEB-INF/lib as well.

Usage example

Clone this repositoryand build the kie-server-demo project. Once you build it you will be able to deploy it to KIE Server (either directly using KIE Server management REST api) or via KIE workbench controller.

Once deployed and KIE Server started you should find in logs that new KIE Server extension started:
Drools-Mina KIE Server extension -- Mina server started at localhost and port 9123
Drools-Mina KIE Server extension has been successfully registered as server extension

That means we are now interact with our Apache Mina based transport in KIE Server. So let's give it a go... we could write a code to interact with Mina server but to avoid another coding exercise let's use... wait for it .... telnet :)

4 komentarze:

Great article. I am facing a KIE Server protocol extension with Apache Thrift, mainly for PHP users. This would create a contract and centralize Rule and Model for several languages into one maven based project. Wrapping the drools core like KIE Server into a EE7 war seems to be most efficient. Any plans on writing a blog entry on this theme?

Have never used Apache Thrift so can't say how easy/difficult it would be to create such extension. But from very quick look at the web site it does look similar to Mina - so you would have to start Thrift server as part of your extension as it's done for this example with Mina. Then you have two options:- same as in this example single server per KIE Server instance- new server per container deployedthe difference in implementation is on which life cycle method of extension you start and stop server and the rest is exactly the same

Thank you for your answer, to be more specific, i only use the thrift serialization part and for transport rest, jms, or if i have to, soap. So i guess i will be extending the kie-server-parent with marshalling capabilities.