Skip to content

Instantly share code, notes, and snippets.

View dhaval201279's full-sized avatar
🎯
Focusing

Dhaval Shah dhaval201279

🎯
Focusing
View GitHub Profile
@dhaval201279
dhaval201279 / HttpClientConfigWithConnPool.java
Last active May 17, 2020 12:00
HttpClient configuration for ConnectionPool using PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager poolingConnectionManager() {
PoolingHttpClientConnectionManager poolingConnectionManager =
new PoolingHttpClientConnectionManager(getConnectionSocketFactoryRegistry());
poolingConnectionManager.setMaxTotal(MAX_CONNECTIONS);
poolingConnectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE_CONNECTION);
poolingConnectionManager.setValidateAfterInactivity(VALIDATE_AFTER_INACTIVITY_IN_MILLIS);
return poolingConnectionManager;
}
@dhaval201279
dhaval201279 / HttpClientConfigWithKeepAliveStrategy.java
Last active May 17, 2020 12:01
Http client config with custom defined keep alive strategy
private ConnectionKeepAliveStrategy connectionKeepAliveStrategy() {
return new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
long keepAliveDuration = super.getKeepAliveDuration(response, context);
if (keepAliveDuration < 0) {
keepAliveDuration = DEFAULT_KEEP_ALIVE_TIME_MILLIS;
} else if (keepAliveDuration > MAX_KEEP_ALIVE_TIME_MILLIS) {
keepAliveDuration = MAX_KEEP_ALIVE_TIME_MILLIS;
}
@dhaval201279
dhaval201279 / HttpClientConfigWithIdleAndExpiredConnMgmt.java
Created May 17, 2020 11:09
Http client config with house keeping of idle and expired connections
public Runnable idleAndExpiredConnectionProcessor(final PoolingHttpClientConnectionManager connectionManager) {
return new Runnable() {
@Override
@Scheduled(fixedDelay = 20000)
public void run() {
try {
if (connectionManager != null) {
connectionManager.closeExpiredConnections();
connectionManager.closeIdleConnections(IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
}
@dhaval201279
dhaval201279 / CardServiceWithRetry.java
Last active May 19, 2020 04:56
Implementing retryable RestClient using Spring Retry
@Retryable(value = {RemoteServiceUnavailableException.class}, maxAttempts = 8,
backoff = @Backoff(delay = 1000, multiplier = 2), label = "generate-alias-retry-label")
String generateAlias(String cardNo);
@Recover
String fallbackForGenerateAlias(Throwable th, String cardNo);
@dhaval201279
dhaval201279 / HttpClientStatsExtractor.java
Created May 17, 2020 11:48
Http stats and stats per route extractor
public Runnable connectionPoolMetricsLogger(final PoolingHttpClientConnectionManager connectionManager) {
return new Runnable() {
@Override
@Scheduled(fixedDelay = 30000)
public void run() {
final StringBuilder buffer = new StringBuilder();
try {
if (connectionManager != null) {
final PoolStats totalPoolStats = connectionManager.getTotalStats();
log.info(" ** HTTP Client Connection Pool Stats : Available = {}, Leased = {}, Pending = {}, Max = {} **",