Now and then in JDeveloper land we have an organization wide bug hunt to improve the overall quality of our product. For the most recent one I decided to try to write a async web service from the bottom up.
For the purposes of this experiment I am going to create a simple HelloWorld example so first off lets created the primary interface:
package model; import ...; @WebService public class Hello { public void sayHello(String name) { ... } }
And of course we need to define the interface for the callback service:
package model; @WebService public interface HelloCallback { public void response(String message); }
Simple enough, right? Now I we are going to create a static wsdl under public_html/wsdl. Use the normal wizard to create a blank document then cut and paste the results of "Show WSDL for annotations" from the Hello class. You also need to do something similar for the HelloCallback service. Unfortunately you can't show the WSDL for a SEI, although I have raised that bug, so the quickest way to generate the WSDL content is to temporarily convert HelloCallback to a class.
To complete the WSDL add a partnerLinkType, this is used by tools such as BPEL to link the two halves of the equation:
<partnerLinkType name="HelloService" xmlns="http://schemas.xmlsoap.org/ws/2003/05/partner-link/"> <role name="In"> <portType name="tns:Hello"/> </role> <role name="Out"> <portType name="tns:HelloCallBack"/> </role> </partnerLinkType>
Then it is worth updating the Hello class to reference this WSDL in the annotations:
package model; @WebService(wsdlLocation = "/wsdl/Hello.wsdl") public class Hello { ... }
Okay so the specifications are in place lets look at a trivial implementation of the sayHello method. Note that in this case the response can arrive before the original call has completed; but this will do for this example.
SOAPMessageContext smc = (SOAPMessageContext)_context.getMessageContext(); // Get hold of the AddressingProperties and look up the reply to address // and later the return message id // AddressingProperties ap = AddressingUtils.getAddressingProperties(smc.getMessage()); EndpointReference endpointReference = ap.getReplyTo(); AttributedURI address = endpointReference.getAddress(); URI replyTo = address.getURI(); // try { // Use the servlet context to get a reference to the local wsdl // ServletContext sc = (ServletContext)_context.getMessageContext().get( "javax.xml.ws.servlet.context"); URL found = sc.getResource("/wsdl/Hello.wsdl"); // Create a new service instance on the callback Service s = Service.create(found, new QName( "http://model/", "HelloCallBackService")); // Create a port HelloCallBack acb = s.getPort( new QName( "http://model/", "HelloCallBackPort"), HelloCallBack.class); BindingProvider bp = (BindingProvider)acb; MaprequestProperties = bp.getRequestContext(); // Change the endpoint address to the match the replyTo address requestProperties.put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, replyTo.toString()); // Make sure that this request has the correct addressing headers AddressingUtils.setupClientSideWSAddressing(requestProperties); String t = requestProperties.keySet().toString(); AddressingProperties rap = AddressingUtils.getOutboundAddressingProperties(requestProperties); rap.setRelatesTo( ap.getMessageID()); // Invoke the response address // acb.response("Hello " + name); } catch (Exception ex) { ex.printStackTrace(); }
Part of the pleasure of using JAX-WS over JAX-RPC is the simplicity of invoking services dynamically in this way. As you can see above this is actually really quite straight forward.
So how would you go about testing this, well the HTTP Analyzer in JDeveloper doesn't support WS-Addressing headers properly although we are working on this. You can of course create your own headers in the HTTP Content view, it might look like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header xmlns:ns1="http://www.w3.org/2005/08/addressing"> <ns1:MessageID>ws:uniqueAddress</ns1:MessageID> <ns1:ReplyTo> <ns1:Address>http://returnAddress.com/example</ns1:Address> </ns1:ReplyTo> </soap:Header> <soap:Body xmlns:ns2="http://model/"> <ns2:sayHello> <arg0>John</arg0> </ns2:sayHello> </soap:Body> </soap:Envelope>
If you start up the HTTP Analyzer before the embedded server you can send his message to the endpoint and you should see the failed attempt to send the message to the returnAddress url in the analyzer log. It should look something like:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing"> <env:Header> <wsa:To>http://returnAddress.com/example</wsa:To> <ns1:MessageID>ws:anotherUniqueAddress</ns1:MessageID> <wsa:RelatesTo xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="0">ws:uniqueAddress</wsa:RelatesTo> </env:Header> <env:Body> <ns0:response xmlns:ns0="http://model/"> <arg0 xmlns="">Hello John</arg0> </ns0:response> </env:Body> </env:Envelope>
This is worth remembering this trick if you are trying to test a async web service and don't want to go to the bother of setting up a valid return address to connect to.
Next time I get a few minutes I will take a look at the client side of this equation. Back to fixing the bugs I have just found.