例: JAX-WS Web サービス・アドレッシング API を使用して汎用 Web サービス・リソース・インスタンスにアクセスする Web サービスの作成

Web サービスを使用して管理する必要があるネットワーク・プリンターを所有する IT 組織について検討します。この組織では、各プリンターを、 エンドポイント参照によって指定するリソースとしています。 この例では、WebSphere® Application Server が提供する JAX-WS Web サービス・アドレッシング (WS-Addressing) アプリケーション・ プログラミング・インターフェース (API) を使用して、このようなサービスをコーディングする方法を示します。

エンドポイント参照をターゲット・サービスに戻す Web サービス・インターフェースの提供

この IT 組織では、CreatePrinter portType エレメントを提供する PrinterFactory サービスを実装します。 この portType エレメントは、CreatePrinterRequest メッセージを受け入れ、論理プリンターを示すリソースを作成し、 そのリソースへの参照となるエンドポイント参照をつけて応答します。

このような PrinterFactory サービスの WSDL 定義には、以下のコードが含まれています。
<wsdl:definitions targetNamespace="http://example.org/printer" ...
                  xmlns:pr="http://example.org/printer">
  <wsdl:types>
    ...
    <xsd:schema...>
      <xsd:element name="CreatePrinterRequest"/>
      <xsd:element name="CreatePrinterResponse" 
                   type="wsa:EndpointReferenceType"/>
    </xsd:schema>   </wsdl:types>
  <wsdl:message name="CreatePrinterRequest">
    <wsdl:part name="CreatePrinterRequest" 
               element="pr:CreatePrinterRequest" />
  </wsdl:message>
  <wsdl:message name="CreatePrinterResponse">
    <wsdl:part name="CreatePrinterResponse" 
               element="pr:CreatePrinterResponse" />
  </wsdl:message>
  <wsdl:portType name="CreatePrinter">
    <wsdl:operation name="createPrinter">
      <wsdl:input name="CreatePrinterRequest" 
                  message="pr:CreatePrinterRequest" />
      <wsdl:output name="CreatePrinterResponse"
                  message="pr:CreatePrinterResponse" />
    </wsdl:operation>
  </wsdl:portType>
</wsdl:definitions>

上記の例の CreatePrinter オペレーションは、 新規に作成されたプリンター・リソースを 示す wsa:EndpointReference オブジェクトを戻します。 クライアントは、このエンドポイント参照を使用して、プリンターを示すサービス・インスタンスにメッセージを送信します。

Web サービス・インターフェースの実装

以下の例にある createPrinter メソッドは、 個々のプリンター・リソース・インスタンスの ID を入手します。 オペレーションは、その後、Printer サービスへのエンドポイント参照を作成し、 プリンター ID をエンドポイント参照に関連付けます。 最後に、createPrinter メソッドは、エンドポイント参照を戻します。

import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;

import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MyClass {

  // Create the printer
  ...

  // Define the printer resource ID as a static constant as it is required in later steps
    public static final QName PRINTER_SERVICE_QNAME = new QName("example.printer.com", "printer", "..."); 
    public static final QName PRINTER_ENDPOINT_NAME = new QName("example.printer.com", "PrinterService", "...");
    
    public W3CEndpointReference createPrinter(java.lang.Object createPrinterRequest)
    {
     Document document = ...;

     // Create or lookup the stateful resource and derive a resource
     // identifier string.
     String resource_identifier = "...";
     
     // Associate this resource identifier with the EndpointReference as  
     // a reference parameter.
     // The choice of name is arbitrary, but should be unique     
     // to the service.
     Element element = document.createElementNS("example.printersample",
             "IBM_WSRF_PRINTERID");
     element.appendChild( document.createTextNode(resource_identifier) );
     ...
     
     // Create an EndpointReference that targets the appropriate WebService URI and port name.
     // Alternatively, the getEndpointReference() method of the MessageContext can be used.
     W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
     builder.serviceName(PRINTER_SERVICE_QNAME);
     builder.endpointName(PRINTER_ENDPOINT_NAME);
     builder.referenceParameter(element);
     
     // The endpoint reference now targets the resource rather than the service.
     return builder.build();
    }
}

着信メッセージを Web サービス・リソース・インスタンスとマッチングするためのターゲット・サービスの拡張

これまでに説明した Web サービス実装によって、プリンター・リソース・インスタンスには、そのエンドポイント参照で固有の ID が組み込まれます。この ID は、Web サービスをターゲットとする、後続のメッセージの SOAP ヘッダーで、参照パラメーターになります。また、Web サービスが着信メッセージを適切なプリンターとマッチングさせるために、この ID を使用できます。

Web サービスが WS-Addressing メッセージ・アドレッシング・プロパティーを含むメッセージを受信すると、WebSphere Application Server は、メッセージがアプリケーション・エンドポイントにディスパッチされる前にこれらのプロパティーを処理し、スレッド上のメッセージ・コンテキストにそのプロパティーを設定します。以下の例で説明されているとおり、 Printer Web サービス・アプリケーションは、WebServiceContext オブジェクトの ターゲット・エンドポイントに関連付けられている、 参照パラメーターにアクセスします。

@Resource
private WebServiceContext context;
...
List list = (List) context.getMessageContext().get(MessageContext.REFERENCE_PARAMETERS);

アプリケーションで WS-Addressing 仕様の 2004/08 バージョンを使用する場合は、 以下の例に示すように、IBM 専有の API を使用してメッセージ・パラメーターを取得します。

import com.ibm.websphere.wsaddressing.EndpointReferenceManager;
...
 // Initialize the reference parameter name
 QName name = new QName(..);
 // Extract the String value.
 String resource_identifier = 
        EndpointReferenceManager.getReferenceParameterFromMessageContext(PRINTER_ID_PARAM_QNAME);

Web サービス実装は、プリンター ID に基づいて、メッセージを適切なプリンター・インスタンスに転送できます。

エンドポイント参照を使用したメッセージのエンドポイントへの送信

クライアントは、以下の例に示すように、サービスから返されたエンドポイント参照を使用して、 プリンターの JAX-WS プロキシーを作成します。
javax.xml.ws.Service jaxwsServiceObject= ...;
W3CEndpointReference epr = ...;
...
Printer myPrinterProxy = jaxwsServiceObject.getPort(epr, Printer.class, new AddressingFeature());
これで、プロキシー・オブジェクトは、新規プリンター・リソース・インスタンスを表すようになります。 クライアントはこのオブジェクトを使用して、Printer Web サービス経由でメッセージをプリンターに送信することができます。 クライアントがサービスを起動する際に、WebSphere Application Server は、 該当するメッセージ・アドレッシング・プロパティーをメッセージ・ヘッダーに追加します。 この場合、このプロパティーが、ターゲット・プリンター・リソースを識別するエンドポイント参照に含まれる 参照パラメーターになります。

クライアント側から見ると、エンドポイント参照は不透明です。 クライアントは、いずれのエンドポイント参照パラメーターの内容も解釈できず、 完全に使用できません。 クライアントは、エンドポイント参照のインスタンスを直接作成できません。これは、参照パラメーターが、サービス・プロバイダー専用になっているためです。クライアントは、サービス・プロバイダーからエンドポイント参照を取得し (例えば、プロバイダー・ファクトリー・サービスを通じて)、そのエンドポイント参照を使用して、エンドポイント参照によって表されているエンドポイントに、Web サービス・オペレーションを送信する必要があります。


トピックのタイプを示すアイコン 参照トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=xwbs_wsa_jaxws
ファイル名:xwbs_wsa_jaxws.html