Interstage Application Server SOAPサービス ユーザーズガイド |
目次
索引
![]() ![]() |
第5章 RPC方式のアプリケーションの実装 | > 5.4 スタブ方式によるRPCクライアントアプリケーション |
以下、サンプルプログラムSampleRpcCL1.javaの例を用いて処理を説明します。
import javax.xml.rpc.Service; import javax.xml.rpc.ServiceFactory; import javax.xml.rpc.holders.IntHolder; import javax.xml.soap.SOAPElement; import javax.xml.soap.Detail; import javax.xml.namespace.QName; import javax.xml.transform.Source; import com.fujitsu.interstage.soapx.types.UnsignedInt; import com.fujitsu.interstage.soapx.holders.SourceHolder; import java.util.Iterator; public class SampleRpcCL1 { public static void main( String[] args ) //**(1)** { try { //**(2)** QName serviceQName = new QName( "urn:IntSample", "IntSampleService" ); //**(3)** ServiceFactory factory = ServiceFactory.newInstance(); IntSampleServiceLocator locator = (IntSampleServiceLocator) factory.createService(serviceQName); IntSample target = locator.getIntSamplePort(); //**(4)** int arg1 = 100; UnsignedInt arg2 = new UnsignedInt(200l); IntHolder arg3 = new IntHolder(); SourceHolder arg4 = new SourceHolder(); System.out.println( "arg1 = " + arg1 ); System.out.println( "arg2 = " + arg2.longValue() ); //**(5)** int result = target.opInt( arg1, arg2, arg3, arg4 ); //**(6)** System.out.println( "arg3 = " + arg3.value ); System.out.println( "ret = " + result ); //**(7)** } 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(); } } } |
RPCクライアントアプリケーションでは、実装や継承が必要なクラスはありません。任意クラスの任意メソッドで送信処理を行うことができます。−(1)
リモート呼び出しするRPCサーバアプリケーションを、javax.xml.namespace.QNameオブジェクトを使用して特定します。−(2)
javax.xml.namespace.QNameオブジェクトを作成するためには、WSDLで示された以下の情報が必要です。
リモート呼び出し先のスタブオブジェクトを取得します。−(3)
まず最初にjavax.xml.rpc.ServiceFactoryオブジェクトのcreateServiceメソッドを使用してロケータオブジェクトを取得します。
createServiceメソッドのパラメタには、(2)で用意したjavax.xml.namespace.QNameオブジェクトを指定します。
次にロケータオブジェクトからスタブオブジェクトを取得します。
リモート呼び出しで設定するパラメタを用意します。−(4)
パラメタには、通常の呼び出し元で設定した値が呼び出し先に送られるinパラメタのほかに、outおよびinoutパラメタがあります。outパラメタは、呼び出し元では値を設定せず、呼び出し先で値が設定されて返されるパラメタです。inoutパラメタは呼び出し元で設定した値が呼び出し先に送られるとともに、呼び出し先でその値が更新されて返されるパラメタです。
outおよびinoutのパラメタには、javax.rpc.soap.holdersパッケージ、com.fujitsu.intertstage.soapx.holdersパッケージのHolderクラスを使用します。例ではarg3, arg4がoutパラメタになっています。
使用できるデータ型については“サポートされるデータ型”を参照してください。
RPCサーバアプリケーションのメソッド(オペレーション)を、Javaのメソッド呼び出しのように呼び出します。−(5)
スタブオブジェクトのメソッド(例ではopInt)を呼び出すことで、リモート呼び出しが実行されます。
呼び出すメソッド(オペレーション)名は、WSDLのbinding要素と関連付けられたportType要素中のoperation要素のname属性の値です。
RPCサーバアプリケーションの呼び出し結果を、返り値とout/inoutパラメタはHolderオブジェクトのvalueメンバで取得します。−(6)
RPCクライアントアプリケーションは、以下の例外をcatchする必要があります。−(7)
RPCクライアントアプリケーションでは、これら例外情報を取得できます。例ではFaultの情報を標準出力に出力しています。
目次
索引
![]() ![]() |