Konstantin Kulagin
Konstantin Kulagin

Reputation: 724

How to configure rest client in quarkus microprofile case

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)?

Upvotes: 4

Views: 8371

Answers (2)

Konstantin Kulagin
Konstantin Kulagin

Reputation: 724

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)

  1. 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

  2. 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

  1. Profit

Now native support: After steps above:

  1. 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
       }
     ]
  1. 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$"
    }
  ]
}
  1. register both files in application.yaml:
quarkus:
  native:
    additional-build-args: -H:ResourceConfigurationFiles=resources-config.json, -H:ReflectionConfigurationFiles=reflection-config.json
  1. Native profit

Have fun

Upvotes: 2

Ken
Ken

Reputation: 835

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?

Upvotes: 5

Related Questions