例: WS-Notification コンシューマーのサブスクリプション
提供されているコード例の抜粋に基づいて、パブリッシャー登録ロールを果たす JAX-RPC クライアントが、ブローカーにパブリッシャー (プロデューサー) アプリケーションを登録するためのコードを作成する場合は、このタスクを使用します。
このタスクについて
この例は 、Java™ API for XML-based remote procedure calls (JAX-RPC) API と、WSDL2Java ツール (WS-Notification サービス・ポイントの作成の結果として生成された Notification Broker WSDL に対し実行) および WebSphere® Application Server API および SPI を使用して生成されたコードと併せた使用を基にしています。
- JAX-WS
- JAX-WS クライアント・プログラミング・モデル
- 静的 JAX-WS Web サービス・クライアントの実装
- Writing JAX-WS applications for WS-Notification
- Web services hints and tips: JAX-RPC versus JAX-WS, Part 1
この例の最初のオプションのコード・ブロックは、ロー・サブスクリプション の作成方法を示しています。 この概念は、Web サービスの基本通知仕様のセクション 4.2 で定義されます。
通常のケースでは、ラップされたサブスクリプションによって、一致するイベント通知が使用可能になったときに NotificationConsumer の「通知」操作が駆動されます。 サブスクライバーがロー・サブスクリプションを作成する場合は、イベント通知のアプリケーション固有のコンテンツ (つまり、NotificationMessage エレメントのコンテンツ) のみがターゲット・コンシューマー・エンドポイントへ送信されます。 UseRaw (つまり、ロー・サブスクリプション要求) が指定されたサブスクライブ要求の ConsumerReference 内で指定された Web サービス・エンドポイントは、NotificationConsumer ポート・タイプを実装する必要がありません。「通知」操作がイベント通知の配信に使用されないためです。
このことは、コンシューマーは特定のトピックでパブリッシュされるアプリケーション・コンテンツのタイプごとに、操作を受け入れることができる必要があることを意味します。 このパターンによって、WS-Notification が、WS-Notification 認識ではないが、同じ情報を受信したい既存の Web サービス・アプリケーションのグループを起動することが可能になります。
JAX-WS はアクション・ベースのディスパッチをサポートし、JAX-WS ロー・コンシューマー・アプリケーションは、http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer/Notify アクション URI を受け入れる必要があります。詳しくは、トラブルシューティングのヒントであるブローカー経由の通知のロー・コンシューマーである JAX-WS アプリケーションは、通知ブローカー SOAP アクションを認識しなければならないを参照してください。
パブリッシャー登録ロールを果たす JAX-RPC クライアントが、ブローカーにパブリッシャー (プロデューサー) アプリケーションを登録するためのコードを作成するには、以下のステップを実行します (詳しくは、コード例の抜粋を参照)。
手順
例
// 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);
// Create the ConsumerReference. This contains the address of the consumer Web service that is being
// subscribed, or alternatively is a reference to a pull point (see alternative below). Specifying a
// pull point EPR indicates that the consumer is to use pull-based notifications.
EndpointReference consumerEPR =
EndpointReferenceManager.createEndpointReference(new URI("http://myserver.mycom.com:9080/Consumer"));
/*
// Alternative ConsumerReference for pull-based notifications:
EndpointReference consumerEPR = pullPointEPR;
*/
// Create the Filter. This provides the name of the topic to which you want to subscribe the consumer
Filter filter = new Filter();
// Create a topic expression and add it to the filter. The prefixMappings are mappings between namespace
// prefixes and their corresponding namespaces for prefixes used in the expression
Map prefixMappings = new HashMap();
prefixMappings.put("abc", "uri:example");
TopicExpression exp =
new TopicExpression(TopicExpression.SIMPLE_TOPIC_EXPRESSION, "abc:ExampleTopic", prefixMappings);
filter.addTopicExpression(exp);
//Create an XPath 1.0 message content filter
//This example selects a subset of the available messages in the topic, based upon salary level
String filterExpression = "/company/department/employee/salary > 10000";
URI xpathURI = new URI(http://www.w3.org/TR/1999/REC-xpath-19991116);
QueryExpression qexp =
new QueryExpression(xpathURI, filterExpression);
filter.addMessageContentExpression(qexp);
// Create the InitialTerminationTime. This is the time when you want the subscription to terminate.
// For example, set a time of 1 year in the future.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
AbsoluteOrRelativeTime initialTerminationTime = new AbsoluteOrRelativeTime(cal);
// Create the Policy information
SOAPElement[] policyElements = null;
/*
Optional
--------
The following lines show how to construct a policy indicating that the consumer is to
receive raw style notifications:
javax.xml.soap.SOAPFactory soapFactory = javax.xml.soap.SOAPFactory.newInstance();
SOAPElement useRawElement = null;
if (soapFactory instanceof IBMSOAPFactory) {
// You can use the value add methods provided by the IBMSOAPFactory API to create the SOAPElement
// from an XML string.
String useRawElementXML = "<mno:UseRaw xmlns:mno=¥"http://docs.oasis-open.org/wsn/b-2¥"/>";
useRawElement = ((IBMSOAPFactory) soapFactory).createElementFromXMLString(useRawElementXML);
} else {
useRawElement = soapFactory.createElement("UseRaw", "mno", "http://docs.oasis-open.org/wsn/b-2");
}
policyElements = new SOAPElement[] { useRawElement };
*/
// Create holders to hold the multiple values returned from the broker:
// The subscription reference
EndpointReferenceTypeHolder subscriptionRefHolder = new EndpointReferenceTypeHolder();
// The current time at the broker
CalendarHolder currentTimeHolder = new CalendarHolder();
// The termination time for the subscription
CalendarHolder terminationTimeHolder = new CalendarHolder();
// Any additional elements
AnyArrayHolder anyOtherElements = new AnyArrayHolder();
// Invoke the Subscribe operation by calling the associated method on the stub
stub.subscribe(consumerEPR,
filter,
initialTerminationTime,
policyElements,
anyOtherElements,
subscriptionRefHolder,
currentTimeHolder,
terminationTimeHolder);
// Get the returned values:
// An endpoint reference for the subscription that has been created. It is required for
// subsequent lifetime management of the subscription, for example pausing the subscription
com.ibm.websphere.wsaddressing.EndpointReference subscriptionRef = subscriptionRefHolder.value;
// The current time at the broker
Calendar currentTime = currentTimeHolder.value;
// The termination time of the subscription
Calendar terminationTime = terminationTimeHolder.value;
// Any other information
SOAPElement[] otherElements = anyOtherElements.value;