Skip to content

Commit 7ff7e84

Browse files
committedOct 28, 2019
Experiment 1 : Calling thread propagation in async() and rx() calls
No troubles here, this is exactly what we need
1 parent f4ccbb3 commit 7ff7e84

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) [2019] Payara Foundation and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.client;
42+
43+
import org.glassfish.jersey.client.spi.PreInvocationInterceptor;
44+
import org.junit.Test;
45+
46+
import javax.ws.rs.client.Client;
47+
import javax.ws.rs.client.ClientBuilder;
48+
import javax.ws.rs.client.ClientRequestContext;
49+
import javax.ws.rs.client.ClientRequestFilter;
50+
import javax.ws.rs.core.Response;
51+
import java.io.IOException;
52+
import java.util.concurrent.CompletionStage;
53+
import java.util.concurrent.ExecutionException;
54+
import java.util.concurrent.Future;
55+
import java.util.concurrent.TimeUnit;
56+
import java.util.concurrent.TimeoutException;
57+
58+
import static org.junit.Assert.assertEquals;
59+
60+
public class PreInvocationUsageTest {
61+
static final String KEY = "thread_name";
62+
private static final String URL = "http://localhost:8080";
63+
64+
@Test
65+
public void contextPropagatesFromCallerToExecutorInAsync() throws InterruptedException, ExecutionException, TimeoutException {
66+
String thisThread = Thread.currentThread().getName();
67+
Client client = ClientBuilder.newBuilder()
68+
.register(ContextPropagator.class)
69+
.register(new OtherThreadChecker(thisThread))
70+
.build();
71+
72+
Future<Response> response = client.target(URL).request().async().get();
73+
assertEquals(200, response.get(1, TimeUnit.SECONDS).getStatus());
74+
75+
}
76+
77+
@Test
78+
public void contextPropagatesFromCallerToExecutorInRx() throws InterruptedException, ExecutionException, TimeoutException {
79+
String thisThread = Thread.currentThread().getName();
80+
Client client = ClientBuilder.newBuilder()
81+
.register(ContextPropagator.class)
82+
.register(new OtherThreadChecker(thisThread))
83+
.build();
84+
85+
CompletionStage<Response> response = client.target(URL).request().rx().get();
86+
assertEquals(200, response.toCompletableFuture().get(1, TimeUnit.SECONDS).getStatus());
87+
88+
}
89+
90+
static class ContextPropagator implements PreInvocationInterceptor {
91+
92+
@Override
93+
public void beforeRequest(ClientRequestContext requestContext) {
94+
requestContext.setProperty(KEY, Thread.currentThread().getName());
95+
}
96+
}
97+
98+
static class OtherThreadChecker implements ClientRequestFilter {
99+
100+
public OtherThreadChecker(String callingThread) {
101+
this.callingThread = callingThread;
102+
}
103+
104+
private String callingThread;
105+
106+
@Override
107+
public void filter(ClientRequestContext requestContext) throws IOException {
108+
String currentThread = Thread.currentThread().getName();
109+
System.out.println("Executing in thread " + currentThread);
110+
if (!currentThread.equals(callingThread)) {
111+
String context = (String) requestContext.getProperty(KEY);
112+
System.out.println("Tracing context is " + context);
113+
if (callingThread.equals(context)) {
114+
requestContext.abortWith(Response.ok().build());
115+
} else {
116+
requestContext.abortWith(Response.serverError().entity("unexpected request in context: " + context).build());
117+
}
118+
} else {
119+
requestContext.abortWith(Response.serverError().entity("Request executed at calling thread").build());
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)