Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Dhaval Shah dhaval201279

🎯
Focusing
View GitHub Profile
@dhaval201279
dhaval201279 / HttpClientStatsExtractor.java
Created May 17, 2020
Http stats and stats per route extractor
View HttpClientStatsExtractor.java
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 = {} **",
@dhaval201279
dhaval201279 / CardServiceWithRetry.java
Last active May 19, 2020
Implementing retryable RestClient using Spring Retry
View CardServiceWithRetry.java
@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 / HttpClientConfigWithIdleAndExpiredConnMgmt.java
Created May 17, 2020
Http client config with house keeping of idle and expired connections
View HttpClientConfigWithIdleAndExpiredConnMgmt.java
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 / HttpClientConfigWithKeepAliveStrategy.java
Last active May 17, 2020
Http client config with custom defined keep alive strategy
View HttpClientConfigWithKeepAliveStrategy.java
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 / HttpClientConfigWithConnPool.java
Last active May 17, 2020
HttpClient configuration for ConnectionPool using PoolingHttpClientConnectionManager
View HttpClientConfigWithConnPool.java
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 / HttpClientConfig1.java
Last active May 18, 2020
Configuring HttpClient using RequestConfig
View HttpClientConfig1.java
private RequestConfig prepareHttpClientRequestConfig() {
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT)
.build();
return requestConfig;
}
@dhaval201279
dhaval201279 / ReservationService4.java
Created Mar 11, 2018
4th version ReservationService - with @Cacheable
View ReservationService4.java
@Service
public class ReservationService {
private ReservationRepository reservationRepository;
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
@Cacheable("reservation")
@dhaval201279
dhaval201279 / BootifulTddApplication1.java
Created Mar 11, 2018
BootifulTddApplication - with caching annotation
View BootifulTddApplication1.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class BootifulTddApplication {
public static void main(String[] args) {
SpringApplication.run(BootifulTddApplication.class, args);
@dhaval201279
dhaval201279 / ReservationEndToEndTest2.java
Last active Mar 11, 2018
final version of ReservationEndToEndTest
View ReservationEndToEndTest2.java
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ReservationEndToEndTest {
@Autowired
TestRestTemplate testRestTemplate;
@Test
public void getReservation_shouldReturnReservationDetails() {
// Arrange
@dhaval201279
dhaval201279 / ReservationCachingTest1.java
Last active Mar 11, 2018
1st version of ReservationCachingTest
View ReservationCachingTest1.java
package com.its.reservation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;