Tuesday, 21 March 2017

Java Callout from Business Rules

Goal and warning


This blog will show how to invoke java code directly from within a Business Rules component. I didn’t manage to find this in the Oracle product documentation, which is actually a good thing. Invoking java code with business logic directly from within the Business Rules is normally bad design. The proper way to do it would be to first invoke business rules and then – depending on the outcome – invoke the appropriate business logic. Hence, the proper place for business logic is outside of the business rules.
However, for more technical requirements, it may be convenient to do a java callout from within the business rules. The sample situation we have is that a logging line will be generated upon a ‘case start event’. No business logic!

Oracle position


The fact that this is so poorly documented in the official Oracle documentation can mean a couple of things:
  • Oracle thinks that ‘java callouts from business rules that implement business logic is a bad idea’
  • Oracle may even not support the java callout from business rules any more in the near future?
  • I need new glasses because I overlooked the Oracle documentation that clearly describes this (if yes – please send me the reference)
Steps for java callout:
  1. Make and deploy Java code
  2. Java callout from Business rules
  3. Deploy the sample case
  4. Test

1. Java code


Our java code is a logger: it will do a simple println of a string, with 1 argument. The result will be visible in the server log file.

First, create the java code:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Important: the java code has to comply to JAXB standards, i.e. for each of the internal fields that are used in the java callout class, the accessors (get/set) also have to be defined. (In this simple example there are no fields).

The java class files have to be made available in 2 locations: JDeveloper and the server.
  • JDeveloper: shown in the next section
  • Server: make a deployment profile and deploy the java code into a jar file. In the example, that is ‘SimpleCaseLogger.jar’. The resulting jar file has to be put in the $DOMAIN_HOME/lib directory, and the server re-booted.

2. Java callout from Business Rules


First, the java classes must be added in the Business Rules editor as java facts.

Open the case business rules, select the tab Facts and sub-tab Java Facts:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Click the ‘Create Java Facts’ button and then on the ‘Create Java Fact’ pop-up click the ‘Add to Classpath’ button. Here, add the SimpleCaseLogger.jar file:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Don’t forget to select the Logger class:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Here’s how it looks like: the Logger class as a Java Fact:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Add a logging function named ‘logMessage’. Goto the ‘Functions’ tab and click the ‘create’ button, and add the function as shown below:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Next, the java fact can be used. Select the tab ‘SimpleCaseRules’ under ‘Rule Sets’ and create a new Decision Table:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

In the Decision Table, create the condition that looks for a Case Start event:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Next, under the action section, call the logMessage function that was created earlier:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

3. Deploy the sample case


The sample case, the java code and the SoapUI test project can be found here.

The sample case is a simple ACM case, with one Case Activity named ‘Approval’. That Case Activity is a BPMN process that has 1 human task. The Case Activity is automatically started when the case is started. That’s about it.

If you want to test the example, just download the sources and then:
  • in the weblogic console, add group ‘approvers’ and user ‘approver’
  • make the user ‘approver’ member of group ‘approvers’
  • deploy the case from JDeveloper onto your test system
  • login into BPM Process Workspace under user ‘weblogic’ and goto the Administration section
  • add user ‘weblogic’ to the role ‘SimpleCase.ProcessOwner’
  • add user ‘approver’ to the role ‘SimpleCase.Approver’
Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

4. Test


Open the SoapUI project named ‘SimpleCase-soapui-project.xml’ and (1) change the end-point to point to your specific installation and (2) optionally change the data in the startCase operation for the case:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide

Now, fire the start operation and observe in the domain log file the following entry:

Oracle Java Tutorials, Oracle Java Certifications, Oracle Java Guide