Java Callout from Business Rules BR heading

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)

Kudos to ‘http://beautifulwaste.blogspot.nl/2006/03/callout-from-within-oracle-business.html’ who pointed me in the right direction. The blog has all the information but in a very condensed format. So, let’s do it in release 12.2.1 and with some more details.

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:

Java Callout from Business Rules BR 002 Java callout Java Class

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:

Java Callout from Business Rules BR 003 BR Java Facts

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:

Java Callout from Business Rules BR 004 BR Java Facts add jar to classpath

Don’t forget to select the Logger class:

Java Callout from Business Rules BR 005 BR Java Facts select Logger class

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

Java Callout from Business Rules BR 006 BR Java Facts Logger class added

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

br_010_br_add_function

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

br_020_br_rule_set_create

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

br_021_br_rule_set_started_event

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

br_022_br_rule_callout_to_logger

 

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’

br_001_simplecase_workspace

 

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:

br_029_soap_ui

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

br_030_log_line

Summary

  • Sample code can be found here.
  • Warning: please consider carefully if you want to use this mechanism: you may end up hiding business logic in places where you don’t want it to be
  • Enjoy!