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:
- Make and deploy Java code
- Java callout from Business rules
- Deploy the sample case
- 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:
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:
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:
Don’t forget to select the Logger class:
Here’s how it looks like: the Logger class as a Java Fact:
Add a logging function named ‘logMessage’. Goto the ‘Functions’ tab and click the ‘create’ button, and add the function as shown below:
Next, the java fact can be used. Select the tab ‘SimpleCaseRules’ under ‘Rule Sets’ and create a new Decision Table:
In the Decision Table, create the condition that looks for a Case Start event:
Next, under the action section, call the logMessage function that was created earlier:
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’
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:
Now, fire the start operation and observe in the domain log file the following entry:
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!