OpenRules Decision Manager deploys decision models as cloud-based decision services such as AWS Lambda with “one-click“. In this post I will explain how to invoke deployed OpenRules services from any Java application. There are at least 3 options:
- Using the standard Java HttpURLConnection
- Using DecisionServiceClient API
- Using automatically generated API
You can try to run all three options using the sample decision project “VacationDaysLambda” included in the standard OpenRules installation.
Usually a client application that invokes a decision microservice needs to know only two things:
- Service’s endpoint
- Java object or JSON request
Using the standard Java HttpURLConnection
Here is an example of a Java client that invokes a decision service by using only its endpoint and a request payload as a JSON string:
As an example, you may use the following main module to invoke the “vacation-days” decision service:
When you execute this code it will show the following results:
Using Decision Service Client API
Decision Manager provides a built-in Java class DecisionServiceClient. to invoke any OpenRules decision service Here is a simple code that invokes decision service “VacationDaysLambda” provided in the installation package:
This client creates a Java object “employee”, then a client for the remote decision service defined by an endpoint, executes the client for this employee, and immediately displays an updated employee:
Employee(id=Mary Grant, vacationDays=24, eligibleForExtra5Days=false, eligibleForExtra3Days=false,eligibleForExtra2Days=true, age=46, service=18)
If you use client.setTrace(true), it will also display the entire service response including all executed rules. You may get access to all executed rules programmatically, e.g. to add them to your own GUI for explanations. To do this, just add the following code shown in a red frame:
The rules will be shown as below:
Using Automatically Generated Client API
Decision Manager automatically generates a Java code for testing and invocation of the deployed services – you can find an example of the generated code “DecisionModelVacationDaysClient.java” in the folder target/generated-sources/main/java for the standard sample project “VacationDaysLambda”.
First, this code reads all test-employees defined in Excel using another generated class “DecisionModelVacationDaysTestModel”. Then for each test-employee it executes the generated client and displays the modified employee received from the service response: