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

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

5.4.3 セション管理の利用

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

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

クラス名

メソッド

説明

Stub
implements javax.xml.rpc.Stub

public void
setMaintainSession (boolean maintainsSession)

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

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

■設定方法

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

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

 ●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.Service;
 import javax.xml.rpc.ServiceFactory;
 import javax.xml.namespace.QName;
 import javax.xml.soap.SOAPElement;
 import javax.xml.soap.Detail;
 import com.fujitsu.interstage.soapx.client.Stub;
 import java.util.Iterator;

 public class CountRequestClient
 {
   static final int stopTimes=10;
   static void run( String targetURL )
   {
     try {
       QName serviceQName = new QName(
                  "urn: sample-count-request ", "CountRequestService" );
       ServiceFactory factory = ServiceFactory.newInstance();
       CountRequestServiceLocator locator = (CountRequestServiceLocator)
                  factory.createService(serviceQName);
       CountRequest target = locator.getCountRequestPort();

       // セション情報の保持
       ((Stub)target).setMaintainSession(true);
       System.out.println( "started." );

       for( int times = 1; times <= stopTimes; times++ ){
         boolean succeeded;
         try{
           long rettimes = target.count();
           if( times != rettimes ){
             succeeded = false;
           }
           succeeded = true;
         } catch(SOAPException se) {
           System.out.println("SOAPException throwed.");
           System.out.println(se);
           succeeded = false;
         }

         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();
     }
   }

   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 );
   }
 }


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

All Rights Reserved, Copyright(C) 富士通株式会社 2005