Java – unable to configure “keep alive” in camel HTTP component

I'm having some trouble setting up the HTTP component correctly Currently, microservices extract JSON content from the provider, process it, and send it to the next service for further processing The main problem is that this microservice creates a lot of close_ Wait socket connection I know that the whole concept of "keep-alive" should keep the connection open until I close it, but the server may discard the connection and create this close for some reason_ Wait socket

I have created a small service for debugging / testing purposes that can send get calls to Google, but even if the connection remains open until I close the program I tried many different solutions:

> . Setheader ("connect", constant ("close") > - dhttp Keepalive = false as VM parameter > switch from camel HTTP to camel-http4 > httpclient soTimeout = 500(Camel-HTTP),httpClient. Sockettimeout = 500 and connectiontimetolive = 500 (camel-http4) > Setheader ("connection", simple ("keep alive")) and Setheader ("keep alive", simple ("timeout = 10") (camel-http4) > set the response from - 1 (never end) to a specific value in camel-http4 by debugging defaultconnectionkeep alive strategy - this works, but I can't inject my own policy

But I failed So maybe one of you can help me:

>How can I tell camel HTTP that it should close the connection after a specific time? For example, the service is extracted from the content provider every hour After 3-4 hours, httpcomponent shall close the connection after pulling and reopen at the next pull Currently, each connection will be put back into the multithreadedhttpconnectionmanager, and the socket is still open. > If this is not possible with camel http: how do I inject httpclientbuilder when creating a route? I know it should be possible to use the HTTP client option, but I don't understand specific parts of the document

Thank you for your help

Solution

You can provide your own client connection manager to HTTP 4 In general, you should use org. Org apache. http. impl. Conn.poolinghttpclientconnectionmanager instance, you can use your own org apache. http. config. Socketconfig configures it as the setdefaultsocketconfig method of the connection manager

If you use spring with Java config, you will have a method:

@Bean
PoolingHttpClientConnectionManager connectionManager() {
    SocketConfig socketConfig = SocketConfig.custom()
            .setSoKeepAlive(false)
            .setSoReuseAddress(true)
            .build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setDefaultSocketConfig(socketConfig);
    return connectionManager;
}

Then you just need to use it in the endpoint definition, as follows: clientconnectionmanager = #connectionmanager

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>