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 / 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 / HttpClientConfig1.java
Last active May 18, 2020 14:57
Configuring HttpClient using RequestConfig
private RequestConfig prepareHttpClientRequestConfig() {
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT)
.build();
return requestConfig;
}
@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 / 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 / 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 = {} **",
@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 / ReservationCachingTest1.java
Last active March 11, 2018 05:42
1st version of ReservationCachingTest
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;
@dhaval201279
dhaval201279 / ReservationService4.java
Created March 11, 2018 05:40
4th version ReservationService - with @Cacheable
@Service
public class ReservationService {
private ReservationRepository reservationRepository;
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
@Cacheable("reservation")
@dhaval201279
dhaval201279 / BootifulTddApplication1.java
Created March 11, 2018 05:37
BootifulTddApplication - with caching annotation
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 March 11, 2018 05:33
final version of ReservationEndToEndTest
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ReservationEndToEndTest {
@Autowired
TestRestTemplate testRestTemplate;
@Test
public void getReservation_shouldReturnReservationDetails() {
// Arrange