Interstage Application Server SOAPサービス ユーザーズガイド
目次 索引 前ページ次ページ

第5章 RPC方式のアプリケーションの実装> 5.4 DII方式によるRPCクライアントアプリケーション

5.4.4 セション管理の利用

 セション管理を行う場合は、javax.xml.rpc.Callオブジェクトをcom.fujitsu.interstage.soapx.client.Callクラスにキャストして、以下のメソッドを使用してセション情報を保持することで、セションを利用できます。

●パッケージ名:com.fujitsu.interstage.soapx.client

クラス名

メソッド

説明

Call
implements javax.xml.rpc.Call

public void
setMaintainSession (boolean maintainsSession)

HTTPセション継続を行うかどうかを設定します。

 RPCクライアントアプリケーション終了時、セション管理情報を含むcookieは保存されません。
 セション管理を維持できる期間は、RPCクライアントアプリケーションの起動期間です。

■設定方法

 com.fujitsu.interstage.soapx.client.Callクラスのインスタンスに、setMaintainSessionメソッドをパラメタに"true"を指定して呼び出します。これによりこのcom.fujitsu.interstage.soapx.client.Callクラスのインスタンスはセション管理の情報を保持するように設定されます。
 セション管理の情報を保持するように設定されたcom.fujitsu.interstage.soapx.client.Callクラスのインスタンスを使用してRPCサーバアプリケーションを呼び出すことで、セションを継続できます。
 com.fujitsu.interstage.soapx.client.Call.setMaintainSessionメソッドを、パラメタに"false"を指定して呼び出した場合、このcom.fujitsu.interstage.soapx.client.Callクラスのインスタンスはセション管理の情報を破棄し、セションは継続されなくなります。

 
 セション管理を利用して、RPCクライアントアプリケーションからRPCサーバアプリケーションを呼び出した回数の値(calltimes)と、SOAPサーバアプリケーションから呼び出された回数として受け取った値(resp)を比較します。

 ●RPCサーバアプリケーション

 //
 //RPCサーバアプリケーション
 //
 import java.rmi.RemoteException;
 /**
 * <code>CountRequestServer</code>
 * Deploy this with 'Session' scope
 */
 public class CountRequestServer implements CountIf
 {
  private long n= 0;
  /*
  * return the number of requested times in the session.
  * @return number of the requested times in the session.
  */
  public long count() throws RemoteException
  {
   this.n++;
   return this.n;
  }
 }

 

 ●RPCクライアントアプリケーション

 //
 //RPCクライアントアプリケーション
 //
 import javax.xml.rpc.Call;
 import javax.xml.rpc.Service;
 import javax.xml.rpc.ServiceFactory;
 import javax.xml.namespace.QName;
 import java.util.Map;

 public class CountRequestClient
 {
  static final String targetOPName = "count";
  static final String targetURN = "urn:sample-countRequest";
  static final int stopTimes=10;
  static final QName datatype = new QName("http://www.w3.org/2001/XMLSchema", "long");

  static void run( String targetURL )
  {
   try {
    Service service = ServiceFactory.newInstance().createService(
        new QName( "urn:sample-countRequest", "count" ));
    javax.xml.rpc.Call call = service.createCall();

    call.setTargetEndpointAddress(targetURL);
    call.setOperationName( new QName( targetURN, targetOPName ));
    call.setReturnType(datatype);

    // セション情報の保持
    ((com.fujitsu.interstage.soapx.client.Call)call).setMaintainSession(true);

    Object[] param = new Object[] {};
    System.out.println( "started." );
    for( int times = 1; times <= stopTimes; times++ ){
     boolean succeeded;
     Long resp = (Long)call.invoke( param );
     succeeded = proccessResponse( resp, times);

     if( !succeeded ){
      System.out.println( "Call failed ! called "+ times +
         " times. (last one may be trying only)" );
      break;
     }
    }
    System.out.println( "ended." );

   } catch( javax.xml.rpc.ServiceException e ) {
    e.printStackTrace();
    if( e.getLinkedCause() != null ) {
     e.getLinkedCause().printStackTrace();
    }
   } catch( javax.xml.rpc.soap.SOAPFaultException e ) {
    e.printStackTrace();
    System.out.println(" Code = " + e.getFaultCode());
    System.out.println(" String = " + e.getFaultString());
    System.out.println(" Actor = " + e.getFaultActor());
    if( e.getDetail() != null ) {
     Detail detail = e.getDetail();
     Iterator it = detail.getDetailEntries();
     if( it != null ) {
      while(it.hasNext()) {
       SOAPElement elm = (SOAPElement)it.next();
       System.out.println(" Detail = " + elm);
      }
     }
    }
   } catch( javax.xml.rpc.JAXRPCException e ) {
    e.printStackTrace();
    if( e.getLinkedCause() != null ) {
     e.getLinkedCause().printStackTrace();
    }
   } catch( java.rmi.RemoteException e ) {
    e.printStackTrace();
    if( e.detail != null ) {
     e.detail.printStackTrace();
    }
   } catch( Throwable e ) {
    e.printStackTrace();
   }
  }

  static boolean proccessResponse( Long resp, int calltimes ){
  // check the response
   if( resp == null ){
    System.out.println( "-------- the call failed --------" );
    return false;
   } else {
    if( resp.longValue() == calltimes ){
     System.out.println( " requested " + resp + " times." );
     return true;
    } else {
     System.out.println( "ERROR:Server called counter value("+resp+") " +
     "differs from really called times(" +calltimes+")!");
     return false;
    }
   }
  }

  public static void main(String[] args) throws Exception{
   if(args.length<1){
    System.out.println("Usage: java CountRequestClient <RPC router's URL>");
    System.exit(1);
   }
   String targetURL = args[0];
   run( targetURL );
  }
 }

◆Webサービス情報の設定

 Webサービス情報編集ツールのWebサービス情報入力画面で、“Webサービスアプリのライフサイクル”として"session"を選択します。
 詳細については“Webサービス情報の管理”を参照してください。

◆セションタイムアウトの設定

 セション管理のタイムアウト時間(分単位)は、Webアプリケーション環境定義(web.xml)ファイルのsession-configタグで、セションパラメタとしてタイムアウト時間をsession-timeoutタグで指定します。
 詳細については“Webサービス実行環境の環境構築”の“Webアプリケーション”および“J2EE ユーザーズガイド”の“Servlet/JSP編”を参照してください。


目次 索引 前ページ次ページ

Copyright 2003 FUJITSU LIMITED