You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/01/22 15:21:45 UTC

[GitHub] andymc12 closed pull request #368: Ensure that the JAX-RS CompletionStage uses the correct ExecutorService

andymc12 closed pull request #368: Ensure that the JAX-RS CompletionStage uses the correct ExecutorService
URL: https://github.com/apache/cxf/pull/368
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CompletionStageRxInvokerImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CompletionStageRxInvokerImpl.java
index 9bcb5209e3c..e09a33d9ce5 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CompletionStageRxInvokerImpl.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CompletionStageRxInvokerImpl.java
@@ -146,7 +146,7 @@
         if (ex == null) {
             return CompletableFuture.supplyAsync(() -> wc.sync().method(name, entity, responseType));
         }
-        return CompletableFuture.supplyAsync(() -> wc.sync().method(name, entity, responseType), ex);
+        return CxfCompletableFuture.supplyAsync(() -> wc.sync().method(name, entity, responseType), ex);
     }
 
     @Override
@@ -154,7 +154,7 @@
         if (ex == null) {
             return CompletableFuture.supplyAsync(() -> wc.sync().method(name, entity, responseType));
         }
-        return CompletableFuture.supplyAsync(() -> wc.sync().method(name, entity, responseType), ex);
+        return CxfCompletableFuture.supplyAsync(() -> wc.sync().method(name, entity, responseType), ex);
     }
 
     @Override
@@ -162,7 +162,7 @@
         if (ex == null) {
             return CompletableFuture.supplyAsync(() -> wc.sync().method(name, responseType));
         }
-        return CompletableFuture.supplyAsync(() -> wc.sync().method(name, responseType), ex);
+        return CxfCompletableFuture.supplyAsync(() -> wc.sync().method(name, responseType), ex);
     }
 
     @Override
@@ -170,7 +170,7 @@
         if (ex == null) {
             return CompletableFuture.supplyAsync(() -> wc.sync().method(name, responseType));
         }
-        return CompletableFuture.supplyAsync(() -> wc.sync().method(name, responseType), ex);
+        return CxfCompletableFuture.supplyAsync(() -> wc.sync().method(name, responseType), ex);
     }
 
 }
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CxfCompletableFuture.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CxfCompletableFuture.java
new file mode 100644
index 00000000000..5b84aa443c0
--- /dev/null
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/CxfCompletableFuture.java
@@ -0,0 +1,129 @@
+/**
+ * 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.jaxrs.client;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ExecutorService;
+import java.util.function.BiConsumer;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+/**
+ * 
+ */
+public class CxfCompletableFuture<T> extends CompletableFuture<T> {
+
+    private final ExecutorService executor;
+
+    public CxfCompletableFuture(ExecutorService es) {
+        super();
+        executor = es;
+    }
+
+    public static <U> CxfCompletableFuture<U> supplyAsync(Supplier<U> supplier,
+                                                          ExecutorService executor) {
+        if (supplier == null) {
+            throw new NullPointerException();
+        }
+        CxfCompletableFuture<U> d = new CxfCompletableFuture<U>(executor);
+        executor.execute(new Runnable() {
+
+            @Override
+            public void run() {
+                if (d != null && supplier != null) {
+                    try {
+                        d.complete(supplier.get());
+                    } catch (Throwable t) {
+                        d.completeExceptionally(t);
+                    }
+                }
+            } });
+        return d;
+    }
+
+
+
+    @Override
+    public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) {
+        return acceptEitherAsync(other, action, executor);
+    }
+
+    @Override
+    public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) {
+        return applyToEitherAsync(other, fn, executor);
+    }
+
+    @Override
+    public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) {
+        return handleAsync(fn, executor);
+    }
+
+    @Override
+    public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
+                                                     Runnable action) {
+        return runAfterBothAsync(other, action, executor);
+    }
+
+    @Override
+    public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
+                                                       Runnable action) {
+        return runAfterEitherAsync(other, action, executor);
+    }
+
+    @Override
+    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) {
+        return thenAcceptAsync(action, executor);
+    }
+
+    @Override
+    public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,
+                                                           BiConsumer<? super T, ? super U> action) {
+        return thenAcceptBothAsync(other, action, executor);
+    }
+
+    @Override
+    public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn) {
+        return thenApplyAsync(fn, executor);
+    }
+
+    @Override
+    public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, 
+                                                        BiFunction<? super T, ? super U, ? extends V> fn) {
+        return thenCombineAsync(other, fn, executor);
+    }
+
+    @Override
+    public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) {
+        return thenComposeAsync(fn, executor);
+    }
+
+    @Override
+    public CompletableFuture<Void> thenRunAsync(Runnable action) {
+        return thenRunAsync(action);
+    }
+
+    @Override
+    public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) {
+        return whenCompleteAsync(action, executor);
+    }
+}
diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java
index c2f185733b0..dcbf4083cbd 100644
--- a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java
@@ -18,14 +18,26 @@
  */
 package org.apache.cxf.jaxrs.client.spec;
 
+import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.ClientRequestContext;
+import javax.ws.rs.client.ClientRequestFilter;
 import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
 
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.interceptor.Interceptor;
+import org.apache.cxf.jaxrs.client.AbstractClient;
 import org.apache.cxf.jaxrs.client.ClientConfiguration;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.client.spec.ClientImpl.WebTargetImpl;
@@ -127,4 +139,38 @@ public void testTemplateInInitialTarget() {
                    doesClientConfigHaveMyInterceptor(webClientAfterPath));
 
     }
+
+    public static class MockServerResponse implements ClientRequestFilter {
+
+        /** {@inheritDoc}*/
+        @Override
+        public void filter(ClientRequestContext crc) throws IOException {
+            crc.abortWith(Response.ok("SUCCESS").build());
+        }
+        
+    }
+    @Test
+    public void testCompletionStageUsesSpecifiedExecutorForAsyncOperations() throws Exception {
+        CountDownLatch latch = new CountDownLatch(1);
+        List<String> threadInfo = new ArrayList<String>();
+        ExecutorService es = Executors.newSingleThreadExecutor(new ThreadFactory() {
+
+            @Override
+            public Thread newThread(Runnable r) {
+                return new Thread(r, "CXF-Thread");
+            } });
+        Client client = ClientBuilder.newClient()
+                                     .register(MockServerResponse.class)
+                                     .property(AbstractClient.EXECUTOR_SERVICE_PROPERTY, es);
+
+        CompletionStage<String> cs = client.target("http://localhost:9999/dummyApp").request().rx().get(String.class);
+        cs.thenAcceptAsync(o -> {
+            threadInfo.add(Thread.currentThread().getName());
+            latch.countDown();
+        });
+        latch.await();
+        assertEquals("SUCCESS", cs.toCompletableFuture().get());
+        assertEquals(1, threadInfo.size());
+        assertEquals("CXF-Thread", threadInfo.get(0));
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services