Java API for Decision Service Invocation

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:

  1. Service’s endpoint
  2. 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:

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.