set Timeout for jax rs Client

2019-03-02 02:48发布

I want to set a connection-timeout for a jax rs client.

ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);

When i create the client directly with a ClientConfig, i get the following message:

Unable to find a MessageBodyReader of content-type application/xml and type class ..

Thats what i tried. But it wont work, if i try to connect to a wrong host, it takes 20 seconds until i get a ProcessingException. I also tried to set the property on the webTarget, it doesnt help either.

WebTarget target = client.target(protocol + "://" + host + ":" + port);
target = target.path(APPLICATION_PATH + REST_PATH);
target.property(ClientProperties.CONNECT_TIMEOUT, 1);
target.property(ClientProperties.READ_TIMEOUT, 1);

0条回答
登录 后发表回答