Tuesday, March 16, 2010

Steps to Create a Webservice Client Using Weblogic

These steps are performed on weblogic 9.2.
  1. First a working WSDL url is needed. In the file C:\workshop\java\WebServices\Sample_Client\build.xml, modify the related host name and the url so it points to the correct WSDL url.
  2. Open a cmd window. Type C:\bea921\weblogic92\server\bin\setWLSEnv.cmd. This will setup the environment.
  3. type “ant clean”. This will clean up the old folders that may be generated before.
  4. type “ant build-client”. There might be errors. If the error is because of creating the client code itself, then the build file needs to be checked. Otherwise the error will be most likely from compiling the Main.java file. Modify this file and run “ant build-client” again. Repeat this until Main.java is compiled successfully.
  5. type “ant run” to see if the Main.java can be run.
  6. type “ant jar-client” to create the jar file. The jar file is named ws_sample.jar and is in the folder C:\workshop\java\WebServices\Sample_Client\output . This jar file will be put into the application classpath and used by the application.

Notes on the build.xml file.

  1. The target has an attribute packageName. The build.xml file has specified this to be packageName="com.myapp.wsclient.myproj". This is the package that will hold the webservice interface that the client will use to call the webservice.
  2. The webservice has its own request or response custom data types. These java classes are generated in the folder that is specific to the webservice. In this example, these classes are in the folder output\clientclass\.
  3. The Main.java is just a class for testing. Its package name does not matter.

When you run the client to call the webservice, if you want to see the detailed log information such as the actual SOAP message sent to the web service host, you can set the verbose parameter value. In weblogic 10.3, you can either do it on the command line:
-Dweblogic.wsee.verbose=* 
or do it programmatically:
System.setProperty("weblogic.wsee.verbose", "*");

Sample build.xml

<project name="webservices-client_play" default="all"><!-- set global properties for this build -->
 <property name="wls.hostname" value="sample_host" />
 <property name="wls.port" value="80" />
 <property name="example-output" value="output" />
 <property name="clientclass-dir"
  value="${example-output}/clientclass" />
 <path id="client.class.path">
  <pathelement path="${clientclass-dir}" />
  <pathelement path="${java.class.path}" />
 </path>
 <taskdef name="clientgen"
  classname="weblogic.wsee.tools.anttasks.ClientGenTask" />
 <target name="clean">
  <delete dir="${clientclass-dir}" />
 </target>
 <target name="all" depends="clean,build-client,run" />

 <target name="build-client">
  <clientgen
   wsdl="http://${wls.hostname}:${wls.port}/app_path/sample.wsdl"
   destDir="${clientclass-dir}" packageName="com.myapp.wsclient.myproj" 
   type="JAXRPC" />
  <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}"
   includes="**/*.java" />
  <javac srcdir="src" destdir="${clientclass-dir}"
   includes="examples/webservices/client_play/*.java" />
 </target>
 <target name="run">
  <java fork="true"
   classname="examples.webservices.client_play.Main"
   failonerror="true">
   <classpath refid="client.class.path" />
   <arg
    line="http://${wls.hostname}:${wls.port}/app_path/sample.wsdl" />
  </java>
 </target>
 <target name="jar-client">
  <jar destfile="${example-output}/ws_sample.jar"
   basedir="${clientclass-dir}" excludes="**/Main.*" />
 </target>
</project>

Note that in the clientgen target, there is the attribute type. Its default value is JAXRPC. To generate a JAXWS client, you just need to change this value to "JAXWS".

Sample Main.java



package examples.webservices.client_play;

import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import java.util.*;
import examples.webservices.client_play.*;

//import more here

public class Main
{
  public static void main(String[] argsthrows ServiceException,
      RemoteException
  {
    String url = args[0];
    SamplePortTypeService service = new SamplePortTypeService_Impl(url);
    SamplePortType port = service.getSamplePortTypePort();
    System.out.println("port is " + port);
  }
}

No comments:

Post a Comment