I have implemented PoolingHttpClientConnectionManager. How can I make client reusable, so other class can reuse it? Should I put it in a Service?
public class MyClass() {
private static CloseableHttpClient client;
private static final CookieStore commonCookieStore = new BasicCookieStore();
private static final ScheduledExecutorService cleanUpThread = ScheduledExecutorService.wrap(
new ScheduledThreadPoolExecutor(1), "connection-pool-cleanup-thread-%d");
public MyClass() {
try {
String keystoreId = //getKeyStoreId()
if (client == null) {
if (StringUtils.isNotEmpty(keystoreId)) {
SSLConnectionSocketFactory sslConSocFactory = new SSLConnectionSocketFactory(TLSSocketFactoryCache.getInstance().get(keystoreId),
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConSocFactory).build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000)
.setConnectionRequestTimeout(1000)
.setSocketTimeout(1000)
.build();
ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
// Honor 'keep-alive' header
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch(NumberFormatException ignore) {
// ignore
}
}
}
try {
return 60000;
} catch(NumberFormatException ignore) {
// ignore
}
// otherwise keep alive for 60 seconds
return 60 * 1000;
}
};
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setDefaultMaxPerRoute(200);
connectionManager.setMaxTotal(200);
connectionManager.setValidateAfterInactivity(60000);
client = HttpClients.custom().setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.setDefaultRequestConfig(requestConfig)
.setKeepAliveStrategy(keepAliveStrategy)
.setConnectionReuseStrategy(new DefaultConnectionReuseStrategy())
.setUserTokenHandler(context -> null)
.setMaxConnTotal(200)
.setMaxConnPerRoute(200)
.setConnectionTimeToLive(60000, TimeUnit.MILLISECONDS)
.build();
int closeIdle = 60000;
cleanUpThread.scheduleAtFixedRate(() -> {
try {
connectionManager.closeExpiredConnections();
connectionManager.closeIdleConnections(closeIdle, TimeUnit.MILLISECONDS);
} catch (Exception ex) {
LOGGER.error("Exception on SGW clean up thread", ex);
}
}, closeIdle, closeIdle, TimeUnit.MILLISECONDS);
} else {
LOGGER.error("Error! No keystore specified");
}
}
} catch (Exception e) {
LOGGER.error("Encountered exception during retrieval of SSL Context", e);
}
}
}
Related
I am trying to figure out NIO in Java doing some simple client-server project.
The case is I have to concurrent clients in cached thread pool executor, who are communicating with single-threaded server using non-blocking NIO channels.
The problem is that last client cannot receive last server's sent message. It locks in infinite loop waiting for upcoming data.
ClientTask class:
public class ClientTask extends FutureTask<String> {
private Client client;
private List<String> reqList; // requests list (without last and first)
private boolean showRes; // print request results
public ClientTask(Client client, List<String> reqList, boolean showRes) {
super(() -> ClientTask.getLogWhenArrives(client, reqList, showRes));
this.client = client;
this.reqList = reqList;
this.showRes = showRes;
}
public static ClientTask create(Client c, List<String> reqList, boolean showRes) {
return new ClientTask(c, reqList, showRes);
}
private static String getLogWhenArrives(Client client, List<String> reqList, boolean showRes) {
client.connect();
String response = client.send("login " + client.getId());
if (showRes) System.out.println(response);
for (String req : reqList) {
response = client.send(req);
if (showRes) System.out.println(response);
}
String responseLog = client.send("bye and log transfer");
client.close();
return responseLog;
}
}
Client send():
public String send(String req) {
ByteBuffer reqBuffer = ByteBuffer.wrap((req + END).getBytes());
try {
channel.write(reqBuffer);
} catch (IOException e) {
e.printStackTrace();
}
return receive();
}
Client receive()
public String receive() {
StringBuilder result = new StringBuilder();
try {
inBuff.clear();
readLoop:
while (true) { // THIS LOOP WON'T END
int n = channel.read(inBuff);
if (n == -1) {
break;
}
if (n > 0) {
inBuff.flip();
CharBuffer cb = charset.decode(inBuff);
while (cb.hasRemaining()) {
char c = cb.get();
if (c == END.charAt(0)) {
break readLoop;
}
result.append(c);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
Main:
public class Main {
public static void main(String[] args) throws Exception {
String fileName = System.getProperty("user.home") + "/PassTimeServerOptions.yaml";
Options opts = Tools.createOptionsFromYaml(fileName);
String host = opts.getHost();
int port = opts.getPort();
boolean concur = opts.isConcurMode();
boolean showRes = opts.isShowSendRes();
Map<String, List<String>> clRequests = opts.getClientsMap();
ExecutorService es = Executors.newCachedThreadPool();
List<ClientTask> ctasks = new ArrayList<>();
List<String> clogs = new ArrayList<>();
Server s = new Server(host, port);
s.startServer();
// start clients
clRequests.forEach( (id, reqList) -> {
Client c = new Client(host, port, id);
if (concur) {
ClientTask ctask = ClientTask.create(c, reqList, showRes);
ctasks.add(ctask);
es.execute(ctask);
}
});
if (concur) {
ctasks.forEach( task -> {
try {
String log = task.get();
clogs.add(log);
} catch (InterruptedException | ExecutionException exc) {
System.out.println(exc);
}
});
clogs.forEach( System.out::println);
es.shutdown();
}
s.stopServer();
System.out.println("\n=== Server log ===");
System.out.println(s.getServerLog());
}
}
Server is sending all the info and channels are open and connected.
I'm using apache httpClient lib in my java project.
I got an error when I didn't clean the HttpClient
Timeout waiting for connection
public class RoutingUrlHttpClient implements IRoutingUrlHttpClient {
final static Logger logger = Logger.getLogger(RoutingUrlHttpClient.class);
private IRoutingResponseFromStringFetcher routingResponseParser;
private IRoutingResponseConverter routingResponseConverter;
private IUrlUtils urlUtils;
private CloseableHttpClient client;
private ILogUtils logUtils;
#Inject
#Singleton
public RoutingUrlHttpClient(IRoutingResponseFromStringFetcher routingResponseParser,
IRoutingResponseConverter routingResponseConverter, IUrlUtils urlUtils,
ILogUtils logUtils) {
this.routingResponseParser = routingResponseParser;
this.routingResponseConverter = routingResponseConverter;
this.urlUtils = urlUtils;
this.logUtils = logUtils;
RequestConfig requestConfig = RequestConfig.custom()
//time till handshake
.setConnectTimeout(40 * 1000)
//happens when you have a pool of connections and they are all busy, not allowing the connection
// manager to give you one connection to make the request.
.setConnectionRequestTimeout(40 * 1000)
//time till response
.setSocketTimeout(40 * 1000)
.build();
client = HttpClientBuilder
.create()
.setDefaultRequestConfig(requestConfig)
.build();
}
// public CompleteRoutingResponseDtoWrapper sendAndReturnDtoWrapper(String routingRequestUrl) {
// CompleteRoutingResponseDtoWrapper completeRoutingResponseDtoWrapper = sendRoutingRequestString
// (routingRequestUrl);
// completeRoutingResponseDtoWrapper.requestUrl = routingRequestUrl;
// return completeRoutingResponseDtoWrapper;
// }
#Override
public CompleteRoutingResponseLong sendRoutingRequestStringWithFullResponse(String routingRequestUrl) {
CompleteRoutingResponseDtoWrapper completeRoutingResponseDtoWrapper =
sendRoutingRequestString(routingRequestUrl);
completeRoutingResponseDtoWrapper.requestUrl = routingRequestUrl;
return routingResponseConverter.toCompleteRoutingResponseFull(completeRoutingResponseDtoWrapper);
}
private CompleteRoutingResponseDtoWrapper sendRoutingRequestString(String routingRequestUrl) {
return sendRoutingRequestString(Constants.NUM_OF_RETRIES, routingRequestUrl);
}
private CompleteRoutingResponseDtoWrapper sendRoutingRequestString(int numberOfTriesLeft,
String routingRequestUrl) {
routingRequestUrl = urlUtils.getHttpUrl(routingRequestUrl);
CompleteRoutingResponseDtoWrapper answer = new CompleteRoutingResponseDtoWrapper();
CloseableHttpResponse response = null;
try {
logger.debug("before sending http");
Stopwatch stopWatch = Stopwatch.createStarted();
response = client.execute(new HttpGet(routingRequestUrl));
stopWatch.stop();
// String latencyMsg = "after sending http. client-latency: "+stopWatch.elapsed(TimeUnit.MILLISECONDS) +" server-latency: "+response.getHeaders("Latency")[0].getValue();
logUtils.addLongToLongStatisticCollector("http.client.latency", (int)stopWatch.elapsed(TimeUnit.MILLISECONDS));
logUtils.addLongToLongStatisticCollector("http.server.latency", Integer.parseInt(response.getHeaders("Latency")[0].getValue()));
answer = analyzeStatusCodeAndMsgBody(numberOfTriesLeft, routingRequestUrl, answer, response, stopWatch);
} catch (Exception e) {
// e.printStackTrace();
// System.out.println(e.getMessage());
answer.errorMsg = e.getMessage();
answer.latency = null;
}
handleNullResponse(answer);
return answer;
}
so I changed the code to this
#Inject
#Singleton
public RoutingUrlHttpClient(IRoutingResponseFromStringFetcher routingResponseParser,
IRoutingResponseConverter routingResponseConverter, IUrlUtils urlUtils,
ILogUtils logUtils) {
this.routingResponseParser = routingResponseParser;
this.routingResponseConverter = routingResponseConverter;
this.urlUtils = urlUtils;
this.logUtils = logUtils;
requestConfig = RequestConfig.custom()
//time till handshake
.setConnectTimeout(40 * 1000)
//happens when you have a pool of connections and they are all busy, not allowing the connection
// manager to give you one connection to make the request.
.setConnectionRequestTimeout(40 * 1000)
//time till response
.setSocketTimeout(40 * 1000)
.build();
}
// public CompleteRoutingResponseDtoWrapper sendAndReturnDtoWrapper(String routingRequestUrl) {
// CompleteRoutingResponseDtoWrapper completeRoutingResponseDtoWrapper = sendRoutingRequestString
// (routingRequestUrl);
// completeRoutingResponseDtoWrapper.requestUrl = routingRequestUrl;
// return completeRoutingResponseDtoWrapper;
// }
#Override
public CompleteRoutingResponseLong sendRoutingRequestStringWithFullResponse(String routingRequestUrl) {
CompleteRoutingResponseDtoWrapper completeRoutingResponseDtoWrapper =
sendRoutingRequestString(routingRequestUrl);
completeRoutingResponseDtoWrapper.requestUrl = routingRequestUrl;
return routingResponseConverter.toCompleteRoutingResponseFull(completeRoutingResponseDtoWrapper);
}
private CompleteRoutingResponseDtoWrapper sendRoutingRequestString(String routingRequestUrl) {
return sendRoutingRequestString(Constants.NUM_OF_RETRIES, routingRequestUrl);
}
private CompleteRoutingResponseDtoWrapper sendRoutingRequestString(int numberOfTriesLeft,
String routingRequestUrl) {
routingRequestUrl = urlUtils.getHttpUrl(routingRequestUrl);
CompleteRoutingResponseDtoWrapper answer = new CompleteRoutingResponseDtoWrapper();
CloseableHttpResponse response = null;
try {
logger.debug("before sending http");
Stopwatch stopWatch = Stopwatch.createStarted();
//try-with-resources
try (CloseableHttpClient client = HttpClientBuilder
.create()
.setDefaultRequestConfig(requestConfig)
.build()){
response = client.execute(new HttpGet(routingRequestUrl));
stopWatch.stop();
// String latencyMsg = "after sending http. client-latency: "+stopWatch.elapsed(TimeUnit.MILLISECONDS) +" server-latency: "+response.getHeaders("Latency")[0].getValue();
logUtils.addLongToLongStatisticCollector("http.client.latency", (int)stopWatch.elapsed(TimeUnit.MILLISECONDS));
logUtils.addLongToLongStatisticCollector("http.server.latency", Integer.parseInt(response.getHeaders("Latency")[0].getValue()));
answer = analyzeStatusCodeAndMsgBody(numberOfTriesLeft, routingRequestUrl, answer, response, stopWatch);
}
} catch (Exception e) {
// e.printStackTrace();
// System.out.println(e.getMessage());
answer.errorMsg = e.getMessage();
answer.latency = null;
}
handleNullResponse(answer);
return answer;
}
I wanted to ask:
Is this a good practice to init a new httpClient upon each new request?
Am i cleaning the resource correctly?
Is there more efficient way to send HTTP requests? maybe an nio http client lib?
My request for clarity on this in HTTP Client requests done right suggests that since CloseableHttpClient is thread safe it probably can/should be used as a singleton so - no, you do not need to init a new httpClient.
Would recommend a read of the responses to that question - very useful.
CloseableHttpClient is thread-safe so you can safely reuse instances rather than creating them on a per-request basis. You could also investigate connection-pooling. I quite often use a pattern like this:
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connectionManager).build();
Have a read of this for more context.
Following is the code to request NHttpClientConnection from PoolingNHttpClientConnectionManager. The call connFuture.get(), fails to return. Anyone knows why? I am using HttpAsyncClient library httpasyncclient-4.0.1.jar
static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(100);
long connectTimeout=1;
long leaseTimeout=4;
TimeUnit timeUnit = TimeUnit.SECONDS;
Object state = null;
HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit,
new FutureCallback<NHttpClientConnection>() {
public void completed(final NHttpClientConnection c) {
System.out.println("completed");
httpConn = c;
}
public void failed(final Exception ex) {
System.out.println("failed");
}
public void cancelled() {
System.out.println("cancelled");
}
} );
System.out.println("Step3");
connFuture.get(); // Failed to return
System.out.println("Done");
}
I got it. ioReactor needs to be started. Here is the code that works.
static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {
HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
// Create client-side I/O event dispatch
final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT);
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(100);
long connectTimeout=1;
long leaseTimeout=4;
TimeUnit timeUnit = TimeUnit.SECONDS;
Object state = null;
//HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
HttpRoute route = new HttpRoute(new HttpHost("www.google.com"));
// Run the I/O reactor in a separate thread
Thread t = new Thread(new Runnable() {
public void run() {
try {
// Ready to go!
ioReactor.execute(ioEventDispatch);
} catch (InterruptedIOException ex) {
System.err.println("Interrupted");
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
}
System.out.println("Shutdown");
}
});
t.start();
Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit,
new FutureCallback<NHttpClientConnection>() {
public void completed(final NHttpClientConnection c) {
System.out.println("completed");
httpConn = c;
}
public void failed(final Exception ex) {
System.out.println("failed");
}
public void cancelled() {
System.out.println("cancelled");
}
} );
System.out.println("Step3");
connFuture.get();
System.out.println("Done");
ioReactor.shutdown();
}
I want to create a cookie which keeps the number of visits on persistent storage on client side, my server is simple servlet which runs on Apache Tomcat 8.0,
when the client sends a cookie already initialized with counter (for example counter = 12), the server increments the counter and sends back new cookie with new counter (counter = 13) and it's saved correctly by the client on my hard drive. This works fine for me.
but when the client sends a request for the first time, so no cookie is sent to server, and the server should create new cookie with counter = 0 and sends it back to client. This case doesn't work correctly and the client doesn't receive any cookie.
Here's my code example:
Client
public class Test
{
URI uri;
HttpURLConnection httpCon;
public static String urlString = "http://localhost:8082/Test/ReverseServlet";
public Test()
{
CookieManager cookieManager = new CookieManager(new MyCookieStore(), CookiePolicy.ACCEPT_ALL);
CookieManager.setDefault(cookieManager);
try
{
try
{
uri = new URI(urlString);
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
httpCon = (HttpURLConnection) uri.toURL().openConnection();
if (cookieManager.getCookieStore().get(uri).size() > 0)
{
httpCon.setRequestProperty("Cookie", cookieManager.getCookieStore().get(uri).get(0).toString());
}
httpCon.connect();
Map<String, List<String>> headerFields = httpCon.getHeaderFields();
List<String> cookiesHeader = headerFields.get("Set-Cookie");
if (cookiesHeader != null)
{
for (String cookie : cookiesHeader)
{
cookieManager.getCookieStore().add(uri, HttpCookie.parse(cookie).get(0));
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Test();
}
}
class MyCookieStore implements CookieStore
{
CookieStore store;
Path pathCookiesStore;
URI uri;
public MyCookieStore()
{
try
{
pathCookiesStore = Paths.get("D:\\Temp\\cookies.txt");
if (!Files.exists(pathCookiesStore))
{
Files.createFile(pathCookiesStore);
}
try
{
uri = new URI(Test.urlString);
}
catch (URISyntaxException e1)
{
e1.printStackTrace();
}
List<String> cookies = Files.readAllLines(pathCookiesStore);
store = new CookieManager().getCookieStore();
if (cookies.size() > 0)
{
HttpCookie countCookie = new HttpCookie("count", cookies.get(0));
countCookie.setDomain("localhost.local");
countCookie.setMaxAge(-1);
countCookie.setPath("/Test/");
store.add(uri, countCookie);
}
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
try
{
PrintWriter pw = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(pathCookiesStore, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)));
for (HttpCookie cookie : store.getCookies())
{
if (cookie.getName().equals("count"))
{
pw.println(cookie.getValue());
pw.flush();
pw.close();
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
catch (IOException excp)
{
excp.printStackTrace();
}
}
public void add(URI uri, HttpCookie cookie)
{
store.add(uri, cookie);
}
public List<HttpCookie> get(URI uri)
{
return store.get(uri);
}
public List<HttpCookie> getCookies()
{
return store.getCookies();
}
public List<URI> getURIs()
{
return store.getURIs();
}
public boolean remove(URI uri, HttpCookie cookie)
{
return store.remove(uri, cookie);
}
public boolean removeAll()
{
return store.removeAll();
}
}
Server
public class ReverseServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
int countVisits = 0;
Cookie cookies[] = req.getCookies();
if (cookies.length > 0)
{
for (Cookie cookie : cookies)
{
if (cookie.getName().equals("count"))
{
try
{
countVisits = Integer.parseInt(cookie.getValue());
countVisits++;
}
catch (NumberFormatException excp)
{
countVisits = 0;
}
break;
}
}
}
Cookie countCookie = new Cookie("count", String.valueOf(countVisits));
countCookie.setDomain("localhost.local");
countCookie.setMaxAge(-1);
countCookie.setPath("/Test/");
resp.addCookie(countCookie);
}
}
Camel http component does not close connections properly?
Having below route I have observed that connections are being created on the server, but not terminated.
After a while this is causing a problem
java.io.IOException: Too many open files
route:
from("seda:testSeda?concurrentConsumers=20")
.setHeader("Connection", constant("Close"))
.to("http://testServer/testFile.xml?authMethod=Basic&throwExceptionOnFailure=false&authUsername=user&authPassword=password")
.to("file://abc")
.end();
connections are in Close_Wait state any ideas?
I am using camel-http lib in version 2.14
You can override default HttpClient used by Apache Camel and define a custom Keep Alive Strategy.
https://howtodoinjava.com/spring-boot2/resttemplate/resttemplate-httpclient-java-config/
The code bellow resolved my issue in production:
#Configuration
public class AppConfiguration {
#Autowired
private PoolingHttpClientConnectionManager poolingConnectionManager;
#Autowired
private ConnectionKeepAliveStrategy connectionKeepAliveStrategy;
#Autowired
private SSLConnectionSocketFactory sslContext;
#Bean
CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
#Override
public void beforeApplicationStart(CamelContext context) {
HttpComponent httpComponent = context.getComponent("https4", HttpComponent.class);
httpComponent.setHttpClientConfigurer(new HttpClientConfigurer() {
#Override
public void configureHttpClient(HttpClientBuilder builder) {
builder.setSSLSocketFactory(sslContext);
RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslContext).build();
builder.setConnectionManager(poolingConnectionManager);
builder.setKeepAliveStrategy(connectionKeepAliveStrategy);
}
});
}
#Override
public void afterApplicationStart(CamelContext arg0) {
}
};
}
}
#Configuration
public class HttpClientConfig {
private static final int DEFAULT_KEEP_ALIVE_TIME_MILLIS = 20 * 1000;
private static final int CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS = 30;
#Value("${pathCertificado}")
private String pathCertificado;
private Logger logger = LoggerFactory.getLogger(HttpClientConfig.class);
#Bean
public PoolingHttpClientConnectionManager poolingConnectionManager() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(200);
connectionManager.setDefaultMaxPerRoute(20);
return connectionManager;
}
#Bean
public CloseableHttpClient httpClient() {
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000)
.setSocketTimeout(15000).build();
return HttpClientBuilder.create().setSSLSocketFactory(this.getSSLContext())
.setConnectionManager(this.poolingConnectionManager()).setDefaultRequestConfig(config)
.setKeepAliveStrategy(this.connectionKeepAliveStrategy()).build();
}
#Bean
public ConnectionKeepAliveStrategy connectionKeepAliveStrategy() {
return new ConnectionKeepAliveStrategy() {
#Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return DEFAULT_KEEP_ALIVE_TIME_MILLIS;
}
};
}
#Bean
public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
return new Runnable() {
#Override
#Scheduled(fixedDelay = 10000)
public void run() {
if (connectionManager != null) {
connectionManager.closeExpiredConnections();
connectionManager.closeIdleConnections(CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
}
}
};
}
#Bean
public SSLConnectionSocketFactory getSSLContext() {
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
try (FileInputStream jksFile = new FileInputStream(this.pathCertificado)) {
keyStore.load(jksFile, "xxxxxx".toCharArray());
}
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(keyStore, acceptingTrustStrategy).build();
return new SSLConnectionSocketFactory(sslContext);
} catch (Exception e) {
logger.error("Keystore load failed: " + this.pathCertificado, e);
return null;
}
}
}