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.

Note

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:
      	my-app
      	|-- 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:
      <dependency>
          <groupId>org.drools</groupId>
          <artifactId>drools-compiler</artifactId>
          <version>6.3.0.Final-redhat-5</version>
      </dependency>
      <dependency>
          <groupId>org.drools</groupId>
          <artifactId>drools-core</artifactId>
          <version>6.3.0.Final-redhat-5</version>
      </dependency>
      
      <dependency>
          <groupId>org.kie.server</groupId>
          <artifactId>kie-server-api</artifactId>
          <version>6.3.0.Final-redhat-5</version>
      </dependency>
      
      <dependency>
          <groupId>org.kie.server</groupId>
          <artifactId>kie-server-client</artifactId>
          <version>6.3.0.Final-redhat-5</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.13</version>
      </dependency>
      <dependency>
          <groupId>org.brms</groupId>
          <artifactId>MyProject</artifactId>
          <version>1.0.0</version>
      </dependency>

    Note

    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();
        p1.setFirstName("Anton");
        p1.setLastName("RedHat");
        p1.setHourlyRate(11);
        p1.setWage(20);
        
        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<?>>();
        allClasses.add(Person.class);
        config.addJaxbClasses(allClasses);
        
        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);
        System.out.println(response.getResult());
        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-/127.0.0.1:8080-5) Hello Anton RedHat!
      16:26:56,119 INFO  [stdout] (http-/127.0.0.1:8080-5) You are rich!