Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncInvocationinverceptors not properly created for each request #4272

Merged
merged 5 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
*/
class ExecutorServiceWrapper implements ExecutorService {

static final ThreadLocal<List<AsyncInvocationInterceptor>> asyncInterceptors = new ThreadLocal<>();

private final ExecutorService wrapped;
private final List<AsyncInvocationInterceptor> asyncInterceptors;

ExecutorServiceWrapper(ExecutorService wrapped,
List<AsyncInvocationInterceptor> asyncInterceptors) {
ExecutorServiceWrapper(ExecutorService wrapped) {
this.wrapped = wrapped;
this.asyncInterceptors = asyncInterceptors;
}

@Override
Expand Down Expand Up @@ -112,15 +111,21 @@ public void execute(Runnable command) {
}

private <T> Callable<T> wrap(Callable<T> task) {
List<AsyncInvocationInterceptor> asyncInvocationInterceptors = asyncInterceptors.get();
return () -> {
asyncInterceptors.forEach(AsyncInvocationInterceptor::applyContext);
if (asyncInvocationInterceptors != null) {
asyncInvocationInterceptors.forEach(AsyncInvocationInterceptor::applyContext);
Verdent marked this conversation as resolved.
Show resolved Hide resolved
}
return task.call();
};
}

private Runnable wrap(Runnable task) {
List<AsyncInvocationInterceptor> asyncInvocationInterceptors = asyncInterceptors.get();
return () -> {
asyncInterceptors.forEach(AsyncInvocationInterceptor::applyContext);
if (asyncInvocationInterceptors != null) {
asyncInvocationInterceptors.forEach(AsyncInvocationInterceptor::applyContext);
}
task.run();
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptor;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory;
import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.glassfish.jersey.client.inject.ParameterUpdater;
Expand All @@ -64,7 +65,7 @@ class InterfaceModel {
private final CreationalContext<?> creationalContext;

private final List<ClientHeaderParamModel> clientHeaders;
private final List<AsyncInvocationInterceptor> asyncInterceptors;
private final List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories;
private final Set<ResponseExceptionMapper> responseExceptionMappers;
private final Set<ParamConverterProvider> paramConverterProviders;
private final Set<Annotation> interceptorAnnotations;
Expand All @@ -73,23 +74,23 @@ class InterfaceModel {
/**
* Creates new model based on interface class. Interface is parsed according to specific annotations.
*
* @param restClientClass interface class
* @param responseExceptionMappers registered exception mappers
* @param paramConverterProviders registered parameter providers
* @param asyncInterceptors async interceptors
* @param restClientClass interface class
* @param responseExceptionMappers registered exception mappers
* @param paramConverterProviders registered parameter providers
* @param asyncInterceptorFactories async interceptor factories
* @param injectionManager
* @return new model instance
*/
static InterfaceModel from(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
List<AsyncInvocationInterceptor> asyncInterceptors,
List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories,
InjectionManager injectionManager,
BeanManager beanManager) {
return new Builder(restClientClass,
responseExceptionMappers,
paramConverterProviders,
asyncInterceptors,
asyncInterceptorFactories,
injectionManager,
beanManager).build();
}
Expand All @@ -106,7 +107,7 @@ private InterfaceModel(Builder builder) {
this.paramConverterProviders = builder.paramConverterProviders;
this.interceptorAnnotations = builder.interceptorAnnotations;
this.creationalContext = builder.creationalContext;
this.asyncInterceptors = builder.asyncInterceptors;
this.asyncInterceptorFactories = builder.asyncInterceptorFactories;
this.beanManager = builder.beanManager;
}

Expand Down Expand Up @@ -169,8 +170,8 @@ List<ClientHeaderParamModel> getClientHeaders() {
*
* @return registered async interceptors
*/
List<AsyncInvocationInterceptor> getAsyncInterceptors() {
return asyncInterceptors;
List<AsyncInvocationInterceptorFactory> getAsyncInterceptorFactories() {
return asyncInterceptorFactories;
}

/**
Expand Down Expand Up @@ -251,22 +252,22 @@ private static class Builder {
private ClientHeadersFactory clientHeadersFactory;
private CreationalContext<?> creationalContext;
private List<ClientHeaderParamModel> clientHeaders;
private List<AsyncInvocationInterceptor> asyncInterceptors;
private List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories;
private Set<ResponseExceptionMapper> responseExceptionMappers;
private Set<ParamConverterProvider> paramConverterProviders;
private Set<Annotation> interceptorAnnotations;

private Builder(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
List<AsyncInvocationInterceptor> asyncInterceptors,
List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories,
InjectionManager injectionManager,
BeanManager beanManager) {
this.injectionManager = injectionManager;
this.restClientClass = restClientClass;
this.responseExceptionMappers = responseExceptionMappers;
this.paramConverterProviders = paramConverterProviders;
this.asyncInterceptors = asyncInterceptors;
this.asyncInterceptorFactories = asyncInterceptorFactories;
this.beanManager = beanManager;
filterAllInterceptorAnnotations();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.eclipse.microprofile.rest.client.RestClientDefinitionException;
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptor;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;

/**
Expand Down Expand Up @@ -121,7 +122,7 @@ private MethodModel(Builder builder) {
subResourceModel = RestClientModel.from(returnType.getRawType(),
interfaceModel.getResponseExceptionMappers(),
interfaceModel.getParamConverterProviders(),
interfaceModel.getAsyncInterceptors(),
interfaceModel.getAsyncInterceptorFactories(),
interfaceModel.getInjectionManager(),
interfaceModel.getBeanManager());
} else {
Expand Down Expand Up @@ -243,6 +244,14 @@ private CompletableFuture asynchronousCall(Invocation.Builder builder,
Object entity,
Method method,
MultivaluedMap<String, Object> customHeaders) {

//AsyncInterceptors initialization
List<AsyncInvocationInterceptor> asyncInterceptors = interfaceModel.getAsyncInterceptorFactories().stream()
.map(AsyncInvocationInterceptorFactory::newInterceptor)
.collect(Collectors.toList());
asyncInterceptors.forEach(AsyncInvocationInterceptor::prepareContext);
ExecutorServiceWrapper.asyncInterceptors.set(asyncInterceptors);

CompletableFuture<Object> result = new CompletableFuture<>();
Future<Response> theFuture;
if (entity != null
Expand All @@ -255,7 +264,7 @@ private CompletableFuture asynchronousCall(Invocation.Builder builder,

CompletableFuture<Response> completableFuture = (CompletableFuture<Response>) theFuture;
completableFuture.thenAccept(response -> {
interfaceModel.getAsyncInterceptors().forEach(AsyncInvocationInterceptor::removeContext);
asyncInterceptors.forEach(AsyncInvocationInterceptor::removeContext);
try {
evaluateResponse(response, method);
if (returnType.getType().equals(Void.class)) {
Expand All @@ -269,7 +278,7 @@ private CompletableFuture asynchronousCall(Invocation.Builder builder,
result.completeExceptionally(e);
}
}).exceptionally(throwable -> {
interfaceModel.getAsyncInterceptors().forEach(AsyncInvocationInterceptor::removeContext);
asyncInterceptors.forEach(AsyncInvocationInterceptor::removeContext);
result.completeExceptionally(throwable);
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,7 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
//We need to check first if default exception mapper was not disabled by property on builder.
registerExceptionMapper();

//AsyncInterceptors initialization
List<AsyncInvocationInterceptor> asyncInterceptors = asyncInterceptorFactories.stream()
.map(AsyncInvocationInterceptorFactory::newInterceptor)
.collect(Collectors.toList());
asyncInterceptors.forEach(AsyncInvocationInterceptor::prepareContext);

clientBuilder.executorService(new ExecutorServiceWrapper(executorService.get(), asyncInterceptors));
clientBuilder.executorService(new ExecutorServiceWrapper(executorService.get()));

if (null != sslContext) {
clientBuilder.sslContext(sslContext);
Expand All @@ -187,7 +181,7 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
RestClientModel restClientModel = RestClientModel.from(interfaceClass,
responseExceptionMappers,
paramConverterProviders,
asyncInterceptors,
asyncInterceptorFactories,
injectionManagerExposer.injectionManager,
CdiUtil.getBeanManager());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.ext.ParamConverterProvider;

import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptor;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.glassfish.jersey.internal.inject.InjectionManager;

Expand Down Expand Up @@ -56,13 +56,13 @@ class RestClientModel {
static RestClientModel from(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
List<AsyncInvocationInterceptor> asyncInterceptors,
List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories,
InjectionManager injectionManager,
BeanManager beanManager) {
InterfaceModel interfaceModel = InterfaceModel.from(restClientClass,
responseExceptionMappers,
paramConverterProviders,
asyncInterceptors,
asyncInterceptorFactories,
injectionManager,
beanManager);
return new Builder()
Expand Down