Exemple : Création d'un squelette de service Web de consommateur de notification
Utilisez cet exemple pour créer un service Web qui implémente le type de port NotificationConsumer défini par la spécification de notification de base des services Web.
Pourquoi et quand exécuter cette tâche
Cette tâche fournit deux exemples de code :
- Un exemple de document WSDL qui décrit un service Web qui implémente le type de port NotificationConsumer défini par la spécification de notification de base des services Web.
- Une implémentation de base de l'interface SEI (Service Endpoint Interface) générée à partir du document WSDL précédent à l'aide de l'outil WSDL2Java.
Remarque : L'article Ecriture d'applications JAX-WS pour
WS-Notification contient également un exemple de service Web
de consommateur.
Procédure
Si vous créez un un squelette de service Web de consommateur de notification, reportez-vous aux exemples de code ci-après.
Exemple
L'exemple de document WSDL suivant décrit un service Web qui implémente le type de port NotificationConsumer défini par la spécification de notification de base des services Web :
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wsn-bw="http://docs.oasis-open.org/wsn/bw-2"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="uri:example.wsn/consumer"
targetNamespace="uri:example.wsn/consumer">
<wsdl:import namespace="http://docs.oasis-open.org/wsn/bw-2"
location="http://docs.oasis-open.org/wsn/bw-2.wsdl" />
<wsdl:binding name="NotificationConsumerBinding" type="wsn-bw:NotificationConsumer">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Notify">
<wsdlsoap:operation soapAction="" />
<wsdl:input>
<wsdlsoap:body use="literal" />
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="NotificationConsumerService">
<wsdl:port name="NotificationConsumerPort" binding="tns:NotificationConsumerBinding">
<wsdlsoap:address location="http://myserver.mycom.com:9080/Consumer" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
L'exemple suivant présente une implémentation de base de l'interface SEI (Service Endpoint Interface) générée à partir du document WSDL précédent à l'aide de l'outil WSDL2Java :
public class ConsumerExample implements NotificationConsumer {
public void notify(NotificationMessage[] notificationMessage, SOAPElement[] any)
throws RemoteException {
// Process each NotificationMessage
for (int i=0; i<notificationMessage.length; i++) {
NotificationMessage message = notificationMessage[i];
// Get the contents of the message
SOAPElement messageContent = message.getMessageContents();
// Get the expression indicating which topic the message is associated with
TopicExpression topic = message.getTopic();
// Get a reference to the producer (this value is optional and so might be null)
EndpointReference producerRef = message.getProducerReference();
// Get a reference to the subscription (this value is optional and so might be null)
EndpointReference subscriptionRef = message.getSubscriptionReference();
// User defined processing ...
}
}
}