Tuesday, April 22, 2008

Creating and invoking WebService in Weblogic Server

Create the following file myRemote, myHome and myEJBClass under c:\myEJBexample .Set the path as c:\Webservicestaging.
com.example.webservices structure will be created under c:\Webservicestaging.
// myRemote.java
package com.example.webservices;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface myRemote extends EJBObject
{

public int mymethod(int i) throws RemoteException;
}

//myHome
package com.example.webservices;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface myHome extends EJBHome
{
myRemote create() throws RemoteException,CreateException;
}

//myEJBClass
package com.example.webservices;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class myEJBClass implements SessionBean
{
public myEJBClass(){}
public int mymethod(int i)
{
return i+5;
}

public void ejbCreate(){}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(SessionContext sc){}
}

Compile the EJB as below
C:\myEJBexample>javac -d c:\staging *.java
Now class file will be generated under com.example.webservices structure in c:\ staging
Create META-INF folder in c:\ staging
Create ejb-jar.xml and weblogic-ejb-jar.xml under META-INF as below
C:\ staging>java weblogic.ant.taskdefs.ejb.DDInit c:\staging
Create myjar.jar under c:\ staging using the below command
C:\staging>jar cvf myjar.jar .
Create build.xml at C:\staging using the below script for creating an EAR file.

project name="buildWebservice" default="ear"
target name="ear"
servicegen
destEar="myear.ear"
contextURI="myServiceTest"
service
ejbJar="myjar.jar"
targetNamespace="http://www.bea.com/webservices/basic/statelesSession"
serviceName="myServiceTest"
serviceURI="/myServiceTest"
generateTypes="True"
expandMethods="True"
style="rpc"
/service
/servicegen
/target
/project

Execute the created build.xml using the below command
C:\staging\ant
Now an EAR file named myear would be created in the folder C:\staging.
Deploy myear in Weblogic server bu following the below said steps,
In the weblogic server console click Applications under Deployments.
Now Applications window will be opened in the right panel.
Click on Deploy an Application in the right panel.
In the Deploy an Application window go to C:\staging folder and select myear.ear file and select continue.
After successful deployment of myear.ear file; two files namely myjar.jar and myServiceTest would be created under myear.ear in the left panel under Applications.
Now click on myServiceTest in the left panel, and click on Testing tab in the right panel.
In the right panel under testing tab click on the link beside Launch Test Page.
In the page opened you can get the following details,
a. WSDL will be opened on click of the link Service Description
b. The method exposed in the WSDL can be tested by clicking the link myMethod.
c. Example code that invokes this service using generated stub will be given. This sample code can be used for writing out client.
d. To generate a client stub for this Web Service the ant task that needs to be used will be given.
Now create a folder named client under c:\staging to place all client related files.
Create build.xml to generate client jar using the ant task mentioned in step 8.d
Run this build.xml from c:\staging\client
Client jar containing supporting stubs will be created.
Extract the created client jar under c:\staging\client
Here is the sample client file for invoking the created the Web Service,create this file in c:\staging\client
import com.myco.myservice.client.MyServiceTest;
import com.myco.myservice.client.MyServiceTest_Impl;
import com.myco.myservice.client.MyServiceTestPort;
import com.myco.myservice.client.MyServiceTestPort_Stub;
public class myClient
{
public static void main(String args[])
{
try{
String wsdlUrl = "http://localhost:7001/myServiceTest/myServiceTest?WSDL";
MyServiceTest service = new MyServiceTest_Impl( wsdlUrl );
MyServiceTestPort port = service.getmyServiceTestPort();
int result = port.mymethod(10);
System.out.println("Result is ******** " + result);
}
catch(Exception e)
{
}
}
}
Now compile this file using the following command,
C:\staging\client>javac -d . *.java
17. Now run the generated class file to get the result.

No comments: