Exemple : inscription d'un destinataire WS-Notification
Cette tâche vous permet d'écrire le code pour un client JAX-RPC dans le rôle de l'enregistrement du diffuseur de publications, pour enregistrer un diffuseur de publications avec un courtier, sur la base du code fourni à titre d'exemple.
Pourquoi et quand exécuter cette tâche
Cet exemple repose sur l'utilisation des API JAX-RPC (Java™ API for XML-based remote procedure calls) avec le code généré en utilisant l'outil WSDL2Java (exécuté sur le WSDL Notification Broker généré par la création du point de service WS-Notification) et les API et SPI WebSphere Application Server.
- JAX-WS
- Modèle de programmation client JAX-WS
- Implémentation de clients de services Web JAX-WS statiques
- Ecriture d'applications JAX-WS pour WS-Notification
- Astuces et conseils sur les services Web : JAX-RPC versus JAX-WS, Partie 1
Dans cet exemple, le premier bloc de code facultatif indique comment créer un abonnement brut. Ce concept est défini dans la section 4.2 de la spécification Web Services Base Notification.
En temps normal, un abonnement encapsulé provoque l'exécution de l'opération Notify du destinataire de notification lorsque des notifications d'événements deviennent disponibles. Si, au lieu de cela, l'abonné crée un abonnement brut, alors seul le contenu spécifique de l'application de la notification d'événements (c'est-à-dire le contenu de l'élément NotificationMessage) sera envoyé au noeud final du destinataire cible. Notez que le noeud final de service Web indiqué dans la référence du consommateur de la requête Subscribe qui indique également UseRaw (c'est-à-dire, une requête d'abonnement brut) ne doit pas obligatoirement implémenter le type de port du consommateur de la notification, car l'opération Notifiy ne sera pas utilisé pour livrer les notifications d'événements.
Cela signifie que le destinataire doit être capable d'accepter des opérations pour chaque type de contenu d'application qui sera publié dans le sujet spécifié. Ce modèle permet à WS-Notification d'appeler un groupe d'applications de service Web existantes qui ne sont pas compatibles WS-Notification, mais qui souhaitent recevoir les mêmes informations.
JAX-WS prend en charge la répartition basée sur des actions et les applications client JAX-WS doivent accepter l'URI d'action http://docs.oasis-open.org/wsn/bw-2/NotificationConsumer/Notify. Pour plus d'informations, voir le conseil relatif à l'identification et résolution des problèmes, Une application JAX-WS, client de notifications par courtier, doit reconnaître une action SOAP de courtier de notification.
Pour écrire le code pour un client JAX-RPC dans le rôle de l'enregistrement du diffuseur de publications, pour enregistrer un diffuseur de publications avec un courtier, exécutez les étapes ci-après en vous reportant au code fourni à titre d'exemple pour plus d'informations.
Procédure
Exemple
// 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;