To view the supported extensions of a running KIE Server instance, send a
GET request to the following REST API endpoint and review the XML or JSON server response:
Base URL for GET request for KIE Server information
Example JSON response with KIE Server information
*.server.ext.disabled KIE Server system property. For example, to disable the BRM capability, set the system property org.drools.server.ext.disabled=true. For all KIE Server system properties, see
KIE Server system properties.
By default, KIE Server extensions are exposed through REST or JMS data transports and use predefined client APIs. You can extend existing KIE Server capabilities with additional REST endpoints, extend supported transport methods beyond REST or JMS, or extend functionality in the KIE Server client.
This flexibility in KIE Server functionality enables you to adapt your KIE Server instances to your business needs, instead of adapting your business needs to the default KIE Server capabilities.
Extending an existing KIE Server capability with a custom REST API endpoint
The KIE Server REST API enables you to interact with your KIE containers and business assets (such as business rules, processes, and solvers) in jBPM without using the Business Central user interface. The available REST endpoints are determined by the capabilities enabled in your KIE Server system properties (for example,org.drools.server.ext.disabled=false for the BRM capability). You can extend an existing KIE Server capability with a custom REST API endpoint to further adapt the KIE Server REST API to your business needs.
As an example, this procedure extends the Drools KIE Server extension (for the BRM capability) with the following custom REST API endpoint:
Example custom REST API endpoint
-
Create an empty Maven project and define the following packaging type and dependencies in the
pom.xmlfile for the project:Example pom.xml file in the sample project -
Implement the
org.kie.server.services.api.KieServerApplicationComponentsServiceinterface in a Java class in your project, as shown in the following example:Sample implementation of the KieServerApplicationComponentsService interface- Delivers REST endpoints to the KIE Server infrastructure that is deployed when the application starts.
- Specifies the extension that you are extending, such as the
Droolsextension in this example. - Returns all resources that the REST container must deploy. Each extension that is enabled in your KIE Server instance calls the
getAppComponentsmethod, so theif ( !OWNER_EXTENSION.equals(extension) )call returns an empty collection for any extensions other than the specifiedOWNER_EXTENSIONextension. - Lists the services from the specified extension that you want to use, such as the
RulesExecutionServiceandKieServerRegistryservices from theDroolsextension in this example. - Specifies the transport type for the extension, either
RESTorJMS(RESTin this example), and theCustomResourceclass that returns the resource as part of thecomponentslist.
-
Implement the
CustomResourceclass that KIE Server can use to provide the additional functionality for the new REST resource, as shown in the following example:In this example, theSample implementation of the CustomResource classCustomResourceclass for the custom endpoint specifies the following data and behavior:- Uses the base endpoint
server/containers/instances/{containerId}/ksession - Uses the
POSTHTTP method - Expects the following data to be given in REST requests:
- The
containerIdas a path argument - The
ksessionIdas a path argument - List of facts as a message payload
- The
- Supports all KIE Server data formats:
- XML (JAXB, XStream)
- JSON
- Unmarshals the payload into a
List<?>collection and, for each item in the list, creates anInsertCommandinstance followed byFireAllRulesandGetObjectcommands. - Adds all commands to the
BatchExecutionCommandinstance that calls to the Drools engine.
- Uses the base endpoint
-
To make the new endpoint discoverable for KIE Server, create a
META-INF/services/org.kie.server.services.api.KieServerApplicationComponentsServicefile in your Maven project and add the fully qualified class name of theKieServerApplicationComponentsServiceimplementation class within the file. For this example, the file contains the single lineorg.kie.server.ext.drools.rest.CustomDroolsKieServerApplicationComponentsService. -
Build your project and copy the resulting JAR file into the
~/kie-server.war/WEB-INF/libdirectory of your project. -
Start KIE Server and deploy the built project to the running KIE Server. You can deploy the project using either the Business Central interface or the KIE Server REST API (a
PUTrequest tohttp://SERVER:PORT/kie-server/services/rest/server/containers/{containerId}). After your project is deployed on a running KIE Server, you can start interacting with your new REST endpoint. For this example, you can use the following information to invoke the new endpoint:-
Example request URL:
http://localhost:8080/kie-server/services/rest/server/containers/instances/demo/ksession/defaultKieSession -
HTTP method:
POST -
HTTP headers:
Content-Type: application/jsonAccept: application/json
-
Example message payload:
-
Example server response:
200(success) -
Example server log output:
-
Example request URL:
Extending KIE Server to use a custom data transport
By default, KIE Server extensions are exposed through REST or JMS data transports. You can extend KIE Server to support a custom data transport to adapt KIE Server transport protocols to your business needs. As an example, this procedure adds a custom data transport to KIE Server that uses theDrools extension and that is based on Apache MINA, an open-source Java network-application framework. The example custom MINA transport exchanges string-based data that relies on existing marshalling operations and supports only JSON format.
Procedure
-
Create an empty Maven project and define the following packaging type and dependencies in the
pom.xmlfile for the project:Example pom.xml file in the sample project -
Implement the
org.kie.server.services.api.KieServerExtensioninterface in a Java class in your project, as shown in the following example:TheSample implementation of the KieServerExtension interfaceKieServerExtensioninterface is the main extension interface that KIE Server can use to provide the additional functionality for the new MINA transport. The interface consists of the following components:Overview of the KieServerExtension interface- Specifies the capability that is covered by this extension. The capability must be unique within KIE Server.
- Defines a human-readable name for the extension.
- Determines when the specified extension should be started. For extensions that have dependencies on other extensions, this setting must not conflict with the parent setting. For example, in this case, this custom extension depends on the
Droolsextension, which hasStartOrderset to0, so this custom add-on extension must have a start order greater than0(set to20in the sample implementation).
MinaDroolsKieServerExtensionsample implementation of this interface, theinitmethod is the main element for collecting services from theDroolsextension and for bootstrapping the MINA server. All other methods in theKieServerExtensioninterface can remain with the standard implementation to fulfill interface requirements. TheTextBasedIoHandlerAdapterclass is the handler on the MINA server that reacts to incoming requests. -
Implement the
TextBasedIoHandlerAdapterhandler for the MINA server, as shown in the following example:In this example, the handler class receives text messages and executes them in theSample implementation of the TextBasedIoHandlerAdapter handlerDroolsservice. Consider the following handler requirements and behavior when you use theTextBasedIoHandlerAdapterhandler implementation:- Anything that you submit to the handler must be a single line because each incoming transport request is a single line.
- You must pass a KIE container ID in this single line so that the handler expects the format
containerID|payload. - You can set a response in the way that it is produced by the marshaller. The response can be multiple lines.
- The handler supports a stream mode that enables you to send commands without disconnecting from a KIE Server session. To end a KIE Server session in stream mode, send either an
exitorquitcommand to the server.
-
To make the new data transport discoverable for KIE Server, create a
META-INF/services/org.kie.server.services.api.KieServerExtensionfile in your Maven project and add the fully qualified class name of theKieServerExtensionimplementation class within the file. For this example, the file contains the single lineorg.kie.server.ext.mina.MinaDroolsKieServerExtension. -
Build your project and copy the resulting JAR file and the
mina-core-2.0.9.jarfile (which the extension depends on in this example) into the~/kie-server.war/WEB-INF/libdirectory of your project. -
Start the KIE Server and deploy the built project to the running KIE Server. You can deploy the project using either the Business Central interface or the KIE Server REST API (a
PUTrequest tohttp://SERVER:PORT/kie-server/services/rest/server/containers/{containerId}). After your project is deployed on a running KIE Server, you can view the status of the new data transport in your KIE Server log and start using your new data transport:For this example, you can use Telnet to interact with the new MINA-based data transport in KIE Server:New data transport in the server logStarting Telnet and connecting to KIE Server on port 9123 in a command terminalExample interactions with KIE Server in a command terminalExample server log output
Extending the KIE Server client with a custom client API
KIE Server uses predefined client APIs that you can interact with to use KIE Server services. You can extend the KIE Server client with a custom client API to adapt KIE Server services to your business needs. As an example, this procedure adds a custom client API to KIE Server to accommodate a custom data transport (configured previously for this scenario) that is based on Apache MINA, an open-source Java network-application framework. Procedure-
Create an empty Maven project and define the following packaging type and dependencies in the
pom.xmlfile for the project:Example pom.xml file in the sample project -
Implement the relevant
ServicesClientinterface in a Java class in your project, as shown in the following example:A specific interface is required because you must register client implementations based on the interface, and you can have only one implementation for a given interface. For this example, the custom MINA-based data transport uses theSample RulesMinaServicesClient interfaceDroolsextension, so this exampleRulesMinaServicesClientinterface extends the existingRuleServicesClientclient API from theDroolsextension. -
Implement the
RulesMinaServicesClientinterface that KIE Server can use to provide the additional client functionality for the new MINA transport, as shown in the following example:This example implementation specifies the following data and behavior:Sample implementation of the RulesMinaServicesClient interface- Uses socket-based communication for simplicity
- Relies on default configurations from the KIE Server client and uses
ServerUrlfor providing the host and port of the MINA server - Specifies JSON as the marshalling format
- Requires received messages to be JSON objects that start with an open bracket
{ - Uses direct socket communication with a blocking API while waiting for the first line of the response and then reads all lines that are available
- Does not use stream mode and therefore disconnects the KIE Server session after invoking a command
-
Implement the
org.kie.server.client.helper.KieServicesClientBuilderinterface in a Java class in your project, as shown in the following example:Sample implementation of the KieServicesClientBuilder interface- Enables you to provide additional client APIs to the generic KIE Server client infrastructure
- Defines the KIE Server capability (extension) that the client uses
- Provides a map of the client implementations, where the key is the interface and the value is the fully initialized implementation
-
To make the new client API discoverable for the KIE Server client, create a
META-INF/services/org.kie.server.client.helper.KieServicesClientBuilderfile in your Maven project and add the fully qualified class name of theKieServicesClientBuilderimplementation class within the file. For this example, the file contains the single lineorg.kie.server.ext.mina.client.MinaClientBuilderImpl. -
Build your project and copy the resulting JAR file into the
~/kie-server.war/WEB-INF/libdirectory of your project. -
Start KIE Server and deploy the built project to the running KIE Server. You can deploy the project using either the Business Central interface or the KIE Server REST API (a
PUTrequest tohttp://SERVER:PORT/kie-server/services/rest/server/containers/{containerId}). After your project is deployed on a running KIE Server, you can start interacting with your new KIE Server client. You use your new client in the same way as the standard KIE Server client, by creating the client configuration and client instance, retrieving the service client by type, and invoking client methods. For this example, you can create aRulesMinaServiceClientclient instance and invoke operations on KIE Server through the MINA transport:Sample implementation to create the RulesMinaServiceClient clientSample configuration to invoke operations on KIE Server through the MINA transport