22.3. Example requests with the KIE Server Java client API
The following are examples of KIE Server Java client API requests for basic interactions with KIE Server. For the full list of available KIE Server Java clients, download the Red Hat Decision Manager 7.9.1 Source Distribution from the Red Hat Customer Portal and navigate to ~/rhdm-7.9.1-sources/src/droolsjbpm-integration-$VERSION/kie-server-parent/kie-server-remote/kie-server-client/src/main/java/org/kie/server/client.
- Listing KIE Server capabilities
You can use the
org.kie.server.api.model.KieServerInfoobject to identify server capabilities. TheKieServicesClientclient requires the server capability information to correctly produce service clients. You can specify the capabilities globally inKieServicesConfiguration; otherwise they are automatically retrieved from KIE Server.Example request to return KIE Server capabilities
public void listCapabilities() { KieServerInfo serverInfo = kieServicesClient.getServerInfo().getResult(); System.out.print("Server capabilities:"); for (String capability : serverInfo.getCapabilities()) { System.out.print(" " + capability); } System.out.println(); }- Listing KIE containers in KIE Server
KIE containers are represented by the
org.kie.server.api.model.KieContainerResourceobject. The list of resources is represented by theorg.kie.server.api.model.KieContainerResourceListobject.Example request to return KIE containers from KIE Server
public void listContainers() { KieContainerResourceList containersList = kieServicesClient.listContainers().getResult(); List<KieContainerResource> kieContainers = containersList.getContainers(); System.out.println("Available containers: "); for (KieContainerResource container : kieContainers) { System.out.println("\t" + container.getContainerId() + " (" + container.getReleaseId() + ")"); } }You can optionally filter the KIE container results using an instance of the
org.kie.server.api.model.KieContainerResourceFilterclass, which is passed to theorg.kie.server.client.KieServicesClient.listContainers()method.Example request to return KIE containers by release ID and status
public void listContainersWithFilter() { // Filter containers by releaseId "org.example:container:1.0.0.Final" and status FAILED KieContainerResourceFilter filter = new KieContainerResourceFilter.Builder() .releaseId("org.example", "container", "1.0.0.Final") .status(KieContainerStatus.FAILED) .build(); // Using previously created KieServicesClient KieContainerResourceList containersList = kieServicesClient.listContainers(filter).getResult(); List<KieContainerResource> kieContainers = containersList.getContainers(); System.out.println("Available containers: "); for (KieContainerResource container : kieContainers) { System.out.println("\t" + container.getContainerId() + " (" + container.getReleaseId() + ")"); } }- Creating and disposing KIE containers in KIE Server
You can use the
createContaineranddisposeContainermethods in theKieServicesClientclient to dispose and create KIE containers. In this example, when you dispose a container, theServiceResponsereturns aVoidresponse. When you create a container, theServiceResponsereturns aKieContainerResourceobject.Example request to dispose and re-create a KIE container
public void disposeAndCreateContainer() { System.out.println("== Disposing and creating containers =="); // Retrieve list of KIE containers List<KieContainerResource> kieContainers = kieServicesClient.listContainers().getResult().getContainers(); if (kieContainers.size() == 0) { System.out.println("No containers available..."); return; } // Dispose KIE container KieContainerResource container = kieContainers.get(0); String containerId = container.getContainerId(); ServiceResponse<Void> responseDispose = kieServicesClient.disposeContainer(containerId); if (responseDispose.getType() == ResponseType.FAILURE) { System.out.println("Error disposing " + containerId + ". Message: "); System.out.println(responseDispose.getMsg()); return; } System.out.println("Success Disposing container " + containerId); System.out.println("Trying to recreate the container..."); // Re-create KIE container ServiceResponse<KieContainerResource> createResponse = kieServicesClient.createContainer(containerId, container); if(createResponse.getType() == ResponseType.FAILURE) { System.out.println("Error creating " + containerId + ". Message: "); System.out.println(responseDispose.getMsg()); return; } System.out.println("Container recreated with success!"); }- Executing runtime commands in KIE Server
Red Hat Decision Manager supports runtime commands that you can send to KIE Server for asset-related operations, such as inserting or retracting objects in a KIE session or firing all rules. The full list of supported runtime commands is located in the
org.drools.core.command.runtimepackage in your Red Hat Decision Manager instance.You can use the
org.kie.api.command.KieCommandsclass to insert commands, and useorg.kie.api.KieServices.get().getCommands()to instantiate theKieCommandsclass. If you want to add multiple commands, use theBatchExecutionCommandwrapper.Example request to insert an object and fire all rules
import org.kie.api.command.Command; import org.kie.api.command.KieCommands; import org.kie.server.api.model.ServiceResponse; import org.kie.server.client.RuleServicesClient; import org.kie.server.client.KieServicesClient; import org.kie.api.KieServices; import java.util.Arrays; ... public void executeCommands() { String containerId = "hello"; System.out.println("== Sending commands to the server =="); RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class); KieCommands commandsFactory = KieServices.Factory.get().getCommands(); Command<?> insert = commandsFactory.newInsert("Some String OBJ"); Command<?> fireAllRules = commandsFactory.newFireAllRules(); Command<?> batchCommand = commandsFactory.newBatchExecution(Arrays.asList(insert, fireAllRules)); ServiceResponse<String> executeResponse = rulesClient.executeCommands(containerId, batchCommand); if(executeResponse.getType() == ResponseType.SUCCESS) { System.out.println("Commands executed with success! Response: "); System.out.println(executeResponse.getResult()); } else { System.out.println("Error executing rules. Message: "); System.out.println(executeResponse.getMsg()); } }注記A conversation between a client and a specific KIE Server container in a clustered environment is secured by a unique
conversationID. TheconversationIDis transferred using theX-KIE-ConversationIdREST header. If you update the container, unset the previousconversationID. UseKieServiesClient.completeConversation()to unset theconversationIDfor Java API.