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-7037] Removing synchronized blocks from AsyncResponseImpl
Date Mon, 05 Sep 2016 11:51:29 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 53dcc25f5 -> 2a406be28


[CXF-7037] Removing synchronized blocks from AsyncResponseImpl


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

Branch: refs/heads/3.0.x-fixes
Commit: 2a406be28370dce26cb5c16db79f9e629c86f123
Parents: 53dcc25
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Sep 5 12:48:56 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Sep 5 12:51:11 2016 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/impl/AsyncResponseImpl.java       | 38 ++++++++++----------
 .../jaxrs/AbstractJAXRSContinuationsTest.java   | 11 ++++++
 .../systest/jaxrs/BookContinuationStore.java    | 15 ++++++++
 .../jaxrs/JAXRSContinuationsServlet3Test.java   |  3 ++
 .../cxf/systest/jaxrs/SlowJAXRSInvoker.java     | 35 ++++++++++++++++++
 .../resources/jaxrs_async/WEB-INF/beans.xml     | 20 +++++++++--
 6 files changed, 101 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/2a406be2/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 7b3178d..17c03a9 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
@@ -49,12 +49,12 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
     
     private Continuation cont;
     private Message inMessage;
-    private boolean initialSuspend;
-    private boolean cancelled;
-    private volatile boolean done;
-    private boolean resumedByApplication;
     private TimeoutHandler timeoutHandler;
-    private Long pendingTimeout;
+    private volatile boolean initialSuspend;
+    private volatile boolean cancelled;
+    private volatile boolean done;
+    private volatile boolean resumedByApplication;
+    private volatile Long pendingTimeout;
     
     private List<CompletionCallback> completionCallbacks = new LinkedList<CompletionCallback>();
     private List<ConnectionCallback> connectionCallbacks = new LinkedList<ConnectionCallback>();
@@ -82,13 +82,13 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
         return isCancelled() || !isSuspended();
     }
     
-    private synchronized boolean doResume(Object response) {
+    private boolean doResume(Object response) {
         if (isCancelledOrNotSuspended()) {
             return false;
         }
         return doResumeFinal(response);
     }
-    private synchronized boolean doResumeFinal(Object response) {
+    private boolean doResumeFinal(Object response) {
         inMessage.getExchange().put(AsyncResponse.class, this);
         cont.setObject(response);
         resumedByApplication = true;
@@ -115,24 +115,24 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
         return doCancel(HttpUtils.getHttpDateFormat().format(retryAfter));
     }
     
-    private synchronized boolean doCancel(String retryAfterHeader) {
-        if (cancelled) {
-            return true;
-        }
+    private boolean doCancel(String retryAfterHeader) {
         if (!isSuspended()) {
             return false;
         }
+        if (cancelled) {
+            return true;
+        }
+        cancelled = true;
         ResponseBuilder rb = Response.status(503);
         if (retryAfterHeader != null) {
             rb.header(HttpHeaders.RETRY_AFTER, retryAfterHeader);
         }
-        cancelled = true;
         doResumeFinal(rb.build());
         return cancelled;
     }
 
     @Override
-    public synchronized boolean isSuspended() {
+    public boolean isSuspended() {
         if (cancelled || resumedByApplication) {
             return false;
         }
@@ -150,7 +150,7 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
     }
 
     @Override
-    public synchronized boolean setTimeout(long time, TimeUnit unit) throws IllegalStateException
{
+    public boolean setTimeout(long time, TimeUnit unit) throws IllegalStateException {
         if (isCancelledOrNotSuspended()) {
             return false;
         }
@@ -259,17 +259,17 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
         }
     }
     
-    public synchronized boolean suspendContinuationIfNeeded() {
-        if (!cont.isPending() && !resumedByApplication) {
-            initialSuspend = false;
+    public boolean suspendContinuationIfNeeded() {
+        if (!resumedByApplication && !cont.isPending() && !cont.isResumed())
{
             cont.suspend(AsyncResponse.NO_TIMEOUT);
+            initialSuspend = false;
             return true;
         } else {
             return false;
         }
     }
     
-    public synchronized Object getResponseObject() {
+    public Object getResponseObject() {
         Object obj = cont.getObject();
         if (!(obj instanceof Response) && !(obj instanceof Throwable)) {
             if (obj == null) {
@@ -281,7 +281,7 @@ public class AsyncResponseImpl implements AsyncResponse, ContinuationCallback
{
         return obj;
     }
     
-    public synchronized boolean isResumedByApplication() {
+    public boolean isResumedByApplication() {
         return resumedByApplication;
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/2a406be2/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
index cfba7a4..b47818d 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/AbstractJAXRSContinuationsTest.java
@@ -55,6 +55,14 @@ public abstract class AbstractJAXRSContinuationsTest extends AbstractBusClientSe
         String str = wc.get(String.class);
         assertEquals("immediateResume", str);
     }
+    @Test
+    public void testResumeFromFastAppThread() throws Exception {
+        WebClient wc = WebClient.create("http://localhost:" + getPort() + getBaseAddress2()

+            + "/books/resumeFromFastThread");
+        wc.accept("text/plain");
+        String str = wc.get(String.class);
+        assertEquals("resumeFromFastThread", str);
+    }
     
     @Test
     public void testNoContent() throws Exception {
@@ -229,6 +237,9 @@ public abstract class AbstractJAXRSContinuationsTest extends AbstractBusClientSe
     protected String getBaseAddress() {
         return "/bookstore";
     }
+    protected String getBaseAddress2() {
+        return "/bookstore";
+    }
     
     protected abstract String getPort();
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/2a406be2/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
index ccf1ed6..c2e1e84 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookContinuationStore.java
@@ -66,6 +66,21 @@ public class BookContinuationStore implements BookAsyncInterface {
     public void getBookDescriptionImmediateResume(@Suspended AsyncResponse async) {
         async.resume("immediateResume");
     }
+    @GET
+    @Path("/books/resumeFromFastThread")
+    @Produces("text/plain")
+    public void getBookDescriptionResumeFromFastThread(@Suspended AsyncResponse async) {
+        executor.execute(new Runnable() {
+            public void run() {
+                try {
+                    Thread.sleep(1000);
+                } catch (Exception ex) {
+                    // ignore    
+                }
+                async.resume("resumeFromFastThread");
+            }
+        });
+    }
     
     @GET
     @Path("/books/nocontent")

http://git-wip-us.apache.org/repos/asf/cxf/blob/2a406be2/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 d1441a0..d002e14 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
@@ -137,6 +137,9 @@ public class JAXRSContinuationsServlet3Test extends AbstractJAXRSContinuationsTe
     protected String getBaseAddress() {
         return "/async/bookstore";
     }
+    protected String getBaseAddress2() {
+        return "/async2/bookstore";
+    }
     
     protected String getPort() {
         return PORT;

http://git-wip-us.apache.org/repos/asf/cxf/blob/2a406be2/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SlowJAXRSInvoker.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SlowJAXRSInvoker.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SlowJAXRSInvoker.java
new file mode 100644
index 0000000..ea5af25
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/SlowJAXRSInvoker.java
@@ -0,0 +1,35 @@
+/**
+ * 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.lang.reflect.Method;
+
+import org.apache.cxf.jaxrs.JAXRSInvoker;
+import org.apache.cxf.message.Exchange;
+
+public class SlowJAXRSInvoker extends JAXRSInvoker {
+
+    @Override
+    protected Object performInvocation(Exchange exchange, final Object serviceObject, Method
m,
+                                       Object[] paramArray) throws Exception {
+        Object response = super.performInvocation(exchange, serviceObject, m, paramArray);
+        Thread.sleep(5000);
+        return response;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/2a406be2/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 2dd4859..cfd07b1 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
@@ -31,7 +31,7 @@
     
     <bean class="org.apache.cxf.systest.jaxrs.BookContinuationStore" id="serviceBean"/>
     
-    <jaxrs:server id="bookservice" address="/async">
+    <jaxrs:server id="bookserviceAsync" address="/async">
         <jaxrs:serviceBeans>
             <ref bean="serviceBean"/>
             <bean class="org.apache.cxf.systest.jaxrs.AsyncResource"/>
@@ -44,8 +44,24 @@
             <entry key="disconnected.client.exception.class" value="org.eclipse.jetty.io.EofException"/>
         </jaxrs:properties>
     </jaxrs:server>
+    <jaxrs:server id="bookserviceAsync2" address="/async2">
+        <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"/>
+        </jaxrs:providers>        
+        <jaxrs:properties>
+            <entry key="disconnected.client.exception.class" value="org.eclipse.jetty.io.EofException"/>
+        </jaxrs:properties>
+        <jaxrs:invoker>
+            <bean class="org.apache.cxf.systest.jaxrs.SlowJAXRSInvoker"/>
+        </jaxrs:invoker>
+    </jaxrs:server>
     
-    <jaxrs:server id="bookservice2" address="/asyncexecutor">
+    <jaxrs:server id="bookserviceAsync3" address="/asyncexecutor">
     
         <jaxrs:executor>
             <ref bean="workerPool" />


Mime
View raw message