问题
I have a tcp client-server application where the client opens a connection which is for single use and disconnects after configured time. How can I configure it keep the connection alive all the time, reconnect on closed connection and ensure multiple clients connections are open to the server.
Client config:
<int-ip:tcp-connection-factory id="client"
type="client"
host="${server.TCP.host}"
port="${server.TCP.port}"
single-use="true"
so-timeout="${client.TCP.socketTimeOut}" />
<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="bytesOut"
reply-channel="bytesIn"
connection-factory="client"
request-timeout="${client.TCP.requestTimeOut}"
reply-timeout="${client.TCP.replyTimeout}" />
Server config:
<int-ip:tcp-connection-factory id="tcpServerConnFactory"
type="server"
port="${service.tcp.port}"
using-nio="true"
single-use="false"
so-timeout="${service.tcp.socketTimeout}"
task-executor="taskExecutor"/>
<int-ip:tcp-inbound-gateway
id="tcpInboundGateway"
connection-factory="tcpServerConnFactory"
request-channel="bytesInChannel"
reply-channel="bytesOutChannel"
error-channel="errorChannel" />
回答1:
Single use on the client side means exactly that - each socket is used for one request/reply then closed.
When single-use="false"
, one shared connection is used for all requests/replies - and each caller blocks waiting for the socket.
You can use a 'CachingClientConnectionFactory` to maintain a pool of permanent connections - see the documentation.
As noted above, TCP sockets can be single-use (one request/response) or shared. Shared sockets do not perform well with outbound gateways, in high-volume environments, because the socket can only process one request/response at a time.
To improve performance, users could use collaborating channel adapters instead of gateways, but that requires application-level message correlation. See Section 31.8, “TCP Message Correlation” for more information.
Spring Integration 2.2 introduced a caching client connection factory, where a pool of shared sockets is used, allowing a gateway to process multiple concurrent requests with a pool of shared connections.
The upcoming 5.0 release - currently at milestone 7 (5.0.0.M7). Has a Thread Affinity Connection Factory which keeps a connection open per calling thread.
来源:https://stackoverflow.com/questions/46583757/configure-keep-alive-to-keep-connection-alive-all-the-time