例: WS-Notification pull ポイントからのメッセージの取得
このタスクは、示されているコード例の抜粋に基づいて、pull スタイルのコンシューマー・ロールを果たす JAX-RPC クライアントが、pull ポイントからメッセージを要求するコードを作成する場合に使用します。
このタスクについて
この例は 、Java™ API for XML-based remote procedure calls (JAX-RPC) API と、WSDL2Java ツール (WS-Notification サービス・ポイントの作成の結果として生成された Notification Broker WSDL に対し実行) および WebSphere® Application Server API および SPI を使用して生成されたコードと併せた使用を基にしています。
WebSphere Application Server では、WS-Notification サービスにはバージョン 6.1 およびバージョン 7.0 の 2 つの実装があります。この JAX-RPC 例は、バージョン 6.1 またはバージョン 7.0 WS-Notification サービス・ポイントと正常にやり取りすることができます。 ただし、WS-Notification をポリシー・セットと一緒に使用したい場合は (例えば、WS-ReliableMessaging による構成を可能にするため)、Java API for XML-based Web Services (JAX-WS) プログラミング・モデルを使用するように WS-Notification アプリケーションをエンコードする必要があります。なおかつ WS-Notification アプリケーションはバージョン 7.0 WS-Notification サービス・ポイントとやり取りする必要があります。 JAX-WS クライアント・アプリケーションをプログラミングした経験がない場合は、以下のトピックを参照してください。
pull スタイルのコンシューマー・ロールを果たす JAX-RPC クライアントが、pull ポイントからメッセージを要求するコードを作成するには、以下のステップを実行します (詳しくは、コード例の抜粋を参照)。
手順
- JAX-RPC サービスを検索します。 JNDI 名は、Web サービス・クライアントの実装に固有のものです。
- 操作の呼び出しを行うポートのスタブを取得します。
- 要求を pull ポイントに関連付けます。 pullPointEPR は、CreatePullPoint 操作を呼び出して返された EndpointReference です。
- 取得するメッセージの数を指定します。
- オプションの情報があれば、作成します。
- 要求情報を作成します。
- スタブで関連付けられているメソッドを呼び出して、GetMessages 操作を呼び出します。
- 応答から返されたメッセージを取得します。
例
以下は、pull スタイルのコンシューマー・ロールを果たす JAX-RPC クライアントが、pull ポイントからメッセージを要求するコード例を示しています。
// Look up the JAX-RPC service. The JNDI name is specific to your web services client implementation
InitialContext context = new InitialContext();
javax.xml.rpc.Service service = (javax.xml.rpc.Service) context.lookup(
"java:comp/env/services/NotificationBroker");
// Get a stub for the port on which you want to invoke operations
NotificationBroker stub = (NotificationBroker) service.getPort(NotificationBroker.class);
// Associate the request with a pull point. The pullPointEPR is the EndpointReference returned
// from invoking the CreatePullPoint operation
((Stub) stub)._setProperty(WSAConstants.WSADDRESSING_DESTINATION_EPR, pullPointEPR);
// Specify the number of messages you want to retrieve
Integer numberOfMessages = new Integer(2);
// Create any optional information
SOAPElement[] optionalInformation = new SOAPElement[] {};
// Create the request information
GetMessages request = new GetMessages(numberOfMessages, optionalInformation);
// Invoke the GetMessages operation by calling the associated method on the stub
GetMessagesResponse response = stub.getMessages(request);
// Get the messages returned from the response
NotificationMessage[] messages = response.getMessages();