Class CachingClientConnectionFactory
java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory
org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory
org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactory
- All Implemented Interfaces:
Aware,BeanFactoryAware,BeanNameAware,DisposableBean,InitializingBean,ApplicationContextAware,ApplicationEventPublisherAware,Lifecycle,ExpressionCapable,ConnectionFactory,NamedComponent,ManageableLifecycle
public class CachingClientConnectionFactory
extends AbstractClientConnectionFactory
implements DisposableBean
Connection factory that caches connections from the underlying target factory. The underlying
factory will be reconfigured to have
singleUse=true in order for the connection to be
returned to the cache after use. Users should not subsequently set the underlying property to
false, or cache starvation will result.- Since:
- 2.2
- Author:
- Gary Russell, Christian Tzolov, Artem Bilan
-
Field Summary
Fields inherited from class org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory
connections, connectionsMonitor, DEFAULT_REPLY_TIMEOUT, lifecycleMonitorFields inherited from class org.springframework.integration.context.IntegrationObjectSupport
EXPRESSION_PARSER, logger -
Constructor Summary
ConstructorsConstructorDescriptionCachingClientConnectionFactory(AbstractClientConnectionFactory target, int poolSize) Construct a caching connection factory that delegates to the provided factory, with the provided pool size. -
Method Summary
Modifier and TypeMethodDescriptionbooleancloseConnection(String connectionId) Close a connection with the specified connection id.voiddestroy()voidSet whether to automatically (default) or manually add aTcpListenerto the connections created by this factory.booleanvoidforceClose(TcpConnection connection) Force close the connection and null the field if it's a shared connection.intintSubclasses may implement this method to provide component type information.Deserializer<?>getHost()intintintgetPort()Serializer<?>intintintintintinthashCode()booleanbooleanbooleanbooleanbooleanvoidregisterListener(TcpListener listener) Delegate TCP Client Connection factories that are used to receive data need a Listener to send the messages to.voidregisterSender(TcpSender sender) Registers a TcpSender; for server sockets, used to provide connection information so a sender can be used to reply to incoming messages.voidsetComponentName(String componentName) Sets the name of this component.voidsetConnectionWaitTimeout(int connectionWaitTimeout) voidsetDeserializer(Deserializer<?> deserializer) voidsetInterceptorFactoryChain(TcpConnectionInterceptorFactoryChain interceptorFactoryChain) voidsetLookupHost(boolean lookupHost) If true, DNS reverse lookup is done on the remote ip address.voidsetMapper(TcpMessageMapper mapper) voidsetPoolSize(int poolSize) voidsetSerializer(Serializer<?> serializer) voidsetSingleUse(boolean singleUse) Ignored on this factory; connections are always cached in the pool.voidsetSoKeepAlive(boolean soKeepAlive) voidsetSoLinger(int soLinger) voidsetSoReceiveBufferSize(int soReceiveBufferSize) voidsetSoSendBufferSize(int soSendBufferSize) voidsetSoTcpNoDelay(boolean soTcpNoDelay) voidsetSoTimeout(int soTimeout) voidsetSoTrafficClass(int soTrafficClass) voidsetTaskExecutor(Executor taskExecutor) voidstart()voidstop()Stops the server.Methods inherited from class org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory
buildNewConnection, getConnection, getConnectionTest, getConnectTimeout, getTheConnection, initializeConnection, obtainNewConnection, obtainSharedConnection, setConnectionTest, setConnectTimeout, setTheConnectionMethods inherited from class org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory
addConnection, checkActive, delayRead, doAccept, getApplicationEventPublisher, getDelayedReads, getLifecycleMonitor, getListener, getOpenConnectionIds, getReadDelay, getSenders, getSslHandshakeTimeout, getTaskExecutor, getTcpSocketSupport, harvestClosedConnections, isActive, onInit, processNioSelections, setActive, setApplicationEventPublisher, setHost, setLeaveOpen, setNioHarvestInterval, setPort, setReadDelay, setSocketAttributes, setSslHandshakeTimeout, setTcpSocketSupport, toString, unregisterSender, wrapConnectionMethods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentName, getConversionService, getExpression, getIntegrationProperties, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler
-
Constructor Details
-
CachingClientConnectionFactory
Construct a caching connection factory that delegates to the provided factory, with the provided pool size.- Parameters:
target- the target factory.poolSize- the number of connections to allow.
-
-
Method Details
-
setConnectionWaitTimeout
public void setConnectionWaitTimeout(int connectionWaitTimeout) - Parameters:
connectionWaitTimeout- the new timeout.- See Also:
-
setPoolSize
public void setPoolSize(int poolSize) - Parameters:
poolSize- the new pool size.- See Also:
-
getPoolSize
public int getPoolSize()- Returns:
- the pool size.
- See Also:
-
getIdleCount
public int getIdleCount()- Returns:
- the idle count.
- See Also:
-
getActiveCount
public int getActiveCount()- Returns:
- the active count.
- See Also:
-
getAllocatedCount
public int getAllocatedCount()- Returns:
- the allocated count.
- See Also:
-
obtainConnection
- Overrides:
obtainConnectionin classAbstractClientConnectionFactory
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceLifecycle- Specified by:
isRunningin interfaceManageableLifecycle- Overrides:
isRunningin classAbstractConnectionFactory
-
hashCode
public int hashCode() -
setComponentName
Description copied from class:IntegrationObjectSupportSets the name of this component.- Overrides:
setComponentNamein classIntegrationObjectSupport- Parameters:
componentName- The component name.
-
getComponentType
Description copied from class:IntegrationObjectSupportSubclasses may implement this method to provide component type information.- Specified by:
getComponentTypein interfaceNamedComponent- Overrides:
getComponentTypein classIntegrationObjectSupport
-
equals
-
getSoTimeout
public int getSoTimeout()- Overrides:
getSoTimeoutin classAbstractConnectionFactory- Returns:
- the soTimeout
-
setSoTimeout
public void setSoTimeout(int soTimeout) - Overrides:
setSoTimeoutin classAbstractConnectionFactory- Parameters:
soTimeout- the soTimeout to set
-
getSoReceiveBufferSize
public int getSoReceiveBufferSize()- Overrides:
getSoReceiveBufferSizein classAbstractConnectionFactory- Returns:
- the soReceiveBufferSize
-
setSoReceiveBufferSize
public void setSoReceiveBufferSize(int soReceiveBufferSize) - Overrides:
setSoReceiveBufferSizein classAbstractConnectionFactory- Parameters:
soReceiveBufferSize- the soReceiveBufferSize to set
-
getSoSendBufferSize
public int getSoSendBufferSize()- Overrides:
getSoSendBufferSizein classAbstractConnectionFactory- Returns:
- the soSendBufferSize
-
setSoSendBufferSize
public void setSoSendBufferSize(int soSendBufferSize) - Overrides:
setSoSendBufferSizein classAbstractConnectionFactory- Parameters:
soSendBufferSize- the soSendBufferSize to set
-
isSoTcpNoDelay
public boolean isSoTcpNoDelay()- Overrides:
isSoTcpNoDelayin classAbstractConnectionFactory- Returns:
- the soTcpNoDelay
-
setSoTcpNoDelay
public void setSoTcpNoDelay(boolean soTcpNoDelay) - Overrides:
setSoTcpNoDelayin classAbstractConnectionFactory- Parameters:
soTcpNoDelay- the soTcpNoDelay to set
-
getSoLinger
public int getSoLinger()- Overrides:
getSoLingerin classAbstractConnectionFactory- Returns:
- the soLinger
-
setSoLinger
public void setSoLinger(int soLinger) - Overrides:
setSoLingerin classAbstractConnectionFactory- Parameters:
soLinger- the soLinger to set
-
isSoKeepAlive
public boolean isSoKeepAlive()- Overrides:
isSoKeepAlivein classAbstractConnectionFactory- Returns:
- the soKeepAlive
-
setSoKeepAlive
public void setSoKeepAlive(boolean soKeepAlive) - Overrides:
setSoKeepAlivein classAbstractConnectionFactory- Parameters:
soKeepAlive- the soKeepAlive to set
-
getSoTrafficClass
public int getSoTrafficClass()- Overrides:
getSoTrafficClassin classAbstractConnectionFactory- Returns:
- the soTrafficClass
-
setSoTrafficClass
public void setSoTrafficClass(int soTrafficClass) - Overrides:
setSoTrafficClassin classAbstractConnectionFactory- Parameters:
soTrafficClass- the soTrafficClass to set
-
getHost
- Overrides:
getHostin classAbstractConnectionFactory- Returns:
- the host
-
getPort
public int getPort()- Overrides:
getPortin classAbstractConnectionFactory- Returns:
- the port
-
getSender
- Overrides:
getSenderin classAbstractConnectionFactory- Returns:
- the first sender, if present.
-
getSerializer
- Overrides:
getSerializerin classAbstractConnectionFactory- Returns:
- the serializer
-
getDeserializer
- Overrides:
getDeserializerin classAbstractConnectionFactory- Returns:
- the deserializer
-
getMapper
- Overrides:
getMapperin classAbstractConnectionFactory- Returns:
- the mapper
-
registerListener
Delegate TCP Client Connection factories that are used to receive data need a Listener to send the messages to. This applies to client factories used for outbound gateways or for a pair of collaborating channel adapters.During initialization, if a factory detects it has no listener it's listening logic (active thread) is terminated.
The listener registered with a factory is provided to each connection it creates, so it can call the onMessage() method.
This code satisfies the first requirement in that this listener signals to the factory that it needs to run its listening logic.
When we wrap actual connections with CachedConnections, the connection is given the wrapper as a listener, so it can enhance the headers in onMessage(); the wrapper then invokes the real listener supplied here, with the modified message.
- Overrides:
registerListenerin classAbstractConnectionFactory- Parameters:
listener- the TcpListener.
-
registerSender
Description copied from class:AbstractConnectionFactoryRegisters a TcpSender; for server sockets, used to provide connection information so a sender can be used to reply to incoming messages.- Overrides:
registerSenderin classAbstractConnectionFactory- Parameters:
sender- The sender
-
setTaskExecutor
- Overrides:
setTaskExecutorin classAbstractConnectionFactory- Parameters:
taskExecutor- the taskExecutor to set
-
setDeserializer
- Overrides:
setDeserializerin classAbstractConnectionFactory- Parameters:
deserializer- the deserializer to set
-
setSerializer
- Overrides:
setSerializerin classAbstractConnectionFactory- Parameters:
serializer- the serializer to set
-
setMapper
- Overrides:
setMapperin classAbstractConnectionFactory- Parameters:
mapper- the mapper to set; defaults to aTcpMessageMapper
-
isSingleUse
public boolean isSingleUse()- Overrides:
isSingleUsein classAbstractConnectionFactory- Returns:
- the singleUse
-
setSingleUse
public void setSingleUse(boolean singleUse) Ignored on this factory; connections are always cached in the pool. The underlying connection factory will have its singleUse property coerced to true (causing the connection to be returned). Setting it to false on the underlying factory after initialization will cause cache starvation.- Overrides:
setSingleUsein classAbstractConnectionFactory- Parameters:
singleUse- the singleUse.
-
setInterceptorFactoryChain
public void setInterceptorFactoryChain(TcpConnectionInterceptorFactoryChain interceptorFactoryChain) - Overrides:
setInterceptorFactoryChainin classAbstractConnectionFactory
-
setLookupHost
public void setLookupHost(boolean lookupHost) Description copied from class:AbstractConnectionFactoryIf true, DNS reverse lookup is done on the remote ip address. Default false: not all environments (e.g. Docker containers) perform reliable DNS resolution.- Overrides:
setLookupHostin classAbstractConnectionFactory- Parameters:
lookupHost- the lookupHost to set
-
isLookupHost
public boolean isLookupHost()- Overrides:
isLookupHostin classAbstractConnectionFactory- Returns:
- the lookupHost
-
forceClose
Description copied from class:AbstractClientConnectionFactoryForce close the connection and null the field if it's a shared connection.- Overrides:
forceClosein classAbstractClientConnectionFactory- Parameters:
connection- The connection.
-
enableManualListenerRegistration
public void enableManualListenerRegistration()Description copied from class:AbstractClientConnectionFactorySet whether to automatically (default) or manually add aTcpListenerto the connections created by this factory. By default, the factory automatically configures the listener. When manual registration is in place, incoming messages will be delayed until the listener is registered.- Overrides:
enableManualListenerRegistrationin classAbstractClientConnectionFactory
-
closeConnection
Description copied from class:AbstractConnectionFactoryClose a connection with the specified connection id.- Overrides:
closeConnectionin classAbstractConnectionFactory- Parameters:
connectionId- the connection id.- Returns:
- true if the connection was closed.
-
start
public void start()- Specified by:
startin interfaceLifecycle- Specified by:
startin interfaceManageableLifecycle- Overrides:
startin classAbstractConnectionFactory
-
stop
public void stop()Description copied from class:AbstractConnectionFactoryStops the server.- Specified by:
stopin interfaceLifecycle- Specified by:
stopin interfaceManageableLifecycle- Overrides:
stopin classAbstractConnectionFactory
-
destroy
public void destroy()- Specified by:
destroyin interfaceDisposableBean
-