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
-
Field Summary
Fields inherited from class org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory
DEFAULT_REPLY_TIMEOUT, lifecycleMonitor
Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport
EXPRESSION_PARSER, logger
-
Constructor Summary
ConstructorDescriptionCachingClientConnectionFactory
(AbstractClientConnectionFactory target, int poolSize) Construct a caching connection factory that delegates to the provided factory, with the provided pool size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
destroy()
void
Set whether to automatically (default) or manually add aTcpListener
to the connections created by this factory.boolean
void
forceClose
(TcpConnection connection) Force close the connection and null the field if it's a shared connection.int
int
Subclasses may implement this method to provide component type information.Deserializer<?>
getHost()
int
int
int
getPort()
Serializer<?>
int
int
int
int
int
int
hashCode()
boolean
boolean
boolean
boolean
boolean
void
registerListener
(TcpListener listener) Delegate TCP Client Connection factories that are used to receive data need a Listener to send the messages to.void
registerSender
(TcpSender sender) Registers a TcpSender; for server sockets, used to provide connection information so a sender can be used to reply to incoming messages.void
setComponentName
(String componentName) Sets the name of this component.void
setConnectionWaitTimeout
(int connectionWaitTimeout) void
setDeserializer
(Deserializer<?> deserializer) void
setInterceptorFactoryChain
(TcpConnectionInterceptorFactoryChain interceptorFactoryChain) void
setLookupHost
(boolean lookupHost) If true, DNS reverse lookup is done on the remote ip address.void
setMapper
(TcpMessageMapper mapper) void
setPoolSize
(int poolSize) void
setSerializer
(Serializer<?> serializer) void
setSingleUse
(boolean singleUse) Ignored on this factory; connections are always cached in the pool.void
setSoKeepAlive
(boolean soKeepAlive) void
setSoLinger
(int soLinger) void
setSoReceiveBufferSize
(int soReceiveBufferSize) void
setSoSendBufferSize
(int soSendBufferSize) void
setSoTcpNoDelay
(boolean soTcpNoDelay) void
setSoTimeout
(int soTimeout) void
setSoTrafficClass
(int soTrafficClass) void
setTaskExecutor
(Executor taskExecutor) void
start()
void
stop()
Stops the server.Methods inherited from class org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory
buildNewConnection, getConnection, getConnectionTest, getConnectTimeout, getTheConnection, initializeConnection, obtainNewConnection, obtainSharedConnection, setConnectionTest, setConnectTimeout, setTheConnection
Methods inherited from class org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory
addConnection, checkActive, closeConnection, 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, wrapConnection
Methods 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:
obtainConnection
in classAbstractClientConnectionFactory
-
isRunning
public boolean isRunning()- Specified by:
isRunning
in interfaceLifecycle
- Specified by:
isRunning
in interfaceManageableLifecycle
- Overrides:
isRunning
in classAbstractConnectionFactory
-
hashCode
public int hashCode() -
setComponentName
Description copied from class:IntegrationObjectSupport
Sets the name of this component.- Overrides:
setComponentName
in classIntegrationObjectSupport
- Parameters:
componentName
- The component name.
-
getComponentType
Description copied from class:IntegrationObjectSupport
Subclasses may implement this method to provide component type information.- Specified by:
getComponentType
in interfaceNamedComponent
- Overrides:
getComponentType
in classIntegrationObjectSupport
-
equals
-
getSoTimeout
public int getSoTimeout()- Overrides:
getSoTimeout
in classAbstractConnectionFactory
- Returns:
- the soTimeout
-
setSoTimeout
public void setSoTimeout(int soTimeout) - Overrides:
setSoTimeout
in classAbstractConnectionFactory
- Parameters:
soTimeout
- the soTimeout to set
-
getSoReceiveBufferSize
public int getSoReceiveBufferSize()- Overrides:
getSoReceiveBufferSize
in classAbstractConnectionFactory
- Returns:
- the soReceiveBufferSize
-
setSoReceiveBufferSize
public void setSoReceiveBufferSize(int soReceiveBufferSize) - Overrides:
setSoReceiveBufferSize
in classAbstractConnectionFactory
- Parameters:
soReceiveBufferSize
- the soReceiveBufferSize to set
-
getSoSendBufferSize
public int getSoSendBufferSize()- Overrides:
getSoSendBufferSize
in classAbstractConnectionFactory
- Returns:
- the soSendBufferSize
-
setSoSendBufferSize
public void setSoSendBufferSize(int soSendBufferSize) - Overrides:
setSoSendBufferSize
in classAbstractConnectionFactory
- Parameters:
soSendBufferSize
- the soSendBufferSize to set
-
isSoTcpNoDelay
public boolean isSoTcpNoDelay()- Overrides:
isSoTcpNoDelay
in classAbstractConnectionFactory
- Returns:
- the soTcpNoDelay
-
setSoTcpNoDelay
public void setSoTcpNoDelay(boolean soTcpNoDelay) - Overrides:
setSoTcpNoDelay
in classAbstractConnectionFactory
- Parameters:
soTcpNoDelay
- the soTcpNoDelay to set
-
getSoLinger
public int getSoLinger()- Overrides:
getSoLinger
in classAbstractConnectionFactory
- Returns:
- the soLinger
-
setSoLinger
public void setSoLinger(int soLinger) - Overrides:
setSoLinger
in classAbstractConnectionFactory
- Parameters:
soLinger
- the soLinger to set
-
isSoKeepAlive
public boolean isSoKeepAlive()- Overrides:
isSoKeepAlive
in classAbstractConnectionFactory
- Returns:
- the soKeepAlive
-
setSoKeepAlive
public void setSoKeepAlive(boolean soKeepAlive) - Overrides:
setSoKeepAlive
in classAbstractConnectionFactory
- Parameters:
soKeepAlive
- the soKeepAlive to set
-
getSoTrafficClass
public int getSoTrafficClass()- Overrides:
getSoTrafficClass
in classAbstractConnectionFactory
- Returns:
- the soTrafficClass
-
setSoTrafficClass
public void setSoTrafficClass(int soTrafficClass) - Overrides:
setSoTrafficClass
in classAbstractConnectionFactory
- Parameters:
soTrafficClass
- the soTrafficClass to set
-
getHost
- Overrides:
getHost
in classAbstractConnectionFactory
- Returns:
- the host
-
getPort
public int getPort()- Overrides:
getPort
in classAbstractConnectionFactory
- Returns:
- the port
-
getSender
- Overrides:
getSender
in classAbstractConnectionFactory
- Returns:
- the first sender, if present.
-
getSerializer
- Overrides:
getSerializer
in classAbstractConnectionFactory
- Returns:
- the serializer
-
getDeserializer
- Overrides:
getDeserializer
in classAbstractConnectionFactory
- Returns:
- the deserializer
-
getMapper
- Overrides:
getMapper
in 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:
registerListener
in classAbstractConnectionFactory
- Parameters:
listener
- the TcpListener.
-
registerSender
Description copied from class:AbstractConnectionFactory
Registers a TcpSender; for server sockets, used to provide connection information so a sender can be used to reply to incoming messages.- Overrides:
registerSender
in classAbstractConnectionFactory
- Parameters:
sender
- The sender
-
setTaskExecutor
- Overrides:
setTaskExecutor
in classAbstractConnectionFactory
- Parameters:
taskExecutor
- the taskExecutor to set
-
setDeserializer
- Overrides:
setDeserializer
in classAbstractConnectionFactory
- Parameters:
deserializer
- the deserializer to set
-
setSerializer
- Overrides:
setSerializer
in classAbstractConnectionFactory
- Parameters:
serializer
- the serializer to set
-
setMapper
- Overrides:
setMapper
in classAbstractConnectionFactory
- Parameters:
mapper
- the mapper to set; defaults to aTcpMessageMapper
-
isSingleUse
public boolean isSingleUse()- Overrides:
isSingleUse
in 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:
setSingleUse
in classAbstractConnectionFactory
- Parameters:
singleUse
- the singleUse.
-
setInterceptorFactoryChain
public void setInterceptorFactoryChain(TcpConnectionInterceptorFactoryChain interceptorFactoryChain) - Overrides:
setInterceptorFactoryChain
in classAbstractConnectionFactory
-
setLookupHost
public void setLookupHost(boolean lookupHost) Description copied from class:AbstractConnectionFactory
If 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:
setLookupHost
in classAbstractConnectionFactory
- Parameters:
lookupHost
- the lookupHost to set
-
isLookupHost
public boolean isLookupHost()- Overrides:
isLookupHost
in classAbstractConnectionFactory
- Returns:
- the lookupHost
-
forceClose
Description copied from class:AbstractClientConnectionFactory
Force close the connection and null the field if it's a shared connection.- Overrides:
forceClose
in classAbstractClientConnectionFactory
- Parameters:
connection
- The connection.
-
enableManualListenerRegistration
public void enableManualListenerRegistration()Description copied from class:AbstractClientConnectionFactory
Set whether to automatically (default) or manually add aTcpListener
to 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:
enableManualListenerRegistration
in classAbstractClientConnectionFactory
-
start
public void start()- Specified by:
start
in interfaceLifecycle
- Specified by:
start
in interfaceManageableLifecycle
- Overrides:
start
in classAbstractConnectionFactory
-
stop
public void stop()Description copied from class:AbstractConnectionFactory
Stops the server.- Specified by:
stop
in interfaceLifecycle
- Specified by:
stop
in interfaceManageableLifecycle
- Overrides:
stop
in classAbstractConnectionFactory
-
destroy
- Specified by:
destroy
in interfaceDisposableBean
- Throws:
Exception
-