-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jersey JAX-RS client context propagation #3762
Signed-off-by: Daniel Kec <[email protected]>
- Loading branch information
Showing
7 changed files
with
402 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
jersey/client/src/main/java/io/helidon/jersey/client/ClientBuilderListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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 io.helidon.jersey.client; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.ws.rs.Priorities; | ||
import jakarta.ws.rs.client.ClientBuilder; | ||
|
||
/** | ||
* Listen to the Jersey's client builder being invoked and register Helidon executor provider. | ||
*/ | ||
// Lowest priority gets invoked first and first registered provider wins, | ||
// user's custom ClientBuilderListeners needs to be able to override this | ||
@Priority(Priorities.USER + 100) | ||
public class ClientBuilderListener implements org.glassfish.jersey.client.spi.ClientBuilderListener { | ||
|
||
private static final String USE_CONTEXT_AWARE_EXEC_PROVIDER = "io.helidon.jersey.client.useContextAwareExecutorProvider"; | ||
private static Boolean useProvider; | ||
|
||
@Override | ||
public void onNewBuilder(ClientBuilder builder) { | ||
if (useContextAwareProvider()) { | ||
builder.register(ExecutorProvider.class); | ||
builder.register(ScheduledExecutorProvider.class); | ||
} | ||
} | ||
|
||
private boolean useContextAwareProvider() { | ||
if (useProvider == null) { | ||
useProvider = Boolean.parseBoolean(System.getProperty(USE_CONTEXT_AWARE_EXEC_PROVIDER, "true")); | ||
} | ||
return useProvider; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
jersey/client/src/main/java/io/helidon/jersey/client/ExecutorProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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 io.helidon.jersey.client; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.logging.Logger; | ||
|
||
import io.helidon.common.context.Contexts; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Named; | ||
import org.glassfish.jersey.client.ClientAsyncExecutor; | ||
import org.glassfish.jersey.client.internal.LocalizationMessages; | ||
import org.glassfish.jersey.internal.util.collection.LazyValue; | ||
import org.glassfish.jersey.internal.util.collection.Value; | ||
import org.glassfish.jersey.internal.util.collection.Values; | ||
import org.glassfish.jersey.spi.ThreadPoolExecutorProvider; | ||
|
||
/** | ||
* Wraps default executor to enable Helidon context propagation for Jersey async calls. | ||
*/ | ||
@ClientAsyncExecutor | ||
public class ExecutorProvider extends ThreadPoolExecutorProvider { | ||
|
||
static final String THREAD_NAME_PREFIX = "helidon-client-async-executor"; | ||
private static final Logger LOGGER = Logger.getLogger(ExecutorProvider.class.getName()); | ||
private final LazyValue<Integer> asyncThreadPoolSize; | ||
|
||
/** | ||
* Create new instance of the context aware executor provider. | ||
* @param poolSize Maximum pool size | ||
*/ | ||
@Inject | ||
public ExecutorProvider(@Named("ClientAsyncThreadPoolSize") final Optional<Integer> poolSize) { | ||
super(THREAD_NAME_PREFIX); | ||
|
||
this.asyncThreadPoolSize = Values.lazy((Value<Integer>) () -> { | ||
if (poolSize.filter(i -> i > 0).isEmpty()) { | ||
LOGGER.config(LocalizationMessages.IGNORED_ASYNC_THREADPOOL_SIZE(poolSize.orElse(0))); | ||
// using default | ||
return Integer.MAX_VALUE; | ||
} else { | ||
LOGGER.config(LocalizationMessages.USING_FIXED_ASYNC_THREADPOOL(poolSize.get())); | ||
return poolSize.get(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected int getMaximumPoolSize() { | ||
return asyncThreadPoolSize.get(); | ||
} | ||
|
||
@Override | ||
protected int getCorePoolSize() { | ||
// Mimicking the Executors.newCachedThreadPool and newFixedThreadPool configuration values. | ||
final int maximumPoolSize = getMaximumPoolSize(); | ||
if (maximumPoolSize != Integer.MAX_VALUE) { | ||
return maximumPoolSize; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
@Override | ||
public ExecutorService getExecutorService() { | ||
return Contexts.wrap(super.getExecutorService()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
jersey/client/src/main/java/io/helidon/jersey/client/ScheduledExecutorProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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 io.helidon.jersey.client; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
import io.helidon.common.context.Contexts; | ||
|
||
import org.glassfish.jersey.client.ClientBackgroundScheduler; | ||
import org.glassfish.jersey.spi.ScheduledThreadPoolExecutorProvider; | ||
|
||
/** | ||
* Wraps default executor to enable Helidon context propagation for Jersey async calls. | ||
*/ | ||
@ClientBackgroundScheduler | ||
public class ScheduledExecutorProvider extends ScheduledThreadPoolExecutorProvider { | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
public ScheduledExecutorProvider() { | ||
super("helidon-client-background-scheduler"); | ||
} | ||
|
||
@Override | ||
protected int getCorePoolSize() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public ScheduledExecutorService getExecutorService() { | ||
return Contexts.wrap(super.getExecutorService()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
jersey/client/src/main/java/io/helidon/jersey/client/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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. | ||
* | ||
*/ | ||
|
||
/** | ||
* Jersey JAX-RS client with context propagation support. | ||
*/ | ||
package io.helidon.jersey.client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.