Reviews
Java SOA TrainingAccess WSDL
Access WSDL and Generate Axis Stubs to Access the Web Service Remotely
Now for the really cool stuff. As we discussed earlier, we have set up the ServiceMix as a separate web service gateway in front of the actual web service deployment. Now we have to check whether we can access the WSDL from the ServiceMix. For this, we can point our browser using the standard WSDL query string, like:
http://localhost:8081/services/HelloWebService/?wsdl
or
http://localhost:8081/services/HelloWebService/main.wsdl
Note that, the above URL points to the locationURI attribute configured for the consumer component, which is http://localhost:8081/services/HelloWebService. The WSDL placed in location ch10\ServiceMixHttpBinding\HelloWebService-esb.wsdl, matches the following code:
- <?xml version="1.0" encoding="UTF-8"?>
- <wsdl:definitions
- xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
- xmlns:apachesoap="http://xml.apache.org/xml-soap"
- xmlns:impl="http://AxisEndToEnd.axis.apache.binildas.com"
- xmlns:intf="http://AxisEndToEnd.axis.apache.binildas.com"
- xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow"
- targetNamespace="http://AxisEndToEnd.axis.apache.binildas.com">
- <wsdl:types>
- <schema xmlns="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow"
- elementFormDefault="qualified"
- targetNamespace="http://AxisEndToEnd.axis.
- apache.binildas.com">
- <element name="hello">
- <complexType>
- <sequence>
- <element name="in0" type="xsd:string"/>
- </sequence>
- </complexType>
- </element>
- <element name="helloResponse">
- <complexType>
- <sequence>
- <element name="helloReturn" type="xsd:string"/>
- </sequence>
- </complexType>
- </element>
- </schema>
- </wsdl:types>
- <wsdl:message name="helloRequest">
- <wsdl:part element="impl:hello" name="parameters">
- </wsdl:part>
- </wsdl:message>
- <wsdl:message name="helloResponse">
- <wsdl:part element="impl:helloResponse" name="parameters">
- </wsdl:part>
- </wsdl:message>
- <wsdl:portType name="IHelloWeb">
- <wsdl:operation name="hello">
- <wsdl:input message="impl:helloRequest" name="helloRequest">
- </wsdl:input>
- <wsdl:output message="impl:helloResponse"
- name="helloResponse">
- </wsdl:output>
- </wsdl:operation>
- </wsdl:portType>
- <wsdl:binding name="HelloWebServiceBinding" type="impl:IHelloWeb">
- <wsdlsoap:binding style="document"
- transport="http://schemas.xmlsoap.org/
- soap/http"/>
- <wsdl:operation name="hello">
- <wsdlsoap:operation soapAction=""/>
- <wsdl:input name="helloRequest">
- <wsdlsoap:body use="literal"/>
- </wsdl:input>
- <wsdl:output name="helloResponse">
- <wsdlsoap:body use="literal"/>
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- <wsdl:service name="MyConsumerService">
- <wsdl:port binding="impl:HelloWebServiceBinding"
- name="HelloWebService">
- <wsdlsoap:address location="http://localhost:8081/
- services/HelloWebService/"/>
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
If we compare the two WSDL, the major difference is in the service description section. Here, ServiceMix forms the service and port name taking values from service and endpoint attributes of the consumer service?MyConsumerService and HelloWebService respectively.
If we are able to retrieve the WSDL, the next step is to use the Apache Axis tools to auto-generate the client-side stubs and binding classes, using which we can write simple Java client code to access the service through HTTP channel. The Axis client classes are placed in the directory ch10\ServiceMixHttpBinding\03_AxisClient.
To do that, we have to use the wsdl2java ant task. Let us first declare the task definition and execute that task to generate the stub classes.
- <taskdef name="wsdl2java"
- classname="org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask"
- loaderref="axis" >
- <classpath refid="classpath"/>
- </taskdef>
- <target name="wsdl2java">
- <java classname="org.apache.axis.wsdl.WSDL2Java"
- fork="true"
- failonerror="true">
- <arg value="-o"/>
- <arg value="${src}"/>
- <arg value="-x"/>
- <arg value="http://io.java"/>
- <arg value="http://localhost:8081/services/HelloWebService/
- main.wsdl"/>
- <classpath>
- <path refid="classpath"/>
- <pathelement location="${build}"/>
- </classpath>
- </java>
- </target>
The task will extract the WSDL from the specified location and generate the following client-side artifacts:
com\binildas\apache\axis\AxisEndToEnd\HelloWebServiceBindingStub.java
com\binildas\apache\axis\AxisEndToEnd\IHelloWeb.java
com\binildas\apache\axis\AxisEndToEnd\MyConsumerService.java
com\binildas\apache\axis\AxisEndToEnd\MyConsumerServiceLocator.java
The Client Java class can be written against these generated files as follows:
- public class Client
- {
- private static String wsdlUrl = "http://localhost:8081/services/
- HelloWebService/main.wsdl";
- private static String namespaceURI = "http://AxisEndToEnd.
- axis.apache.binildas.com";
- private static String localPart = "MyConsumerService";
- protected void executeClient(String[] args)throws Exception
- {
- MyConsumerService myConsumerService = null;
- IHelloWeb iHelloWeb = null;
- if(args.length == 3)
- {
- myConsumerService = new MyConsumerServiceLocator(args[0],
- new QName(args[1], args[2]));
- }
- else
- {
- myConsumerService = new MyConsumerServiceLocator(wsdlUrl,
- new QName(namespaceURI, localPart));
- }
- iHelloWeb = myConsumerService.getHelloWebService();
- }
- public static void main(String[] args)throws Exception
- {
- Client client = new Client();
- client.executeClient(args);
- }
- }
To build the entire Axis client codebase, assuming that the ServiceMix is up and running, change directory to ch10\ServiceMixHttpBinding\03_AxisClient, which contains a build.xml file. Execute ant as shown as follows:
cd ch10\ServiceMixHttpBinding\03_AxisClient
ant
This will generate the required Axis client-side stubs and compile the client classes. Now to run the client, execute the following command:
ant run
Summary
We started this chapter by introducing the servicemix-http JBI component. Then we looked at the samples of binding web services to ESB using the servicemix-http binding component. By doing so, we have, in fact, implemented a complete functional web services gateway at the ESB.
A lot of times, we utilize this pattern to expose useful web services hosted deep inside your corporate networks protected by multiple levels of firewall. When we do so, the web services gateway is the access point for any external client. It should mock the actual web service not only in providing the functionality but also in exposing the web services contract (WSDL). Now, do you want to improve the QOS attributes of your web service?
The next chapter will take you through a similar exercise by demonstrating how to access your HTTP-based web services through an MOM channel like JMS.
Java SOA Training







