cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1409193 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/transports/http/src/main/java/org/apache/cxf/transport/http/ systests/jaxrs/src/test/java/org/apac...
Date Wed, 14 Nov 2012 13:31:27 GMT
Author: sergeyb
Date: Wed Nov 14 13:31:26 2012
New Revision: 1409193

URL: http://svn.apache.org/viewvc?rev=1409193&view=rev
Log:
Updating Servlet3ContinuationProvider to startAsync every time the continuation is requested,
and JAX-RS AsyncResponseImpl not to cache continuations

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java Wed Nov
14 13:31:26 2012
@@ -84,6 +84,7 @@ public class JAXRSInvoker extends Abstra
             AsyncResponse asyncResp = exchange.get(AsyncResponse.class);
             if (asyncResp != null) {
                 AsyncResponseImpl asyncImpl = (AsyncResponseImpl)asyncResp;
+                asyncImpl.prepareContinuation();
                 
                 if (asyncImpl.isResumedByApplication()) {
                     Object asyncObj = asyncImpl.getResponseObject();

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
Wed Nov 14 13:31:26 2012
@@ -53,9 +53,7 @@ public class AsyncResponseImpl implement
         inMessage.getExchange().put(ContinuationCallback.class, this);
         this.inMessage = inMessage;
         
-        ContinuationProvider provider = 
-            (ContinuationProvider)inMessage.get(ContinuationProvider.class.getName());
-        cont = provider.getContinuation();
+        initContinuation();
     }
     
     @Override
@@ -94,12 +92,12 @@ public class AsyncResponseImpl implement
     
     private synchronized void doCancel(String retryAfterHeader) {
         checkSuspended();
-        cancelled = true;
         ResponseBuilder rb = Response.status(503);
         if (retryAfterHeader != null) {
             rb.header(HttpHeaders.RETRY_AFTER, retryAfterHeader);
         }
         doResume(rb.build());
+        cancelled = true;
     }
 
     @Override
@@ -185,6 +183,23 @@ public class AsyncResponseImpl implement
         }
     }
     
+    @Override
+    public void onComplete() {
+        done = true;
+        if (completionCallback != null) {
+            completionCallback.onComplete();
+        }
+    }
+
+    @Override
+    public void onError(Throwable error) {
+        if (completionCallback != null) {
+            Throwable actualError = error instanceof Fault ? ((Fault)error).getCause() :
error;
+            completionCallback.onError(actualError);
+        }
+        
+    }
+    
     // these methods are called by the runtime, not part of AsyncResponse    
     public synchronized void suspend() {
         checkCancelled();
@@ -213,22 +228,15 @@ public class AsyncResponseImpl implement
         
     }
 
-    
-    
-    @Override
-    public void onComplete() {
-        done = true;
-        if (completionCallback != null) {
-            completionCallback.onComplete();
-        }
+    private void initContinuation() {
+        ContinuationProvider provider = 
+            (ContinuationProvider)inMessage.get(ContinuationProvider.class.getName());
+        cont = provider.getContinuation();
     }
-
-    @Override
-    public void onError(Throwable error) {
-        if (completionCallback != null) {
-            Throwable actualError = error instanceof Fault ? ((Fault)error).getCause() :
error;
-            completionCallback.onError(actualError);
-        }
-        
+    
+    public void prepareContinuation() {
+        initContinuation();
     }
+    
+    
 }

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Servlet3ContinuationProvider.java
Wed Nov 14 13:31:26 2012
@@ -57,32 +57,30 @@ public class Servlet3ContinuationProvide
 
         if (continuation == null) {
             continuation = new Servlet3Continuation();
+        } else {
+            continuation.startAsyncAgain();
         }
         return continuation;
     }
     
     public class Servlet3Continuation implements Continuation, AsyncListener {
-        final AsyncContext context;
+        AsyncContext context;
         volatile boolean isNew;
         volatile boolean isResumed;
         volatile boolean isPending;
         volatile Object obj;
         private ContinuationCallback callback;
         public Servlet3Continuation() {
-            // It looks current Servlet3 implementation request doesn't pass the isAsyncStart

-            // status to the redispatched request, so we use the attribute to check the statues
-            isNew = req.getAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE) ==
null;
-            if (isNew) {
-                req.setAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE,
-                                 inMessage.getExchange().getInMessage());
-                callback = inMessage.getExchange().get(ContinuationCallback.class);
-                context = req.startAsync(req, resp);
-                req.setAttribute(AbstractHTTPDestination.CXF_ASYNC_CONTEXT, context);
-                context.addListener(this);
-            } else {
-                context = (AsyncContext)req.getAttribute(AbstractHTTPDestination.CXF_ASYNC_CONTEXT);
-            }
-            
+            req.setAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE,
+                             inMessage.getExchange().getInMessage());
+            callback = inMessage.getExchange().get(ContinuationCallback.class);
+            context = req.startAsync(req, resp);
+            context.addListener(this);
+        }
+        
+        void startAsyncAgain() {
+            context = req.startAsync();
+            context.addListener(this);
         }
         
         public boolean suspend(long timeout) {

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
Wed Nov 14 13:31:26 2012
@@ -24,6 +24,7 @@ import java.util.concurrent.CountDownLat
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 
 import org.apache.commons.httpclient.HttpClient;
@@ -48,6 +49,23 @@ public abstract class AbstractJAXRSConti
     }
     
     @Test
+    public void testTimeoutAndCancel() throws Exception {
+        WebClient wc = WebClient.create("http://localhost:" + getPort() + "/bookstore/books/cancel");
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        Response r = wc.get();
+        assertEquals(503, r.getStatus());
+        String retryAfter = r.getHeaderString(HttpHeaders.RETRY_AFTER);
+        assertNotNull(retryAfter);
+        assertEquals("10", retryAfter);
+    }
+    
+    @Test
+    public void testContinuationWithTimeHandler() throws Exception {
+        
+        doTestContinuation("books/timeouthandler");
+    }
+    
+    @Test
     public void testContinuation() throws Exception {
         
         doTestContinuation("books");

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
Wed Nov 14 13:31:26 2012
@@ -48,12 +48,19 @@ public class BookContinuationStore {
     
     @GET
     @Path("/books/defaulttimeout")
-    public void getBookDescriptionWithHandler(AsyncResponse async) {
+    public void getBookDescriptionWithTimeout(AsyncResponse async) {
         async.register(new CallbackImpl());
         async.setTimeout(2000, TimeUnit.MILLISECONDS);
     }
     
     @GET
+    @Path("/books/cancel")
+    public void getBookDescriptionWithCancel(@PathParam("id") String id, AsyncResponse async)
{
+        async.setTimeout(2000, TimeUnit.MILLISECONDS);
+        async.setTimeoutHandler(new CancelTimeoutHandlerImpl());
+    }
+    
+    @GET
     @Path("/books/timeouthandler/{id}")
     public void getBookDescriptionWithHandler(@PathParam("id") String id, AsyncResponse async)
{
         async.setTimeout(2000, TimeUnit.MILLISECONDS);
@@ -115,7 +122,7 @@ public class BookContinuationStore {
         
         @Override
         public void handleTimeout(AsyncResponse asyncResponse) {
-            if (timeoutExtendedCounter.addAndGet(1) < 2) {
+            if (timeoutExtendedCounter.addAndGet(1) <= 2) {
                 asyncResponse.setTimeout(1, TimeUnit.SECONDS);
             } else {
                 asyncResponse.resume(books.get(id));
@@ -124,6 +131,16 @@ public class BookContinuationStore {
         
     }
     
+    private class CancelTimeoutHandlerImpl implements TimeoutHandler {
+
+        @Override
+        public void handleTimeout(AsyncResponse asyncResponse) {
+            asyncResponse.cancel(10);
+            
+        }
+        
+    }
+    
     private class CallbackImpl implements CompletionCallback {
 
         @Override

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
Wed Nov 14 13:31:26 2012
@@ -22,8 +22,6 @@ package org.apache.cxf.systest.jaxrs;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 
 import org.junit.BeforeClass;
-import org.junit.Ignore;
-import org.junit.Test;
 
 
 public class JAXRSContinuationsServlet3Test extends AbstractJAXRSContinuationsTest {
@@ -38,13 +36,6 @@ public class JAXRSContinuationsServlet3T
                    
     }
     
-    @Test
-    @Ignore
-    public void testContinuationWithTimeHandler() throws Exception {
-        
-        doTestContinuation("books/timeouthandler");
-    }
-    
     protected String getPort() {
         return PORT;
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsTest.java?rev=1409193&r1=1409192&r2=1409193&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsTest.java
Wed Nov 14 13:31:26 2012
@@ -22,7 +22,6 @@ package org.apache.cxf.systest.jaxrs;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 
 import org.junit.BeforeClass;
-import org.junit.Test;
 
 
 public class JAXRSContinuationsTest extends AbstractJAXRSContinuationsTest {
@@ -32,16 +31,11 @@ public class JAXRSContinuationsTest exte
         AbstractResourceInfo.clearAllMaps();
         createStaticBus();
         assertTrue("server did not launch correctly",
-                   launchServer(BookContinuationServer.class));
+                   launchServer(BookContinuationServer.class, true));
                    
                    
     }
     
-    @Test
-    public void testContinuationWithTimeHandler() throws Exception {
-        
-        doTestContinuation("books/timeouthandler");
-    }
     
     protected String getPort() {
         return PORT;



Mime
View raw message