Jersey 2.7 - setting retry handler

2019-03-29 09:24发布

问题:

I would like to set a retry handler for Jersey client utilizing ApacheConnector. I wish to do it, because I want it to retry on timeout (my HAProxy will switch it to another server). I have no clue how to do this in Jersey 2.7.

Example code:

public static void Example() {

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.connectorProvider(new ApacheConnectorProvider());
    clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, new PoolingHttpClientConnectionManager());

    RequestConfig reqConfig = RequestConfig.custom().build(); 
    clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, reqConfig);

    Client client = ClientBuilder.newClient(clientConfig);
    WebTarget apiTarget = client.target("http://127.0.0.1/rest");
    System.out.println(apiTarget.path(ApiConstant.PING)
            .path(ApiConstant.PING1)
            .request(MediaType.TEXT_PLAIN)
            .get(String.class));
}

How, using this code, I can set a retry handler to send request again if server responds with an error? Is it possible?