Search code examples
quarkusmicroprofile

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


Solution

  • 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