Tuesday, 16 August 2011

custom faults in SOA11g


In this post, I am going to discuss how custom faults can be created and handled in Oracle SOA Suite 11g.Faults in SOA suite can be broadly categorized into following categories:-
                                   
                                             System Faults
                                             Custom Faults
                                             Mediator Faults

So lets get started.First create a new SOA application and a new SOA project.Choose empty composite template.Next open the composite file and drag and drop a new BPEL process.Enter FaultThrower as the name,choose synchronous template and accept the default input and output.Make sure the "Expose as SOAP service" is checked. Hit Ok once your done.
                                                   


Your composite should look like the following image:-



Now we shall create a message on which our fault variable is going to be based on.I should mention here that fault variables in Oracle SOA Suite 11g must be WSDL message based.
To do that,first create a new XML Schema Document.I used the name Faults.xsd.You can choose any name you like.Following is the source for Faults.xsd:-


<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://www.customfaults.org"
            targetNamespace="http://www.customfaults.org"
            elementFormDefault="qualified">
 
  <xsd:element name="MyBusinessFault" type="tns:MyBusinessFaultType"/>
 
  <xsd:complexType name="MyBusinessFaultType">
 
    <xsd:sequence>
      <xsd:element name="FaultCode" type="xsd:string"/>
      <xsd:element name="FaultDesciption" type="xsd:string"/>
    </xsd:sequence>
 
  </xsd:complexType>
 
 
</xsd:schema>

Next, open the FaultThrower.wsdl file.First import the schema Faults.xsd into your wsdl:

    <import namespace="http://www.customfaults.org" schemaLocation="xsd/Faults.xsd"/>



Next add a namespace declaration:-

                         xmlns:tns="http://www.customfaults.org"


Now add the following WSDL message:-

   <wsdl:message name="MyCustomFaultMessage">
    <wsdl:part name="businessFault" type="tns:MyBusinessFaultType"/>
  </wsdl:message>



Next, we are going to specify a fault in the operation section for our service.To do this, add the follwing line of code in the operation section:-

                  <wsdl:fault message="client:MyCustomFaultMessage" name="MyBusinessFault"/>


So we are done with WSDL file.Next open the FaultThrowerBPEL process by double clicking on it.Now, we shall create a fault variable based on our WSDL message type.To do this, click on the create new variable icon in the BPEL process as shown in the image below:-


The create new variable dialog opens.Click on the green plus icon to add a new variable.


Enter the name FaultVar(or any name you wish).Select Message Type radio button and click on the magnifying glass as shown in the image below:-


In the type chooser dialog, select the fault message that you created.


Hit Ok three times to complete the variable creation.In this step, I created a variable manually.In the subsequent steps, we are going to see how JDev can automatically create the fault variable based on our message type.Next drag and drop a throw activity onto your BPEL process , between the Receive and Reply activities.Double click on the throw activity.On the window that appears, click on the magnifying glass as shown in the image below to select the fault that you want to throw:-


Select the fault that you created.Take a moment to recall that the fault name that is being displayed i.e. MyBusinessFault was specified in the name attribute while creating the fault inside the WSDL file's operation section.
     <wsdl:fault message="client:MyCustomFaultMessage" name="MyBusinessFault"/>






Next drag an Assign activity onto the BPEL process just before the throw activity.The BPEL process should look like the following image:-




Double click on the Assign activity.Add a new copy operation.




Create a copy operation as shown in the following image.Hit Ok once you are done.


Create another copy operation as shown in the following image.Hit Ok twice to return to the BPEL process.


This completes the assign activity.Next add a catch branch by licking on the add catch branch icon on the BPEL process as shown in the following image:-


A catch branch is added to the BPEL process.Next, double click on the catch activity to specify the fault that we intend to catch.On the window that comes up, click on the magnifying glass as shown in the image below to choose the fault.


In the fault chooser dialog, select the fault that we created i.e. MyBusinessFault.Hit Ok once you are done.


Now, we could use the same fault variable that we created earlier but for now, we are going to create a new fault variable.To do that,click on the green plus icon as shown in the image below.


The variable creation dialog appears.Enter a name.To follow along, enter the name MyFaultVar.Notice that in the type section, JDev has automatically selected the fault message that we created.


Hit Ok once you are done.Now drag and drop a Reply activity under the catch branch.Your BPEL process should look like the following image:-



Now double click on the Reply activity.In the dialog that opens, click on the magnifying glass to select the partner link.


In the partner chooser dialog, select the faultthrower_client partner link.


Next, click on the magnifying glass to select the variable.


Select the newly created fault variable MyFaultVar.hit Ok once you are done.


Now click on the magnifying glass to choose the fault that we are returning.


In the fault chooser dialog, select the fault MyBusinessFault.Hit Ok twice to return to the BPEL process.




The reply activity is now configured to return a fault.This completes our BPEL process.You can now deploy and test the BPEL process.When tested, a fault is going to be returned by the process which is the custom fault that we created.

In fact, we can use BPEL throw activity to throw any custom fault that we like.For example,while throwing a fault, we can choose any arbitrary namespace URI and  local part.





 We can also catch the fault using a catch activity.In this case, the URI and local part must be same as the fult that was thrown.



We can use such throw activities in a similar way use break statements in C.However, if we want to associate a fault variable with the Throw activity, the fault variable must be a WSDL message based.Hope this helps.

No comments:

Post a Comment