cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6395] Handling AsyncResponse.setTimeout on the pending continuation
Date Fri, 08 May 2015 09:38:01 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 6187cb922 -> 4a233ad73


[CXF-6395] Handling AsyncResponse.setTimeout on the pending continuation


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4a233ad7
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4a233ad7
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4a233ad7

Branch: refs/heads/master
Commit: 4a233ad73bf6cc5d0486b724e69a7d066c528d6d
Parents: 6187cb9
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri May 8 10:37:42 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri May 8 10:37:42 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/impl/AsyncResponseImpl.java       | 20 +++++++--
 .../cxf/jaxrs/client/spec/ClientImpl.java       |  1 +
 .../cxf/systest/jaxrs/AsyncResource2.java       | 45 ++++++++++++++++++++
 .../jaxrs/JAXRSContinuationsServlet3Test.java   | 20 +++++++++
 .../resources/jaxrs_async/WEB-INF/beans.xml     |  1 +
 5 files changed, 84 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/4a233ad7/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
index 60c820b..773faaf 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/AsyncResponseImpl.java
@@ -54,6 +54,7 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
     private volatile boolean done;
     private boolean resumedByApplication;
     private TimeoutHandler timeoutHandler;
+    private Long pendingTimeout;
     
     private List<CompletionCallback> completionCallbacks = new LinkedList<CompletionCallback>();
     private List<ConnectionCallback> connectionCallbacks = new LinkedList<ConnectionCallback>();
@@ -153,13 +154,22 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
         if (isCancelledOrNotSuspended()) {
             return false;
         }
-        inMessage.getExchange().put(AsyncResponse.class, this);
+        setAsyncResponseOnExchange();
         long timeout = TimeUnit.MILLISECONDS.convert(time, unit);
         initialSuspend = false;
-        cont.suspend(timeout);
+        if (!cont.isPending()) {
+            cont.suspend(timeout);
+        } else {
+            pendingTimeout = timeout;
+            cont.resume();
+        }
         return true;
     }
 
+    private void setAsyncResponseOnExchange() {
+        inMessage.getExchange().put(AsyncResponse.class, this);
+    }
+
     @Override
     public void setTimeoutHandler(TimeoutHandler handler) {
         timeoutHandler = handler;
@@ -273,7 +283,11 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
     
     public synchronized void handleTimeout() {
         if (!resumedByApplication) {
-            if (timeoutHandler != null) {
+            if (pendingTimeout != null) {
+                setAsyncResponseOnExchange();
+                cont.suspend(pendingTimeout);
+                pendingTimeout = null;
+            } else if (timeoutHandler != null) {
                 timeoutHandler.handleTimeout(this);
             } else {
                 cont.setObject(new ServiceUnavailableException());

http://git-wip-us.apache.org/repos/asf/cxf/blob/4a233ad7/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
index f19a32f..08c3268 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientImpl.java
@@ -304,6 +304,7 @@ public class ClientImpl implements Client {
                 JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
                 bean.setAddress(uri.toString());
                 targetClient = bean.createWebClient();
+                WebClient.getConfig(targetClient).getHttpConduit().getClient().setReceiveTimeout(1000000);
                 ClientImpl.this.baseClients.add(targetClient);
             } else if (!targetClient.getCurrentURI().equals(uri)) {
                 targetClient.to(uri.toString(), false);

http://git-wip-us.apache.org/repos/asf/cxf/blob/4a233ad7/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource2.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource2.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource2.java
new file mode 100644
index 0000000..dd53cc1
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AsyncResource2.java
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.systest.jaxrs;
+
+import java.util.concurrent.TimeUnit;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.container.AsyncResponse;
+import javax.ws.rs.container.Suspended;
+
+@Path("resource2")
+public class AsyncResource2 {
+    static AsyncResponse asyncResponse;
+    
+    @GET
+    @Path("/suspend")
+    public void getSuspendResponse(@Suspended AsyncResponse async) {
+        asyncResponse = async;
+    }
+    
+    @GET
+    @Path("/setTimeOut")
+    public String setTimeOut() {
+        boolean setTimeout = asyncResponse.setTimeout(2, TimeUnit.SECONDS);
+        return String.valueOf(setTimeout);
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/4a233ad7/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
index de32d9c..a8f5641 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSContinuationsServlet3Test.java
@@ -81,6 +81,26 @@ public class JAXRSContinuationsServlet3Test extends AbstractJAXRSContinuationsTe
         Future<Response> cancel = invokeRequest(base + "cancelvoid?stage=1"); 
         assertString(cancel, AsyncResource.FALSE); 
     }
+    @Test
+    public void testSuspendSetTimeoutt() throws Exception { 
+        final String base = "http://localhost:" + getPort() + "/async/resource2/";
+        Future<Response> suspend = invokeRequest(base + "suspend");
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                Future<Response> timeout = invokeRequest(base + "setTimeOut");
+                try {
+                    assertString(timeout, "true");
+                } catch (Exception ex) {
+                    ex.printStackTrace();
+                }
+            }
+        });
+        t.start();
+        t.join();
+        
+        assertEquals(503, suspend.get().getStatus());
+         
+    }
 
     private static void assertString(Future<Response> future, String check) throws
Exception { 
         Response response = future.get(); 

http://git-wip-us.apache.org/repos/asf/cxf/blob/4a233ad7/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
index 9419849..2dd4859 100644
--- a/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs_async/WEB-INF/beans.xml
@@ -35,6 +35,7 @@
         <jaxrs:serviceBeans>
             <ref bean="serviceBean"/>
             <bean class="org.apache.cxf.systest.jaxrs.AsyncResource"/>
+            <bean class="org.apache.cxf.systest.jaxrs.AsyncResource2"/>
         </jaxrs:serviceBeans>
         <jaxrs:providers>
             <bean class="org.apache.cxf.systest.jaxrs.BookContinuationFilter"/>


Mime
View raw message