Mediator Error Handling – Java Call from Fault Policy File

By rajesh | January 7, 2014 | 0 Comment

We use fault handling framework to handle errors in BPEL & Mediator components, In BPEL we can also caught faults using Catch & CatchAll block but there is no such provision in Mediator. In Mediator we have to use fault policy and fault binding files to handle all types of error.

In Mediator, fault name is same for all types of fault so in fault policy file we need to caught only one fault for mediator.

<faultName xmlns:medns=”http://schemas.oracle.com/mediator/faults” name=”medns:mediatorFault”>

Note: Fault policies are applicable to parallel routing rules only. For sequential routing rules, the fault goes back to the caller. It is the responsibility of the caller to handle the fault.

In this post, we show you how to call java code from fault policy file whenever there is any fault in mediator.

Let us first create one java code which we want to call from fault policy file. To do that create a project in Jdeveloper and add fabric-runtime.jar file to project libraries.

Farbric-runtime.jar file can be found at below location.

$Middleware_Home\jdeveloper\soa\modules\oracle.soa.fabric_11.1.1

Untitled

Our custom class should implement IFaultRecoveryJavaClass interface which has two functions defined inside it.

oracle.integration.platform.faultpolicy.IFaultRecoveryJavaClass {

public void handleRetrySuccess(IFaultRecoveryContext ctx);

public String handleFault(IFaultRecoveryContext ctx);

}

The handleRetrySuccess method is invoked upon a successful retry attempt and the handleFault method is invoked to execute a policy of type javaAction.

Untitled

There are many functions defined for IFaultRecoveryContext interface as mentioned below. So you can use required function as per your requirement.

public interface IFaultRecoveryContext {

/**

* Gets implementation type of the fault.

* @return

*/

public String getType();

/**

* @return Get property set of the fault policy action being executed.

*/

public Map getProperties();

/**

* @return Get fault policy id of the fault policy being executed.

*/

public String getPolicyId();

/**

* @return Name of the faulted reference.

*/

public String getReferenceName();

/**

* @return Port type of the faulted reference link.

*/

public QName getPortType();

}

 

Our custom class should look like below.

Untitled

 

package faultpolicyjavacode;

import java.io.PrintStream;

import java.util.Map;

import oracle.integration.platform.faultpolicy.IFaultRecoveryContext;

import oracle.integration.platform.faultpolicy.IFaultRecoveryJavaClass;

 

public class TestClass implements IFaultRecoveryJavaClass {

public void handleRetrySuccess(IFaultRecoveryContext iFaultRecoveryContext) {

handleFault(iFaultRecoveryContext);

}

public String handleFault(IFaultRecoveryContext iFaultRecoveryContext) {

System.out.println(“*******************”);

System.out.println(“Fault policy id: ” +

iFaultRecoveryContext.getPolicyId());

System.out.println(“************************”);

return “OK”;

}

}

Now compile your custom class, prepare jar file and deploy jar file to lib folder of soa server.

You need to copy jar file to below path

Path: ($ORACLE_MIDDLEWARE_HOME/user_projects/domains/$domain_name/lib).

 

Now we create one sample mediator project. Create a new project in Jdeveloper and add synchronous mediator to it. Now add fault-policy file to it, your fault-policy file should look like below.

<?xml version=”1.0″ encoding=”UTF-8″?>

<faultPolicies>

<faultPolicy version=”2.0.1″ id=”Mediator_ServiceFaults”>

<Conditions>

<faultName xmlns:medns=”http://schemas.oracle.com/mediator/faults”

name=”medns:mediatorFault”>

<condition>

<action ref=”ora-java”/>

</condition>

</faultName>

</Conditions>

<Actions>

<Action id=”ora-java”>

<javaAction className=”faultpolicyjavacode.TestClass”

defaultAction=”ora-terminate”>

<returnValue value=”OK” ref=”ora-human-intervention”/>

</javaAction>

</Action>

</Actions>

</faultPolicy>

</faultPolicies>

 

Now create fault-bindings.xml file and add below content to it.

<?xml version=”1.0″ encoding=”UTF-8″?>

<faultPolicyBindings version=”2.0.1″

xmlns=”http://schemas.oracle.com/bpel/faultpolicy”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>

<composite faultPolicy=”Mediator_ServiceFaults”/>

</faultPolicyBindings>

 

As explained earlier we can only handle mediator fault when we use “Parallel” routing rules. So add a parallel routing route and call your target service. When you do request mapping, do some deliberate mapping so that your mediator failed during runtime, In our case we used local dvm file in mapping which really don’t exist.

Untitled

Now you need to refer fault-policies.xml and fault-bindings.xml files in composite.xml file.

Untitled

 

Deploy your composite and see the testing result.

In soa server console, you should be able to see below message.

*****************

Fault policy id: Mediator_ServiceFaults

************************

 

You can see attached sample code below.

Mediator_ErrorHandling_JavaCall

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *