Thursday, May 24, 2007

Webservice using Weblogic

        Webservice is a special service (ie., the service written in any programming language can be accessed from any client apllication written in any programming language) which is available in the web, accessed through some standard protocols.

Webservice Standards :

SOAP(Simple Object Access Protocol) protocal is used as a messaging protocol and HTTP is used as an connectin protocol when the client communicating( ie. transmit the data, service invocation call) the web service.

WSDL(WebService Description Language) is an XML speification used to describe the web service to the client. A WSDL document describes Webservice operations, I/O parameters, and how a client application connects to the Web Service.

JAX-RPC(JAVA API for XML based Remote Procedure Call) is used to invoke a webservice in client application.

UDDI(Universal Description, Discvery, and Integeration) is an universal registry used to register and find the service. It contains the details of the service.

Sample Application

1. Create the component
A component can be EJB component, Java Class, etc.. In this sample application we create a simple java class HelloWorld as component. Here the HelloWorld.java file

HelloWorld.java
package com.webservice.sample;
public class HelloWorld {
        public String sayHello(final String user) {
                return "Hello " + user + "...";
        }
        public long add(final long number1, final long number2) {
                return number1 + number2;
        }
}

2.Make the component as a webservice
To make the above class file as webservice we use the servicegen tool provided by weblogic. To setup the environment to create a webserivce component, we run the setWLSEnv.sh file, which is available in the bea installation folder.

    cmd:\> setWLSEnv
    cmd:\> ant -f build.xml webSerivceEar
After this we will get a deployable component(ws_basic_HelloWorld.ear).

3. Deploy the service
Deploy the .ear component in the weblogic server using the console window. From http://localhost:7001/console, You can access the console window of the bea app server which run on your machine, and deploy the component.

4. Register the service in the registry
Register the service in the UDDI registry. You can register the service and find the registered service, in the URL http://localhost:7001/uddiexplorer. While registering the service you should give the WSDL location (you will get this from the server), to make the service available to others. The WSDL path will be in the form of http://localhost:7001/WebServices<contextURI>/HelloWorld<serviceURI>?WSDL

5. Generate the Client stub to create the client application
By make use of the clientgen tool provided by weblogic, we can create a client stub from the WSDL. The client stub will be created only from the WSDL file, Thats why the WSDL is registered in the UDDI registry.
    cmd:\> ant -f build.xml clientStub

After this we will get a clientStub(clientService.jar).

6. Create sample Client application
We create the clientApplication from the clientStub. Here the client application
SampleClient.java
import java.io.IOException;
import javax.xml.rpc.ServiceException;
import webserapp.client.HelloWorld;
import webserapp.client.HelloWorldPort;
import webserapp.client.HelloWorld_Impl;

class SampleClient {
    public static void main(String[] args) throws IOException, ServiceException {
        String wsdlString = "http://localhost:7001/WebServices/HelloWorld?WSDL";
        HelloWorld service = new HelloWorld_Impl(wsdlString);
        HelloWorldPort port = service.getHelloWprldPort();
        System.out.println(port.sayHello("NAME"));
        System.out.println(port.add(100,200));
    }
}

Compile and run the client SampleClient.

build.xml
<project name="buildWebservice" default="ear">
    <target name="ear">
        <servicegen
            destEar="ws_basic_helloWorld.ear"
            contextURI="WebServices" >
            <service
                javaClassComponents="com.webservice.sample.HelloWorld"
                targetNamespace="http://www.****.com/webservices/basic/helloWorld"
                serviceName="HelloWorld"
                serviceURI="/HelloWorld"
                generateTypes="True"
                expandMethods="True"
                style="rpc" >
            </service>
        </servicegen>
    </target>
    <target name="clientStub">
        <clientgen wsdl="http://localhost:7001/WebSservices/HelloWorld?WSDL"
            packageName="webserapp.client"
            clientJar="clientService.jar">
        </clientgen>
    </target>
</project>

No comments: