This blog post is continuation of the first of the series about KIE Execution Server. In this article KIE Server Client will be introduced and used for basic operations on KIE Execution Server.
In the first part, we have went through the details of installation on Wildfly and verification with simple REST client to show it's actually working. This time we do pretty much the same verification although we expand it with further operations and make it via KIE Server Client instead.
So let's get started. We are going to use same container project (hr - org.jbpm:HR:1.0) that includes hiring process, that process has set of user tasks that we will be creating and working with. To be able to work on tasks our user (kieserver) needs to be member of the following roles used by the hiring process:
Once we have the the client instance we can start executing operations. We start with checking if the container we want to work with is already deployed and if not deploy it
Next let's check what is there available, in terms of processes and get some details about process id we are going to start
We have all the details so we are ready to start the process instance for hiring process. We set two process variables:
Once we started we can fetch tasks waiting to be completed for kieserver user
since the task has been completed and it has moved to another one we can continue until there are tasks available or we can simply abort the process instance to quit the work on this instance. Before we abort process instance let's examine what nodes has been completed so far
This will give us information if the task has already been completed and process moved on. Now let's abort the process instance
In the last step we get the process instance out to check if it was properly aborted - process instance state should be set to 3.
Last but not least, KIE Server Client can be used to insert facts and fire rules in very similar way
So that concludes simple usage scenario of KIE Server Client that covers
In the first part, we have went through the details of installation on Wildfly and verification with simple REST client to show it's actually working. This time we do pretty much the same verification although we expand it with further operations and make it via KIE Server Client instead.
So let's get started. We are going to use same container project (hr - org.jbpm:HR:1.0) that includes hiring process, that process has set of user tasks that we will be creating and working with. To be able to work on tasks our user (kieserver) needs to be member of the following roles used by the hiring process:
- HR
- IT
- Accounting
So to add these roles to our user we again use add-user script that comes with wildfly to simply update already existing user
NOTE: don't forget that kieserver user must have kie-server role assigned as well.
With that we are ready to start the server again
KIE Server Client
KIE Server Client is a lightweight library that custom application can use to interact with KIE Execution Server when is written in Java. That library extremely simplifies usage of the KIE Execution Server and make it easier to migrate between versions because it hides all internals that might change between versions.
To illustrate that it is actually lightweight here is the list of dependencies needed on runtime to execute KIE Server Client
[INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ kie-server-client --- [INFO] org.kie.server:kie-server-client:bundle:6.3.0-SNAPSHOT [INFO] +- org.kie:kie-api:jar:6.3.0-SNAPSHOT:compile [INFO] +- org.kie:kie-internal:jar:6.3.0-SNAPSHOT:compile [INFO] +- org.kie.server:kie-server-api:jar:6.3.0-SNAPSHOT:compile [INFO] | +- org.drools:drools-core:jar:6.3.0-SNAPSHOT:compile [INFO] | | +- org.mvel:mvel2:jar:2.2.6.Final:compile [INFO] | | \- commons-codec:commons-codec:jar:1.4:compile [INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.9.9:compile [INFO] | +- com.thoughtworks.xstream:xstream:jar:1.4.7:compile [INFO] | | +- xmlpull:xmlpull:jar:1.1.3.1:compile [INFO] | | \- xpp3:xpp3_min:jar:1.1.4c:compile [INFO] | \- org.apache.commons:commons-lang3:jar:3.1:compile [INFO] +- org.jboss.resteasy:jaxrs-api:jar:2.3.10.Final:compile [INFO] | \- org.jboss.logging:jboss-logging:jar:3.1.4.GA:compile [INFO] +- org.kie.remote:kie-remote-common:jar:6.3.0-SNAPSHOT:compile [INFO] +- org.codehaus.jackson:jackson-xc:jar:1.9.9:compile [INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.9:compile [INFO] +- org.slf4j:slf4j-api:jar:1.7.2:compile [INFO] +- org.jboss.spec.javax.jms:jboss-jms-api_1.1_spec:jar:1.0.1.Final:compile [INFO] +- com.sun.xml.bind:jaxb-core:jar:2.2.11:compile [INFO] \- com.sun.xml.bind:jaxb-impl:jar:2.2.11:compile
So let's setup a simple maven project that will use KIE Server Client to interact with the execution server
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>org.jbpm.test</groupid> <artifactid>kie-server-test</artifactid> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupid>org.kie</groupid> <artifactid>kie-internal</artifactid> <version>6.3.0-SNAPSHOT</version> </dependency> <dependency> <groupid>org.kie.server</groupid> <artifactid>kie-server-client</artifactid> <version>6.3.0-SNAPSHOT</version> </dependency> <dependency> <groupid>ch.qos.logback</groupid> <artifactid>logback-classic</artifactid> <version>1.1.2</version> </dependency> </dependencies>
That's all dependencies that are needed to have KIE Server Client embedded in custom application. Equipped with this we can start running KIE Server Client towards given server instance
Following is code snippet required to construct KIE Server Client instance using REST as transport
Following is code snippet required to construct KIE Server Client instance using REST as transport
String serverUrl = "http://localhost:8230/kie-server/services/rest/server"; String user = "kieserver"; String password = "kieserver1!"; String containerId = "hr"; String processId = "hiring"; KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password); // other formats supported MarshallingFormat.JSON or MarshallingFormat.XSTREAM configuration.setMarshallingFormat(MarshallingFormat.JAXB); // in case of custom classes shall be used they need to be added and client needs to be created with class loader that has these classes available //configuration.addJaxbClasses(extraClasses); //KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(configuration, kieContainer.getClassLoader()); KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(configuration);
Once we have the the client instance we can start executing operations. We start with checking if the container we want to work with is already deployed and if not deploy it
boolean deployContainer = true; KieContainerResourceList containers = kieServicesClient.listContainers().getResult(); // check if the container is not yet deployed, if not deploy it if (containers != null) { for (KieContainerResource kieContainerResource : containers.getContainers()) { if (kieContainerResource.getContainerId().equals(containerId)) { System.out.println("\t######### Found container " + containerId + " skipping deployment..."); deployContainer = false; break; } } } // deploy container if not there yet if (deployContainer) { System.out.println("\t######### Deploying container " + containerId); KieContainerResource resource = new KieContainerResource(containerId, new ReleaseId("org.jbpm", "HR", "1.0")); kieServicesClient.createContainer(containerId, resource); }
Next let's check what is there available, in terms of processes and get some details about process id we are going to start
// query for all available process definitions QueryServicesClient queryClient = kieServicesClient.getServicesClient(QueryServicesClient.class); List<ProcessDefinition> processes = queryClient.findProcesses(0, 10); System.out.println("\t######### Available processes" + processes); ProcessServicesClient processClient = kieServicesClient.getServicesClient(ProcessServicesClient.class); // get details of process definition ProcessDefinition definition = processClient.getProcessDefinition(containerId, processId); System.out.println("\t######### Definition details: " + definition);
We have all the details so we are ready to start the process instance for hiring process. We set two process variables:
- name - of type string
- age - of type integer
// start process instance Map<String, Object> params = new HashMap<String, Object>(); params.put("name", "john"); params.put("age", 25); Long processInstanceId = processClient.startProcess(containerId, processId, params); System.out.println("\t######### Process instance id: " + processInstanceId);
Once we started we can fetch tasks waiting to be completed for kieserver user
UserTaskServicesClient taskClient = kieServicesClient.getServicesClient(UserTaskServicesClient.class); // find available tasks List<TaskSummary> tasks = taskClient.findTasksAssignedAsPotentialOwner(user, 0, 10); System.out.println("\t######### Tasks: " +tasks); // complete task Long taskId = tasks.get(0).getId(); taskClient.startTask(containerId, taskId, user); taskClient.completeTask(containerId, taskId, user, null);
since the task has been completed and it has moved to another one we can continue until there are tasks available or we can simply abort the process instance to quit the work on this instance. Before we abort process instance let's examine what nodes has been completed so far
List<NodeInstance> completedNodes = queryClient.findCompletedNodeInstances(processInstanceId, 0, 10); System.out.println("\t######### Completed nodes: " + completedNodes);
This will give us information if the task has already been completed and process moved on. Now let's abort the process instance
// at the end abort process instance processClient.abortProcessInstance(containerId, processInstanceId); ProcessInstance processInstance = queryClient.findProcessInstanceById(processInstanceId); System.out.println("\t######### ProcessInstance: " + processInstance);
In the last step we get the process instance out to check if it was properly aborted - process instance state should be set to 3.
Last but not least, KIE Server Client can be used to insert facts and fire rules in very similar way
// work with rules List<GenericCommand> commands = new ArrayList<GenericCommand>(); BatchExecutionCommandImpl executionCommand = new BatchExecutionCommandImpl(commands); executionCommand.setLookup("defaultKieSession"); InsertObjectCommand insertObjectCommand = new InsertObjectCommand(); insertObjectCommand.setOutIdentifier("person"); insertObjectCommand.setObject("john"); FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand(); commands.add(insertObjectCommand); commands.add(fireAllRulesCommand); RuleServicesClient ruleClient = kieServicesClient.getServicesClient(RuleServicesClient.class); ruleClient.executeCommands(containerId, executionCommand); System.out.println("\t######### Rules executed");
So that concludes simple usage scenario of KIE Server Client that covers
- containers
- processes
- tasks
- rules
A complete maven project with this sample execution can be found here.
Enjoy and stay tuned for more to come about awesome KIE Execution Server :)