Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » QAnywhere » Mobile Web Services

Web service connector properties Next Page

Mobile web service example


This example shows you how to create a mobile web service application. The example uses a non-existent web service and so is designed to be read, not run.

For a more full-featured example, see the sample that is installed to samples-dir\QAnywhere\MobileWebServices. (For information about samples-dir, see Samples directory.)

Global Weather web service

Suppose there is a web service called Global Weather. The following WSDL file, called globalweather.wsdl, describes this web service:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:s="http://www.w3.org/2001/XMLSchema"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:tns="http://www.myweather.com"
                  targetNamespace="http://www.myweather.com"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

  <wsdl:types>
    <s:schema targetNamespace="http://www.myweather.com">
      <s:element name="GetWeather">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="CityName" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="CountryName" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetWeatherResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GetWeatherResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>


  <wsdl:message name="GetWeatherSoapIn">
    <wsdl:part name="parameters" element="tns:GetWeather" />
  </wsdl:message>
  <wsdl:message name="GetWeatherSoapOut">
    <wsdl:part name="parameters" element="tns:GetWeatherResponse" />
  </wsdl:message>

  <wsdl:portType name="GlobalWeatherSoap">
    <wsdl:operation name="GetWeather">
      <wsdl:input message="tns:GetWeatherSoapIn" />
      <wsdl:output message="tns:GetWeatherSoapOut" />
    </wsdl:operation>
  </wsdl:portType>


  <wsdl:binding name="GlobalWeatherSoap" type="tns:GlobalWeatherSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <wsdl:operation name="GetWeather">
      <soap:operation soapAction="http://www.myweather.com/GetWeather" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="GlobalWeather">
    <wsdl:port name="GlobalWeatherSoap" binding="tns:GlobalWeatherSoap">
      <soap:address location="http://www.myweather.com/" />
    </wsdl:port>
  </wsdl:service>

</wsdl:definitions>
Generate proxy class

To create a mobile application to access the Global Weather web service, you first run the QAnywhere WSDL compiler. It generates a proxy class that can be used in an application to make requests of the global weather service. In this example, the application is written in Java.

wsdlc -l java globalweather.wsdl

This command generates a proxy class called GlobalWeatherSoap.java, located in the com\myweather directory (relative to the current directory). This proxy class is the service binding class for your application. The following is the content of GlobalWeatherSoap.java:

/*
 * GlobalWeatherSoap.java
 *
 * Generated by the iAnywhere WSDL Compiler
 */

package com.myweather;

import ianywhere.qanywhere.ws.*;
import ianywhere.qanywhere.client.QABinaryMessage;
import ianywhere.qanywhere.client.QAException;

public class GlobalWeatherSoap extends ianywhere.qanywhere.ws.WSBase 
{

    public GlobalWeatherSoap(String iniFile) throws WSException
    {
        super(iniFile);
        init();
    }

    public GlobalWeatherSoap() throws WSException
    {
        init();
    }

    public void init() 
    {

        setServiceName("GlobalWeather");
    }



    public java.lang.String getWeather(java.lang.String cityName, 
                        java.lang.String countryName)  throws QAException,WSException,WSFaultException
    {
        StringBuffer    soapRequest = new StringBuffer();
        QABinaryMessage    qaRequestMsg = null;
        String        responsePartName = "GetWeatherResult";
        java.lang.String    returnValue;

        writeSOAPHeader( soapRequest, "GetWeather", "http://www.myweather.com" );
        soapRequest.append( WSBaseTypeSerializer.serialize("CityName",cityName,"string","http://www.w3.org/2001/XMLSchema",true,true) );
        soapRequest.append( WSBaseTypeSerializer.serialize("CountryName",countryName,"string","http://www.w3.org/2001/XMLSchema",true,true) );
        writeSOAPFooter( soapRequest, "GetWeather" );

        qaRequestMsg = createQAMessage( soapRequest.toString(), "http://www.myweather.com/GetWeather", "GetWeatherResponse" );

        WSResult wsResult = invokeWait( qaRequestMsg );

        returnValue = wsResult.getStringValue(responsePartName);

        return returnValue;
    }


    public WSResult asyncGetWeather(java.lang.String cityName, 
                        java.lang.String countryName)  throws QAException,WSException
    {
        StringBuffer    soapRequest = new StringBuffer();
        QABinaryMessage    qaRequestMsg = null;

        writeSOAPHeader( soapRequest, "GetWeather", "http://www.myweather.com" );
        soapRequest.append( WSBaseTypeSerializer.serialize("CityName",cityName,"string","http://www.w3.org/2001/XMLSchema",true,true) );
        soapRequest.append( WSBaseTypeSerializer.serialize("CountryName",countryName,"string","http://www.w3.org/2001/XMLSchema",true,true) );
        writeSOAPFooter( soapRequest, "GetWeather" );

        qaRequestMsg = createQAMessage( soapRequest.toString(), "http://www.myweather.com/GetWeather", "GetWeatherResponse" );

        WSResult wsResult = invoke( qaRequestMsg );

        return wsResult;
    }

}
Write mobile web service applications

Next, write applications that use the service binding class to make requests of the web service and process the results. Following are two applications, both of which make web service requests offline and process the results at a later time.

The first application, called RequestWeather, makes a request of the global weather service and displays the ID of the request:

import ianywhere.qanywhere.client.*;
import ianywhere.qanywhere.ws.*;
import com.myweather.GlobalWeatherSoap;

class RequestWeather
{
    public static void main( String [] args ) {
    try {
        // QAnywhere initialization
        QAManager mgr = QAManagerFactory.getInstance().createQAManager();
        mgr.open( AcknowledgementMode.EXPLICIT_ACKNOWLEDGEMENT );
        mgr.start();
     
        // Instantiate the web service proxy
        GlobalWeatherSoap service = new GlobalWeatherSoap();
        service.setQAManager( mgr );
        service.setProperty( "WS_CONNECTOR_ADDRESS", "ianywhere.connector.globalweather\\" );
  
   
        // Make a request to get weather for Beijing
        WSResult r = service.asyncGetWeather( "Beijing", "China" );
     
        // Display the request ID so that it can be used by ShowWeather
        System.out.println( "Request ID: " + r.getRequestID() );
     
        // QAnywhere finalization
        mgr.stop();
        mgr.close();
     
    } catch( Exception exc ) {
        System.out.println( exc.getMessage() );
    }
    }
}

The second application, called ShowWeather, shows the weather conditions for a given request ID:

import ianywhere.qanywhere.client.*;
import ianywhere.qanywhere.ws.*;
import com.myweather.GlobalWeatherSoap;

class ShowWeather
{
   public static void main( String [] args ) {
   try {
     // QAnywhere initialization
     QAManager mgr = QAManagerFactory.getInstance().createQAManager();
     mgr.open( AcknowledgementMode.EXPLICIT_ACKNOWLEDGEMENT );
     mgr.start();
     
     // Instantiate the web service proxy
     GlobalWeatherSoap service = new GlobalWeatherSoap();
     service.setQAManager( mgr );
     
     // Get the response for the specified request ID
     WSResult r = service.getResult( args[0] );
     if( r.getStatus() == WSStatus.STATUS_RESULT_AVAILABLE ) {
  System.out.println( "The weather is " + r.getStringValue( "GetWeatherResult" ) );
  r.acknowledge();
     } else {
  System.out.println( "Response not available" );
     }

     
     // QAnywhere finalization
     mgr.stop();
     mgr.close();
     
 } catch( Exception exc ) {
     System.out.println( exc.getMessage() );
 }
    }
}

Compile the application and the service binding class:

javac -classpath ".;%sqlany10%\java\iawsrt.jar;%sqlany10%\java\qaclient.jar" com\myweather\GlobalWeatherSoap.java   RequestWeather.java
javac -classpath ".;%sqlany10%\java\iawsrt.jar;%sqlany10%\java\qaclient.jar" com\myweather\GlobalWeatherSoap.java   ShowWeather.java
Create QAnywhere message stores and start a QAnywhere Agent

Your mobile web service application requires a client message store on each mobile device. It also requires a server message store, but this example uses the QAnywhere sample server message store.

To create a client message store, create a SQL Anywhere database with the dbinit utility and then run the QAnywhere Agent to set it up as a client message store:

dbinit -i qanywhere.db
qaagent -q -si -c "dbf=qanywhere.db"

Start the QAnywhere Agent to connect to your client message store. The following must all be on one command line:

qaagent 
  -c "dbf=qanywhere.db;eng=qanywhere;uid=ml_qa_user;pwd=qanywhere" 
  -policy automatic

Start the MobiLink server. This example uses the QAnywhere sample database as the server message store. The following must all be on one command line:

mlsrv10 
  -m
  -zu+ 
  -c "dsn=QAnywhere 10 Demo;uid=ml_server;pwd=sql;start=dbsrv10
       -xs http(port=8080)"
  -v+
  -ot qanyserv.mls

For more information about these components, see:

Create a web service connector

You must create a web service connector that listens for QAnywhere messages sent to the GetWeather web service, makes web service calls when messages arrive, and sends back responses to the originating client.

Open Sybase Central and connect to your server message store. To create your web service connector, choose File > New Connector. When the Connector wizard appears, choose Web Services. In the wizard pages, you must set the following properties to match the mobile applications you created earlier in the example: