From e721aa978d542d7f7668d9027affaafccfa8bab5 Mon Sep 17 00:00:00 2001 From: edwardsmj Date: Tue, 21 Dec 2010 07:22:12 +0000 Subject: Tweaks and extensions to better match the async service implementation in core + JMS binding git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1051397 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/sample/impl/SampleAsyncResponseInvoker.java | 6 +++++- .../src/main/java/sample/impl/SampleWSDLInvoker.java | 13 ++++++++++--- .../src/main/java/sample/impl/SampleWSDLProxy.java | 14 ++++++-------- .../test/java/sample/UpperSampleAsyncReferenceImpl.java | 14 ++++++++++++-- 4 files changed, 33 insertions(+), 14 deletions(-) (limited to 'sca-java-2.x/trunk/samples') diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleAsyncResponseInvoker.java b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleAsyncResponseInvoker.java index 2366deeb33..aed21ecc4e 100644 --- a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleAsyncResponseInvoker.java +++ b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleAsyncResponseInvoker.java @@ -49,7 +49,11 @@ class SampleAsyncResponseInvoker implements InvokerAsyncResponse { public void invokeAsyncResponse(final Message msg) { try { - String messageID = (String) msg.getHeaders().get(Constants.MESSAGE_ID); + String messageID = (String) msg.getHeaders().get("RELATES_TO"); + if (messageID == null ) { + messageID = (String) msg.getHeaders().get(Constants.MESSAGE_ID); + } // end if + String forwardOpName = (String)asyncMessageMap.get(messageID); // process the async response diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java index 4d33a956a8..6758c1fc1f 100644 --- a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java +++ b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java @@ -21,11 +21,13 @@ package sample.impl; import java.lang.reflect.Method; +import org.apache.tuscany.sca.core.invocation.AsyncResponseInvoker; import org.apache.tuscany.sca.core.invocation.InterceptorAsyncImpl; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLOperation; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.oasisopen.sca.ServiceRuntimeException; import org.w3c.dom.Element; /** @@ -55,6 +57,10 @@ class SampleWSDLInvoker extends InterceptorAsyncImpl { } public void invokeAsyncRequest(Message msg) { + // Retrieve the async callback information + AsyncResponseInvoker respInvoker = (AsyncResponseInvoker)msg.getHeaders().get("ASYNC_RESPONSE_INVOKER"); + if( respInvoker == null ) throw new ServiceRuntimeException("Async Implementation invoked with no response invoker"); + Message responseMsg = processRequest(msg); // in this sample programming model we make the async @@ -62,11 +68,12 @@ class SampleWSDLInvoker extends InterceptorAsyncImpl { // component implementation itself doesn't get a chance to // do async responses. - // At this point we could serialize the ??? and pick it up again + // At this point we could serialize the AsyncResponseInvoker and pick it up again // later to send the async response - ((RuntimeEndpoint)msg.getTo()).invokeAsyncResponse(responseMsg); - } + //((RuntimeEndpoint)msg.getTo()).invokeAsyncResponse(responseMsg); + respInvoker.invokeAsyncResponse(responseMsg); + } // end method invokeAsyncRequest public Message processRequest(Message msg) { try { diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java index 0fc8403cf1..176dac6b15 100644 --- a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java +++ b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java @@ -22,6 +22,8 @@ package sample.impl; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.core.ExtensionPointRegistry; @@ -50,7 +52,7 @@ class SampleWSDLProxy implements WSDLReference { this.ep = ep; mf = ep.getExtensionPoint(MessageFactory.class); repr = (RuntimeEndpointReference)epr; - ops = new HashMap(); + ops = new ConcurrentHashMap(); for(Operation o: wi.getOperations()) ops.put(o.getName(), o); } @@ -71,9 +73,9 @@ class SampleWSDLProxy implements WSDLReference { Message message = mf.createMessage(); message.setBody(new Object[]{e}); - // We could MESSAGE_ID here if required. If not the infrastructure - // will generate a UUID - String messageID = "myuniqueid"; + // Generate MESSAGE_ID here. + // String messageID = "myuniqueid"; + String messageID = UUID.randomUUID().toString(); message.getHeaders().put(Constants.MESSAGE_ID, messageID); // save the message id ready for when we process the response @@ -88,9 +90,5 @@ class SampleWSDLProxy implements WSDLReference { ex.printStackTrace(); } - // if we don't provide a message id we can get the one the - // infrastructure generates - //String messageID = (String) message.getHeaders().get(Constants.MESSAGE_ID); - //asyncMessageMap.put(messageID, op); } } diff --git a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/test/java/sample/UpperSampleAsyncReferenceImpl.java b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/test/java/sample/UpperSampleAsyncReferenceImpl.java index b6f59515cc..7a86d68043 100644 --- a/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/test/java/sample/UpperSampleAsyncReferenceImpl.java +++ b/sca-java-2.x/trunk/samples/extending-tuscany/implementation-sample/src/test/java/sample/UpperSampleAsyncReferenceImpl.java @@ -24,8 +24,12 @@ import static sample.Xutil.elem; import static sample.Xutil.text; import static sample.Xutil.xdom; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + import org.w3c.dom.Element; +import sample.Xutil.NodeBuilder; import sample.api.Java; import sample.api.WSDL; import sample.api.WSDLReference; @@ -42,6 +46,7 @@ public class UpperSampleAsyncReferenceImpl { WSDLReference upper; Element response; + CountDownLatch latch = new CountDownLatch( 1 ); public String upper(String s) { out.println("UpperSampleAsyncReferenceImpl.upper(" + s + ")"); @@ -49,16 +54,20 @@ public class UpperSampleAsyncReferenceImpl { // TODO - I'm passing in the non-wrapped version of the parameter // here which doesn't seem right. Need to test that databinding // wraps it correctly - final Element ureq = xdom("http://sample/upper-async", "s", text(s)); + //final Element ureq = xdom("http://sample/upper-async", "s", text(s)); + NodeBuilder node1 = elem("s", text(s)); + final Element ureq = xdom("http://sample/upper-async", "upper", node1); upper.callAsync("upper", ureq); try { Thread.sleep(500); + latch.await(500, TimeUnit.SECONDS); } catch (Exception ex) { // do nothing } - return response.getTextContent(); + if( response != null ) return response.getTextContent(); + else return "upper did not get called back"; } /** @@ -69,5 +78,6 @@ public class UpperSampleAsyncReferenceImpl { public void upperCallback(Element response) { out.println("UpperSampleAsyncReferenceImpl.upperCallback(" + response.getTextContent() + ")"); this.response = response; + latch.countDown(); } } -- cgit v1.2.3