com.alipay.remoting
Class DefaultConnectionManager

java.lang.Object
  extended by com.alipay.remoting.DefaultConnectionManager
All Implemented Interfaces:
ConnectionHeartbeatManager, ConnectionManager, Scannable

public class DefaultConnectionManager
extends Object
implements ConnectionManager, ConnectionHeartbeatManager, Scannable

Abstract implementation of connection manager


Field Summary
protected  RemotingAddressParser addressParser
          address parser
protected  ConnectionEventHandler connectionEventHandler
          connection event handler
protected  ConnectionEventListener connectionEventListener
          connection event listener
protected  ConnectionFactory connectionFactory
          connection factory
protected  ConnectionSelectStrategy connectionSelectStrategy
          connection pool select strategy
protected  ConcurrentHashMap<String,RunStateRecordedFutureTask<ConnectionPool>> connTasks
          connection pool initialize tasks
protected  ConcurrentHashMap<String,FutureTask<Integer>> healTasks
          heal connection tasks
 
Constructor Summary
DefaultConnectionManager()
          Default constructor
DefaultConnectionManager(ConnectionFactory connectionFactory, RemotingAddressParser addressParser, ConnectionEventHandler connectionEventHandler)
           
DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy)
           
DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy, ConnectionFactory connctionFactory)
           
DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy, ConnectionFactory connctionFactory, ConnectionEventHandler connectionEventHandler, ConnectionEventListener connectionEventListener)
           
DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy, ConnectionFactory connctionFactory, ConnectionEventHandler connectionEventHandler, ConnectionEventListener connectionEventListener, GlobalSwitch globalSwitch)
           
 
Method Summary
 void add(Connection connection)
          Add a connection to ConnectionPool.
 void add(Connection connection, String poolKey)
          Add a connection to ConnectionPool with the specified poolKey.
 void check(Connection connection)
          check a connection whether available, if not, throw RemotingException
 int count(String poolKey)
          Get the number of Connection in ConnectionPool with the specified pool key
 Connection create(String address, int connectTimeout)
          Create a connection using specified String address.
 Connection create(String ip, int port, int connectTimeout)
          Create a connection using specified ip and port.
 Connection create(Url url)
          Create a connection using specified Url.
 void createConnectionAndHealIfNeed(Url url)
          If no task cached, create one and initialize the connections.
 void disableHeartbeat(Connection connection)
          disable heart beat for a certain connection
 void enableHeartbeat(Connection connection)
          enable heart beat for a certain connection
 Connection get(String poolKey)
          Get a connection from ConnectionPool with the specified poolKey.
 RemotingAddressParser getAddressParser()
          Getter method for property addressParser.
 Map<String,List<Connection>> getAll()
          Get all connections of all poolKey.
 List<Connection> getAll(String poolKey)
          Get all connections from ConnectionPool with the specified poolKey.
 Connection getAndCreateIfAbsent(Url url)
          If no task cached, create one and initialize the connections.
 ConnectionEventHandler getConnectionEventHandler()
          Getter method for property connectionEventHandler.
 ConnectionEventListener getConnectionEventListener()
          Getter method for property connectionEventListener.
 ConnectionFactory getConnectionFactory()
          Getter method for property connctionFactory.
 ConnectionSelectStrategy getConnectionSelectStrategy()
          Getter method for property connectionSelectStrategy.
 ConcurrentHashMap<String,RunStateRecordedFutureTask<ConnectionPool>> getConnPools()
          Getter method for property connPools.
 void init()
          init
 void remove(Connection connection)
          Remove a Connection from all ConnectionPool with the poolKeys in Connection, and close it.
 void remove(Connection connection, String poolKey)
          Remove and close a Connection from ConnectionPool with the specified poolKey.
 void remove(String poolKey)
          Remove and close all connections from ConnectionPool with the specified poolKey.
 void removeAll()
          Warning!
 void scan()
          in case of cache pollution and connection leak, to do schedule scan
 void setAddressParser(RemotingAddressParser addressParser)
          Setter method for property addressParser.
 void setConnectionEventHandler(ConnectionEventHandler connectionEventHandler)
          Setter method for property connectionEventHandler.
 void setConnectionEventListener(ConnectionEventListener connectionEventListener)
          Setter method for property connectionEventListener.
 void setConnectionFactory(ConnectionFactory connectionFactory)
          Setter method for property connctionFactory.
 void setConnectionSelectStrategy(ConnectionSelectStrategy connectionSelectStrategy)
          Setter method for property connectionSelectStrategy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connTasks

protected ConcurrentHashMap<String,RunStateRecordedFutureTask<ConnectionPool>> connTasks
connection pool initialize tasks


healTasks

protected ConcurrentHashMap<String,FutureTask<Integer>> healTasks
heal connection tasks


connectionSelectStrategy

protected ConnectionSelectStrategy connectionSelectStrategy
connection pool select strategy


addressParser

protected RemotingAddressParser addressParser
address parser


connectionFactory

protected ConnectionFactory connectionFactory
connection factory


connectionEventHandler

protected ConnectionEventHandler connectionEventHandler
connection event handler


connectionEventListener

protected ConnectionEventListener connectionEventListener
connection event listener

Constructor Detail

DefaultConnectionManager

public DefaultConnectionManager()
Default constructor


DefaultConnectionManager

public DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy)
Parameters:
connectionSelectStrategy -

DefaultConnectionManager

public DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy,
                                ConnectionFactory connctionFactory)
Parameters:
connectionSelectStrategy -
connctionFactory -

DefaultConnectionManager

public DefaultConnectionManager(ConnectionFactory connectionFactory,
                                RemotingAddressParser addressParser,
                                ConnectionEventHandler connectionEventHandler)
Parameters:
connectionFactory -
addressParser -
connectionEventHandler -

DefaultConnectionManager

public DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy,
                                ConnectionFactory connctionFactory,
                                ConnectionEventHandler connectionEventHandler,
                                ConnectionEventListener connectionEventListener)
Parameters:
connectionSelectStrategy -
connctionFactory -
connectionEventHandler -
connectionEventListener -

DefaultConnectionManager

public DefaultConnectionManager(ConnectionSelectStrategy connectionSelectStrategy,
                                ConnectionFactory connctionFactory,
                                ConnectionEventHandler connectionEventHandler,
                                ConnectionEventListener connectionEventListener,
                                GlobalSwitch globalSwitch)
Parameters:
connectionSelectStrategy -
connctionFactory -
connectionEventHandler -
connectionEventListener -
globalSwitch -
Method Detail

init

public void init()
Description copied from interface: ConnectionManager
init

Specified by:
init in interface ConnectionManager
See Also:
ConnectionManager.init()

add

public void add(Connection connection)
Description copied from interface: ConnectionManager
Add a connection to ConnectionPool. If it contains multiple pool keys, this connection will be added to multiple ConnectionPool too.

Specified by:
add in interface ConnectionManager
Parameters:
connection - an available connection, you should ConnectionManager.check(Connection) this connection before add
See Also:
ConnectionManager.add(com.alipay.remoting.Connection)

add

public void add(Connection connection,
                String poolKey)
Description copied from interface: ConnectionManager
Add a connection to ConnectionPool with the specified poolKey.

Specified by:
add in interface ConnectionManager
Parameters:
connection - an available connection, you should ConnectionManager.check(Connection) this connection before add
poolKey - unique key of a ConnectionPool
See Also:
ConnectionManager.add(com.alipay.remoting.Connection, java.lang.String)

get

public Connection get(String poolKey)
Description copied from interface: ConnectionManager
Get a connection from ConnectionPool with the specified poolKey.

Specified by:
get in interface ConnectionManager
Parameters:
poolKey - unique key of a ConnectionPool
Returns:
a Connection selected by ConnectionSelectStrategy
or return null if there is no ConnectionPool mapping with poolKey
or return null if there is no Connection in ConnectionPool.
See Also:
ConnectionManager.get(String)

getAll

public List<Connection> getAll(String poolKey)
Description copied from interface: ConnectionManager
Get all connections from ConnectionPool with the specified poolKey.

Specified by:
getAll in interface ConnectionManager
Parameters:
poolKey - unique key of a ConnectionPool
Returns:
a list of Connection
or return an empty list if there is no ConnectionPool mapping with poolKey.
See Also:
ConnectionManager.getAll(java.lang.String)

getAll

public Map<String,List<Connection>> getAll()
Get all connections of all poolKey.

Specified by:
getAll in interface ConnectionManager
Returns:
a map with poolKey as key and a list of connections in ConnectionPool as value

remove

public void remove(Connection connection)
Description copied from interface: ConnectionManager
Remove a Connection from all ConnectionPool with the poolKeys in Connection, and close it.

Specified by:
remove in interface ConnectionManager
See Also:
ConnectionManager.remove(com.alipay.remoting.Connection)

remove

public void remove(Connection connection,
                   String poolKey)
Description copied from interface: ConnectionManager
Remove and close a Connection from ConnectionPool with the specified poolKey.

Specified by:
remove in interface ConnectionManager
poolKey - unique key of a ConnectionPool
See Also:
ConnectionManager.remove(com.alipay.remoting.Connection, java.lang.String)

remove

public void remove(String poolKey)
Description copied from interface: ConnectionManager
Remove and close all connections from ConnectionPool with the specified poolKey.

Specified by:
remove in interface ConnectionManager
Parameters:
poolKey - unique key of a ConnectionPool
See Also:
ConnectionManager.remove(java.lang.String)

removeAll

public void removeAll()
Warning! This is weakly consistent implementation, to prevent lock the whole ConcurrentHashMap.

Specified by:
removeAll in interface ConnectionManager
See Also:
ConnectionManager.removeAll()

check

public void check(Connection connection)
           throws RemotingException
Description copied from interface: ConnectionManager
check a connection whether available, if not, throw RemotingException

Specified by:
check in interface ConnectionManager
Throws:
RemotingException
See Also:
ConnectionManager.check(com.alipay.remoting.Connection)

count

public int count(String poolKey)
Description copied from interface: ConnectionManager
Get the number of Connection in ConnectionPool with the specified pool key

Specified by:
count in interface ConnectionManager
Parameters:
poolKey - unique key of a ConnectionPool
Returns:
See Also:
ConnectionManager.count(java.lang.String)

scan

public void scan()
in case of cache pollution and connection leak, to do schedule scan

Specified by:
scan in interface Scannable
See Also:
Scannable.scan()

getAndCreateIfAbsent

public Connection getAndCreateIfAbsent(Url url)
                                throws InterruptedException,
                                       RemotingException
If no task cached, create one and initialize the connections.

Specified by:
getAndCreateIfAbsent in interface ConnectionManager
Parameters:
url - Url contains connect infos.
Returns:
the created Connection.
Throws:
InterruptedException - if interrupted
RemotingException - if create failed.
See Also:
ConnectionManager.getAndCreateIfAbsent(Url)

createConnectionAndHealIfNeed

public void createConnectionAndHealIfNeed(Url url)
                                   throws InterruptedException,
                                          RemotingException
If no task cached, create one and initialize the connections. If task cached, check whether the number of connections adequate, if not then heal it.

Specified by:
createConnectionAndHealIfNeed in interface ConnectionManager
Parameters:
url -
Throws:
InterruptedException
RemotingException

create

public Connection create(Url url)
                  throws RemotingException
Description copied from interface: ConnectionManager
Create a connection using specified Url.

Specified by:
create in interface ConnectionManager
Parameters:
url - Url contains connect infos.
Returns:
Throws:
RemotingException
See Also:
ConnectionManager.create(com.alipay.remoting.Url)

create

public Connection create(String ip,
                         int port,
                         int connectTimeout)
                  throws RemotingException
Description copied from interface: ConnectionManager
Create a connection using specified ip and port.

Specified by:
create in interface ConnectionManager
Parameters:
ip - connect ip, e.g. 127.0.0.1
port - connect port, e.g. 1111
connectTimeout - an int connect timeout value
Returns:
the created Connection
Throws:
RemotingException
See Also:
ConnectionManager.create(java.lang.String, int, int)

create

public Connection create(String address,
                         int connectTimeout)
                  throws RemotingException
Description copied from interface: ConnectionManager
Create a connection using specified String address.

Specified by:
create in interface ConnectionManager
Parameters:
address - a String address, e.g. 127.0.0.1:1111
connectTimeout - an int connect timeout value
Returns:
the created Connection
Throws:
RemotingException - if create failed
See Also:
ConnectionManager.create(java.lang.String, int)

disableHeartbeat

public void disableHeartbeat(Connection connection)
Description copied from interface: ConnectionHeartbeatManager
disable heart beat for a certain connection

Specified by:
disableHeartbeat in interface ConnectionHeartbeatManager
See Also:
ConnectionHeartbeatManager.disableHeartbeat(com.alipay.remoting.Connection)

enableHeartbeat

public void enableHeartbeat(Connection connection)
Description copied from interface: ConnectionHeartbeatManager
enable heart beat for a certain connection

Specified by:
enableHeartbeat in interface ConnectionHeartbeatManager
See Also:
ConnectionHeartbeatManager.enableHeartbeat(com.alipay.remoting.Connection)

getConnectionSelectStrategy

public ConnectionSelectStrategy getConnectionSelectStrategy()
Getter method for property connectionSelectStrategy.

Returns:
property value of connectionSelectStrategy

setConnectionSelectStrategy

public void setConnectionSelectStrategy(ConnectionSelectStrategy connectionSelectStrategy)
Setter method for property connectionSelectStrategy.

Parameters:
connectionSelectStrategy - value to be assigned to property connectionSelectStrategy

getAddressParser

public RemotingAddressParser getAddressParser()
Getter method for property addressParser.

Returns:
property value of addressParser

setAddressParser

public void setAddressParser(RemotingAddressParser addressParser)
Setter method for property addressParser.

Parameters:
addressParser - value to be assigned to property addressParser

getConnectionFactory

public ConnectionFactory getConnectionFactory()
Getter method for property connctionFactory.

Returns:
property value of connctionFactory

setConnectionFactory

public void setConnectionFactory(ConnectionFactory connectionFactory)
Setter method for property connctionFactory.

Parameters:
connectionFactory - value to be assigned to property connctionFactory

getConnectionEventHandler

public ConnectionEventHandler getConnectionEventHandler()
Getter method for property connectionEventHandler.

Returns:
property value of connectionEventHandler

setConnectionEventHandler

public void setConnectionEventHandler(ConnectionEventHandler connectionEventHandler)
Setter method for property connectionEventHandler.

Parameters:
connectionEventHandler - value to be assigned to property connectionEventHandler

getConnectionEventListener

public ConnectionEventListener getConnectionEventListener()
Getter method for property connectionEventListener.

Returns:
property value of connectionEventListener

setConnectionEventListener

public void setConnectionEventListener(ConnectionEventListener connectionEventListener)
Setter method for property connectionEventListener.

Parameters:
connectionEventListener - value to be assigned to property connectionEventListener

getConnPools

public ConcurrentHashMap<String,RunStateRecordedFutureTask<ConnectionPool>> getConnPools()
Getter method for property connPools.

Returns:
property value of connPools


Copyright © 2018. All rights reserved.