When using Quarkus microprofile as a rest client, how can I configure underlying HttpClient?Like number of retries, connection pool size per host and so on?Also is it possible to force client restart somehow (so connections pool will be restarted)?
Best Answer
https://download.eclipse.org/microprofile/microprofile-rest-client-2.0-RC2/microprofile-rest-client-2.0-RC2.html#_configuration_keys outlines the full set of configuration keys that can be used.
The ones you're looking for are:
{packageName}.{interfaceName}/mp-rest/connectTimeout{packageName}.{interfaceName}/mp-rest/readTimeout
The RestClientBuilder
also has methods for setting those properties if you're using the programmatic API instead of the CDI approach.
I'm not aware of any means of restarting the underlying HTTP client connection pool. What would be the use case for such a situation that doesn't require the whole application to be restarted?
So... After a lot of digging, here is a solution I've found so far. It is not obvious apparently:
To make it work in pure Java (no native)
Under resources/META-INF/services directory add file named org.eclipse.microprofile.rest.client.spi.RestClientBuilderListener containing class name of your implementation of RestClientBuilderListener interface. For example my.test.MyBuilderListener. This will allow ServiceLocator to execute your listener
Refer a property you want to modify from ResteasyClientBuilder, for example to set your custom value to connectionTTL code will looks like this:
public void onNewBuilder(RestClientBuilder builder) {log.info("Changing TTL for connections");builder.property("resteasy.connectionTTL", List.of(2L, TimeUnit.SECONDS));}
Ie. add a resteasy. prefix to a property name
- Profit
Now native support:After steps above:
- Set both MyBuildListener and ResteasyClientBuilder available for reflection by creating a file reflection-config.json:
[{"name": "org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder","allDeclaredConstructors": true,"allPublicConstructors": true,"allDeclaredMethods": true,"allPublicMethods": true,"allDeclaredFields": true,"allPublicFields": true}, {"name": "my.test.MyBuilderListener","allDeclaredConstructors": true,"allPublicConstructors": true,"allDeclaredMethods": true,"allPublicMethods": true,"allDeclaredFields": true,"allPublicFields": true}]
- Add service registration file to resources. Create a file named resources-config.json with content
{"resources": [{"pattern": "META-INF/services/org\\.eclipse\\.microprofile\\.rest\\.client\\.spi\\.RestClientBuilderListener$"}]}
- register both files in application.yaml:
quarkus:native:additional-build-args: -H:ResourceConfigurationFiles=resources-config.json, -H:ReflectionConfigurationFiles=reflection-config.json
- Native profit
Have fun