Pages

21 November 2012

Using Custom Execution Chain Handlers in Apache Axis2

Execution chain concept is behaving like interceptors in EJB but its structure is a little bit more complex than EJB interceptors. Definition of PhaseRule/Phase/Handler in axis flows(inFlow, outFLow, inFaultFlow, outFaultFlow) enables to execute irrelevant tasks to service business logic such as Security, Transport, WS-* Specifications. Axis2 comes with default handlers and phases. Instead of implementing a new custom phase, users of axis2 overrides AbstractHandler class and alter axis2.xml appropriately (global static configuration) to satisfy the requirements.   

Simplified UML Class Diagram for Axis2 Execution Chain Concept
Phases has handlers and phases' invoke method is called with MessageContext object as parameter by AxisEngine. The above class diagram shows a clear picture of how the handlers and phases related each other.

Handlers are not holding state information whilst manipulating Soap Envelope inside MessageContext. Handlers are easy to develop and integrate into axis2 engine within either a deployed module(.mar) or a jar file under "AXIS_HOME/lib" directory. Below is my sample handler to log incoming soap message. I have made appropriate changes to "AXIS_HOME/conf/axis2.xml" to run my sample handler at "predispatch" phase (before invoking method of any service implementation class in this axis2 engine).


AXIS2.XML
<phaseOrder type="InFlow">
    <!--  System predefined phases       -->
    <phase name="Transport">
        <handler name="RequestURIBasedDispatcher"
                 class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher">
            <order phase="Transport"/>
        </handler>
        <handler name="SOAPActionBasedDispatcher"
                 class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher">
            <order phase="Transport"/>
        </handler>
    </phase>
    <phase name="Addressing">
        <handler name="AddressingBasedDispatcher"
                 class="org.apache.axis2.dispatchers.AddressingBasedDispatcher">
            <order phase="Addressing"/>
        </handler>
    </phase>
    <phase name="Security"/>
    <phase name="PreDispatch">
     <handler name="SampleHandler" class="com.blog.oguzhan.axis2.handler.SampleHandler">
            <order phase="PreDispatch"/>
        </handler>
 </phase>


OUTPUT
H:\Axis2\Download\axis2-1.6.2\bin>axis2server.bat
Using JAVA_HOME    C:\Program Files\Java\jdk1.6.0_29
Using AXIS2_HOME   H:\Axis2\Download\axis2-1.6.2\bin\..
[INFO] [SimpleAxisServer] Starting
[INFO] [SimpleAxisServer] Using the Axis2 RepositoryH:\Axis2\Download\axis2-1.6.2\bin\..\repository
[SimpleAxisServer] Using the Axis2 RepositoryH:\Axis2\Download\axis2-1.6.2\bin\..\repository
[SimpleAxisServer] Using the Axis2 Configuration FileH:\Axis2\Download\axis2-1.6.2\bin\..\conf\axis2.xml
[INFO] Clustering has been disabled
[INFO] Deploying module: addressing-1.6.2 - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/modules/addressing-1.6.2.mar
[INFO] Deploying module: metadataExchange-1.6.2 - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/modules/mex-1.6.2.mar
[INFO] Deploying module: mtompolicy-1.6.2 - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/modules/mtompolicy-1.6.2.mar
[INFO] Deploying module: ping-1.6.2 - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/modules/ping-1.6.2.mar
[INFO] Deploying module: script-1.6.2 - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/modules/scripting-1.6.2.mar
[INFO] Deploying module: soapmonitor-1.6.2 - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/modules/soapmonitor-1.6.2.mar
[INFO] Deploying Web service: version.aar - file:/H:/Axis2/Download/axis2-1.6.2/bin/../repository/services/version.aar
[INFO] Listening on port 8080
[INFO] [SimpleAxisServer] Started
[SimpleAxisServer] Started
[INFO] <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:axis="http://axisversion.sam
ple"><soapenv:Body>
      <axis:getVersion />
   </soapenv:Body></soapenv:Envelope>
[INFO] <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:axis="http://axisversion.sam
ple"><soapenv:Body>
      <axis:getVersion />
   </soapenv:Body></soapenv:Envelope>
[INFO] <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:axis="http://axisversion.sam
ple"><soapenv:Body>
      <axis:getVersion />
   </soapenv:Body></soapenv:Envelope>
[INFO] <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:axis="http://axisversion.sam
ple"><soapenv:Body>
      <axis:getVersion />
   </soapenv:Body></soapenv:Envelope>



Thanks for reading, take care...

No comments:

Post a Comment

Thx for reading! Comments are appreciated...