Monday, December 19, 2011

Web Service with Axis 2

     Check more helpful topics here

Web Service with Axis 2


Advantages of AXIS2 :

  1. AXIS2 provides advantage of hot deployment with .aar / .mar (archive) whereas Axis gives limited facilities for deployments.
  2. AXIS2 woks smoothly with complex message exchanges.
  3. AXIS2 provides easy process to add plugins.
  4. Unlike AXIS for large asynchronous operations AXIS2 responses quickly.
  5. Uses AXIs XML object model deign (AXIOM) to improve both memory use and performance during XML processing.
  6. AXIOM has built-in support for Message Transfer Optimization Mechanism (MTOM).
  7. Supports POJO, Spring, REST,JSON,WSDL 2.0
  8. AXIS2 is based on StAX API, as a result is parses SAX events quicker as compare to AXIS.
  9. It is more flexible than AXIS 1.x , as it has fixed phases and users can also define phases if they want any extension.
  10. Transport independent AXIS2 engine gives more flexibility.

System requirements


Java Development Kit (JDK)
1.5 or later
OS
Windows, Linux, Mac OS X, Fedora core, Ubuntu, Gentoo
    Build Tool
ANT, Maven 2.x

Installation (Standalone)

Download and Install
Download and unpack the Axis2 Standard Binary Distribution into a convenient location. Define an environment variable AXIS2_HOME to the pathname of the extracted directory of Axis2 (Eg: /rtvk/axis2-1.6.1). Linux users can alternatively run the setenv.sh file available in the AXIS2_HOME/bin directory to set the AXIS2_HOME environment variable to the Axis2 classpath.

Start AXIS2 Server
The standalone Axis2 server can be started by executing the following commands:
%AXIS2_HOME%\bin\axis2server.bat(Windows)
$AXIS2_HOME/bin/axis2server.sh (Unix)
After startup, the default web services included with Axis2 will be available by visiting http://localhost:8080/axis2/services/


Installation (in servlet container)


Download and Install
a) Download the axis2.war file or you can build axis2.war using the Source Distribution.
b)Copy the WAR file in the webapps folder of the servlet container. Restart the server if require.
c) Once the WAR is successfully installed, test it by pointing the web browser to the http://<host :port>/axis2. You can ensure that everything installed correctly by clicking ‘validate’ link.
d) Upload services : You can upload .aars on http://<host :port>/axis2/axis-admin/upload.

 

Axis2 scripts / Commands


axis2.{bat/sh}
runs web service clients written using Axis2. 
axis2server.{sh|bat}
Starts a standalone Axis2 server using the AXIS2_HOME/repository as the Axis2 repository and the AXIS2_HOME/conf/axis2.xml as the Axis2 configuration file.
wsdl2java.{bat|sh}
Generates Java code according to a given WSDL file to handle Web service invocations (client-side stubs). This script also has the ability to generate web service skeletons according to the given WSDL.

Usage: wsdl2java.{sh|bat} [OPTION]... -uri <Location of WSDL>

e.g., wsdl2java.sh -uri ../wsdl/Sample.wsdl

java2wsdl.{bat|sh}
Generates the appropriate WSDL file for a given Java class.

Usage: Java2WSDL.{sh|bat} [OPTION]... -cn <fully qualified class name>

e.g., Java2WSDL.sh -cn ../servicesl.ServiceOne




Sample AXIS2 Application:

Creating service


Step1 : Create web project.

Step2: Write service class.

ex.

            package com.rtvk.services;

public class CalculateBMI {
     
public String calculateYourBMI(Float weightInPounds, Float heightInIches) throws Exception{
           
            if(weightInPounds == null || heightInIches == null){
                 
throw new Exception("Please varify input parameters... Mandatory");
            }
Float bmi = (weightInPounds*703)/(heightInIches*heightInIches);
            if(bmi < 18.5){
                  return "Underweight " +bmi;
            }
            if(bmi >= 18.5 && bmi <= 24.9){
                  return "Normal " +bmi;
            }
            if(bmi >= 25 && bmi <= 29.9){
                  return "Overweight " +bmi;
            }
            return "Obese " +bmi;
      }
}

Step3:  (Optional)

Execute java2wsdl command. Here
%AXIS2_HOME%\bin\java2wsdl -cp . -cn com.rtvk.services.CalculateBMI -of CalculateBMI.wsdl

Will generate CalculateBMI.wsdl file.

Step4: Write service.xml in META-INF

<service name="BMICalculator" >
      <Description>
            Please Type your service description here
      </Description>
      <messageReceivers>
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
            <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
      </messageReceivers>
      <parameter name="CalculateBMIService" locked="false">com.rtvk.services.CalculateBMI</parameter>
</service>

Step5: Generate .aar
Below is sample build.xml, using this ‘ant’ command will generate .aar file in lib folder at the level of ‘classes’ folder.

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements. See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership. The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License. You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied. See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
  -->
<project name="BMICalculator" basedir="." default="generate.service">
    <property environment="env"/>
    <property name="AXIS2_HOME" value="${env.AXIS2_HOME}"/>
    <property name="TOMCAT_DEPLOY_DIR" value="${SERVER_HOME}"/>
    <property name="build.dir" value="build"/>
    <path id="axis2.classpath">
        <fileset dir="${AXIS2_HOME}/lib">
            <include name="*.jar"/>
        </fileset>
    </path>
    <target name="compile.service">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.dir}/classes"/>
        <!--First let's compile the classes-->
        <javac debug="on" fork="true" destdir="${build.dir}/classes" srcdir="${basedir}/src" classpathref="axis2.classpath"/>
    </target>
    <target name="generate.wsdl" depends="compile.service">
        <taskdef name="java2wsdl" classname="org.apache.ws.java2wsdl.Java2WSDLTask" classpathref="axis2.classpath"/>
        <java2wsdl className="com.rtvk.services.CalculateBMI" outputLocation="${build.dir}" targetNamespace="http://com.rtvk.services/" schemaTargetNamespace="http://com.rtvk.services/xsd">
            <classpath>
                <pathelement path="${axis2.classpath}"/>
                <pathelement location="${build.dir}/classes"/>
            </classpath>
        </java2wsdl>
    </target>
    <target name="generate.service" depends="compile.service">
        <!--aar them up -->
        <copy toDir="${build.dir}/classes" failonerror="false">
            <fileset dir="${basedir}/resources">
                <include name="**/*.xml"/>
            </fileset>
        </copy>
        <jar destfile="${build.dir}/BMICalculator.aar">
            <fileset excludes="**/Test.class" dir="${build.dir}/classes"/>
        </jar>
        <copy file="${build.dir}/BMICalculator.aar" toDir="${TOMCAT_DEPLOY_DIR}" overwrite="yes"/>
    </target>
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>
</project>

Step6. (Only if you removed corresponding tag from build.xml ) Copy .ar file to ‘webapps\axis2\WEB-INF\services’ folder of container.

Step7: Start server

Step8: You can check deployed service @

You can check wsdl file by entering http://localhost:8080/axis2/services/BMICalculator?wsdl

Creating Client


Command

%AXIS2_HOME%\bin\WSDL2Java -uri CalculateBMI.wsdl -p clients -d adb –s


will create stub to be used by client.


No comments: