Last active
July 23, 2020 11:21
-
-
Save theotherian/6205639 to your computer and use it in GitHub Desktop.
configuring Jersey 2 client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import org.apache.http.HttpHost; | |
import org.apache.http.conn.routing.HttpRoute; | |
import org.apache.http.impl.conn.PoolingClientConnectionManager; | |
import org.glassfish.jersey.apache.connector.ApacheClientProperties; | |
import org.glassfish.jersey.apache.connector.ApacheConnector; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.client.ClientProperties; | |
import org.glassfish.jersey.jackson.JacksonFeature; | |
public final class ClientFactory { | |
public static Client create() { | |
ClientConfig clientConfig = new ClientConfig(); | |
// values are in milliseconds | |
clientConfig.property(ClientProperties.READ_TIMEOUT, 2000); | |
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 500); | |
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); | |
connectionManager.setMaxTotal(100); | |
connectionManager.setDefaultMaxPerRoute(20); | |
connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost")), 40); | |
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager); | |
ApacheConnector connector = new ApacheConnector(clientConfig); | |
clientConfig.connector(connector); | |
Client client = ClientBuilder.newClient(clientConfig); | |
client.register(JacksonFeature.class); | |
return client; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
public final class ClientFactory { | |
public static Client create() { | |
ClientConfig clientConfig = ...; | |
... | |
Client client = ClientBuilder.newClient(clientConfig); | |
client.register(JacksonFeature.class); | |
return client; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Configuring Jersey 2 client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.client.Client; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.client.ClientProperties; | |
public final class ClientFactory { | |
public static Client create() { | |
ClientConfig clientConfig = new ClientConfig(); | |
// values are in milliseconds | |
clientConfig.property(ClientProperties.READ_TIMEOUT, 2000); | |
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 500); | |
... | |
return client; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.client.Client; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.apache.http.impl.conn.PoolingClientConnectionManager; | |
import org.glassfish.jersey.apache.connector.ApacheConnector; | |
import org.glassfish.jersey.apache.connector.ApacheClientProperties; | |
public final class ClientFactory { | |
public static Client create() { | |
ClientConfig clientConfig = ...; | |
PoolingClientConnectionManager connectionManager = ...; | |
// tell the config about the connection manager | |
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager); | |
// tell the connector about the config, which includes the connection manager and timeouts | |
ApacheConnector connector = new ApacheConnector(clientConfig); | |
// tell the config about the connector | |
clientConfig.connector(connector); | |
... | |
return client; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.client.Client; | |
import org.apache.http.impl.conn.PoolingClientConnectionManager; | |
public final class ClientFactory { | |
public static Client create() { | |
... | |
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); | |
connectionManager.setMaxTotal(100); | |
connectionManager.setDefaultMaxPerRoute(20); | |
... | |
return client; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost")), 40); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
... | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.glassfish.jersey</groupId> | |
<artifactId>jersey-bom</artifactId> | |
<version>${jersey.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-json-jackson</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.core</groupId> | |
<artifactId>jersey-client</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.connectors</groupId> | |
<artifactId>jersey-apache-connector</artifactId> | |
</dependency> | |
</dependencies> | |
<properties> | |
<jersey.version>2.1</jersey.version> | |
</properties> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
things have changed and now we need to do :
instead of "new ApacheConnector".
Btw, thanks for the tip !