4.3. Firing Rules Using Kie Server Java Client API

To fire the rules, you need to sent a request to the decision server. In this guide, you will use the Kie Server Java Client API to send requests using REST API. You can use any client to send XML requests to the server. For more information about sending requests to the decision server, see the How to generate and send requests to Decision Server? Knowledgebase article.


Make sure you have configured your Maven installation to use JBoss BRMS online repositories. For more information about Maven configuration, see Chapter 7.4 from the Red Hat JBoss BPM Suite Installation Guide.
  1. Create a Basic Maven Archetype

    1. Navigate to a directory of your choice in your system and execute the following command:
      mvn archetype:generate -DgroupId=com.MyProject.app -DartifactId=my-app  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    2. This creates a directory called my-app with the following structure:
      	|-- pom.xml
      	`-- src
      	    |-- main
      	    |   `-- java
      	    |       `-- com
      	    |           `-- MyProject
      	    |               `-- app
      	    |                   `-- App.java
      	    `-- test
      	        `-- java
      	            `-- com
      	                `-- MyProject
      	                    `-- app
      	                        `-- AppTest.java
  2. Import Maven Dependencies

    1. You must declare the libraries your Maven project will use. Edit the my-app/pom.xml file to set the JBoss BRMS dependencies. Import following dependencies:


    The last dependency is the project you have built in JBoss BRMS. The model class (org.brms.myproject.Person in this example) in the client code, that is your Maven application, needs to match your server side, that is the JBoss BRMS. Otherwise, the rules will not match when sending a request. The easiest way to achieve this is to share the model class between both sides. You achieve this by adding it as a Maven dependency.
    Therefore, make sure that the following attributes match what you had entered while creating a new project in the Business Central:
    • groupId
    • artifactId
    • version
  3. Firing the Rule from TestApp

    1. Locate the testApp method in the my-app/src/test/java/com/MyProject/app/AppTest.java file (which is created by default by Maven).
    2. Edit the AppTest.java:
      1. Add the following imports:
        import org.kie.api.command.BatchExecutionCommand;
        import org.kie.server.api.model.ServiceResponse;
        import org.kie.server.client.KieServicesClient;
        import org.kie.server.client.KieServicesConfiguration;
        import org.kie.server.client.KieServicesFactory;
        import org.kie.server.client.RuleServicesClient;
        import org.kie.api.KieServices;
        import org.kie.api.command.Command;
        import java.util.ArrayList;
        import java.util.HashSet;
        import java.util.List;
        import java.util.Set;
        import org.brms.myproject.Person;
        import org.drools.core.command.impl.GenericCommand;
      2. Locate the testApp() method, load the knowledge base and fire your rule by adding the following code:
        Person p1 = new Person();
        String url = "http://localhost:8080/kie-server/services/rest/server";
        String username = "kieserver";
        String password = "kieserver1!";
        String container = "myContainer";
        String session = "mySession";
        KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(url, username, password);
        Set<Class<?>> allClasses = new HashSet<Class<?>>();
        KieServicesClient client  = KieServicesFactory.newKieServicesClient(config);
        RuleServicesClient ruleClient = client.getServicesClient(RuleServicesClient.class);
        List<GenericCommand<?>> commands = new ArrayList<GenericCommand<?>>();
        commands.add((GenericCommand<?>) KieServices.Factory.get().getCommands().newInsert(p1,"Person Insert ID"));
        commands.add((GenericCommand<?>) KieServices.Factory.get().getCommands().newFireAllRules("fire-identifier"));
        BatchExecutionCommand batchCommand = KieServices.Factory.get().getCommands().newBatchExecution(commands,session);
        ServiceResponse<String> response = ruleClient.executeCommands(container, batchCommand);
        Note that the default marshaller on the server side is JAXB, unless set differently. That means you must set its context with any custom classes you plan to use. In this example, that is Person.class. Since the example uses a stateless session, the marshaller is disposed of once a single command is executed. That is why you need to wrap multiple commands in the BatchCommand object.
        To change the marshaller type, enter one of the following commands and add the MarshallingFormat import to your project:
        • config.setMarshallingFormat(MarshallingFormat.JSON);
        • config.setMarshallingFormat(MarshallingFormat.XSTREAM);
        • import org.kie.server.api.marshalling.MarshallingFormat;
    3. Navigate to the my-app directory and execute the following command from the command line:
      mvn clean install
    4. Note that there is the Maven output and the JBoss BRMS output. The expected output in the JBoss BRMS console is:
      16:26:56,119 INFO  [stdout] (http-/ Hello Anton RedHat!
      16:26:56,119 INFO  [stdout] (http-/ You are rich!