GridGain™ 2.1.0
Java API Specification

org.gridgain.grid.spi.communication.tcp
Class GridTcpCommunicationSpi

java.lang.Object
  extended by org.gridgain.grid.spi.GridSpiAdapter
      extended by org.gridgain.grid.spi.communication.tcp.GridTcpCommunicationSpi
All Implemented Interfaces:
GridCommunicationSpi, GridTcpCommunicationSpiMBean, GridSpi, GridSpiManagementMBean

@GridSpiInfo(author="GridGain Systems",
             url="www.gridgain.org",
             email="support@gridgain.com",
             version="x.x")
@GridSpiMultipleInstancesSupport(value=true)
public class GridTcpCommunicationSpi
extends GridSpiAdapter
implements GridCommunicationSpi, GridTcpCommunicationSpiMBean

GridTcpCommunicationSpi is default communication SPI which uses TCP/IP protocol and Java NIO to communicate with other nodes.

To enable communication with other nodes, this SPI adds GridTcpCommunicationSpi.ATTR_ADDR and GridTcpCommunicationSpi.ATTR_PORT local node attributes (see GridNode.getAttributes().

At startup, this SPI tries to start listening to local port specified by GridTcpCommunicationSpi.setLocalPort(int) method. If local port is occupied, then SPI will automatically increment the port number until it can successfully bind for listening. GridTcpCommunicationSpi.setLocalPortRange(int) configuration parameter controls maximum number of ports that SPI will try before it fails. Port range comes very handy when starting multiple grid nodes on the same machine or even in the same VM. In this case all nodes can be brought up without a single change in configuration.

This SPI caches connections to remote nodes so it does not have to reconnect every time a message is sent. By default, idle connections are kept active for GridTcpCommunicationSpi.DFLT_IDLE_CONN_TIMEOUT period and then are closed. Use GridTcpCommunicationSpi.setIdleConnectionTimeout(long) configuration parameter to configure you own idle connection timeout.

Configuration

Mandatory

This SPI has no mandatory configuration parameters.

Optional

The following configuration parameters are optional:

Java Example

GridTcpCommunicationSpi is used by default and should be explicitely configured only if some SPI configuration parameters need to be overridden. Examples below enable encryption which is disabled by default.
 GridTcpCommunicationSpi commSpi = new GridTcpCommunicationSpi();

 // Override local port.
 commSpi.setLocalPort(4321);

 GridConfigurationAdapter cfg = new GridConfigurationAdapter();

 // Override default communication SPI.
 cfg.setCommunicationSpi(commSpi);

 // Start grid.
 GridFactory.start(cfg);
 

Spring Example

GridTcpCommunicationSpi can be configured from Spring XML configuration file:
 <bean id="grid.custom.cfg" class="org.gridgain.grid.GridConfigurationAdapter" singleton="true">
         ...
         <property name="communicationSpi">
             <bean class="org.gridgain.grid.spi.communication.tcp.GridTcpCommunicationSpi">
                 <!-- Override local port. -->
                 <property name="localPort" value="4321"/>
             </bean>
         </property>
         ...
 </bean>
 


For information about Spring framework visit www.springframework.org



See Also:

  Documentation
  Email Support
  Online Forums
  Issue Tracking

Author:   2005-2008 Copyright © GridGain Systems. All Rights Reserved. ver. 2.1.0

See Also:
GridCommunicationSpi
 

Field Summary
static String ATTR_ADDR
          Node attribute that is mapped to node IP address (value is comm.tcp.addr).
static String ATTR_PORT
          Node attribute that is mapped to node port number (value is comm.tcp.port).
static String CERTIFICATE_SERVER_CN
          Certificate common name (value is org.gridgain.grid.spi.communication.tcp.GridTcpCommunicationSpi.tcp-communication-CA).
static int DFLT_IDLE_CONN_TIMEOUT
          Default idle connection timeout (value is 30000ms).
static int DFLT_MSG_THREADS
          Number of threads responsible for handling messages.
static int DFLT_PORT
          Default port which node sets listener to (value is 47100).
static int DFLT_PORT_RANGE
          Default local port range (value is 100).
 
Constructor Summary
GridTcpCommunicationSpi()
           
 
Method Summary
protected  List<String> getConsistentAttributeNames()
          Returns back list of attributes that should be consistent for this SPI.
 long getIdleConnectionTimeout()
          Gets maximum idle connection time upon which idle connections will be closed.
 String getLocalAddress()
          Gets local host address for socket binding.
 int getLocalPort()
          Gets local port for socket binding.
 int getLocalPortRange()
          Gets maximum number of local ports tried if all previously tried ports are occupied.
 int getMessageThreads()
          Gets number of threads used for handling NIO messages.
 int getNioActiveThreadCount()
          Returns the approximate number of threads that are actively processing NIO tasks.
 int getNioCorePoolSize()
          Returns the core number of NIO threads.
 int getNioLargestPoolSize()
          Returns the largest number of NIO threads that have ever simultaneously been in the pool.
 int getNioMaximumPoolSize()
          Returns the maximum allowed number of NIO threads.
 int getNioPoolSize()
          Returns the current number of NIO threads in the pool.
 int getNioTaskQueueSize()
          Gets current size of the NIO queue size.
 long getNioTotalCompletedTaskCount()
          Returns the approximate total number of NIO tasks that have completed execution.
 long getNioTotalScheduledTaskCount()
          Returns the approximate total number of NIO tasks that have been scheduled for execution.
 Map<String,Serializable> getNodeAttributes()
          This method is called before SPI starts (before method GridSpi.spiStart(String) is called). It allows SPI implementation to add attributes to a local node. Kernel collects these attributes from all SPI implementations loaded up and then passes it to discovery SPI so that they can be exchanged with other nodes.
 boolean isDirectBuffer()
          Gets flag that indicates whether direct or heap allocated buffer is used.
 void sendMessage(Collection<GridNode> destNodes, Serializable msg)
          Sends given message to destination nodes.
 void sendMessage(GridNode destNode, Serializable msg)
          Sends given message to destination node.
 void setDirectBuffer(boolean directBuf)
          Sets flag to allocate direct or heap buffer in SPI.
 void setIdleConnectionTimeout(long idleConnTimeout)
          Sets maximum idle connection timeout upon which a connection to client will be closed.
 void setListener(GridMessageListener listener)
          Set communication listener.
 void setLocalAddress(String localAddr)
          Sets local host address for socket binding.
 void setLocalPort(int localPort)
          Sets local port for socket binding.
 void setLocalPortRange(int localPortRange)
          Sets local port range for local host ports (value must greater than or equal to 0).
 void setMessageThreads(int msgThreads)
          Number of threads used for handling messages received by NIO server.
 void spiStart(String gridName)
          This method is called to start SPI.
 void spiStop()
          This method is called to stop SPI.
 String toString()
          
 
Methods inherited from class org.gridgain.grid.spi.GridSpiAdapter
assertParameter, configInfo, createSpiAttributeName, getAuthor, getGridGainHome, getLocalNodeId, getName, getSpiContext, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, getVendorEmail, getVendorUrl, getVersion, onContextDestroyed, onContextInitialized, registerMBean, setName, setSpiContext, startInfo, startStopwatch, stopInfo, unregisterMBean
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.gridgain.grid.spi.GridSpi
getName, onContextDestroyed, onContextInitialized
 
Methods inherited from interface org.gridgain.grid.spi.GridSpiManagementMBean
getAuthor, getGridGainHome, getLocalNodeId, getName, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, getVendorEmail, getVendorUrl, getVersion
 

Field Detail

CERTIFICATE_SERVER_CN

public static final String CERTIFICATE_SERVER_CN
Certificate common name (value is org.gridgain.grid.spi.communication.tcp.GridTcpCommunicationSpi.tcp-communication-CA).


DFLT_MSG_THREADS

public static final int DFLT_MSG_THREADS
Number of threads responsible for handling messages.

See Also:
Constant Field Values

ATTR_ADDR

public static final String ATTR_ADDR
Node attribute that is mapped to node IP address (value is comm.tcp.addr).

See Also:
Constant Field Values

ATTR_PORT

public static final String ATTR_PORT
Node attribute that is mapped to node port number (value is comm.tcp.port).

See Also:
Constant Field Values

DFLT_PORT

public static final int DFLT_PORT
Default port which node sets listener to (value is 47100).

See Also:
Constant Field Values

DFLT_IDLE_CONN_TIMEOUT

public static final int DFLT_IDLE_CONN_TIMEOUT
Default idle connection timeout (value is 30000ms).

See Also:
Constant Field Values

DFLT_PORT_RANGE

public static final int DFLT_PORT_RANGE
Default local port range (value is 100). See GridTcpCommunicationSpi.setLocalPortRange(int) for details.

See Also:
Constant Field Values
Constructor Detail

GridTcpCommunicationSpi

public GridTcpCommunicationSpi()
Method Detail

setLocalAddress

@GridSpiConfiguration(optional=true)
public void setLocalAddress(String localAddr)
Sets local host address for socket binding. Note that one node could have additional addresses beside the loopback one. This configuration parameter is optional.

Parameters:
localAddr - IP address. Default value is any available local IP address.

getLocalAddress

public String getLocalAddress()
Gets local host address for socket binding. Beside loopback address physical node could have several other ones, but only one is assigned to grid node.

Specified by:
getLocalAddress in interface GridTcpCommunicationSpiMBean
Returns:
Grid node IP address.

setMessageThreads

@GridSpiConfiguration(optional=true)
public void setMessageThreads(int msgThreads)
Number of threads used for handling messages received by NIO server. This number usually should be no less than number of CPUs.

If not provided, default value is GridTcpCommunicationSpi.DFLT_MSG_THREADS.

Parameters:
msgThreads - Number of threads.

getMessageThreads

public int getMessageThreads()
Gets number of threads used for handling NIO messages.

Specified by:
getMessageThreads in interface GridTcpCommunicationSpiMBean
Returns:
Number of threads used for handling NIO messages.

setLocalPort

@GridSpiConfiguration(optional=true)
public void setLocalPort(int localPort)
Sets local port for socket binding.

If not provided, default value is GridTcpCommunicationSpi.DFLT_PORT.

Parameters:
localPort - Port number.

getLocalPort

public int getLocalPort()
Gets local port for socket binding.

Specified by:
getLocalPort in interface GridTcpCommunicationSpiMBean
Returns:
Port number.

setLocalPortRange

@GridSpiConfiguration(optional=true)
public void setLocalPortRange(int localPortRange)
Sets local port range for local host ports (value must greater than or equal to 0). If provided local port (see GridTcpCommunicationSpi.setLocalPort(int)} is occupied, implementation will try to increment the port number for as long as it is less than initial value plus this range.

If port range value is 0, then implementation will try bind only to the port provided by GridTcpCommunicationSpi.setLocalPort(int) method and fail if binding to this port did not succeed.

Local port range is very useful during development when more than one grid nodes need to run on the same physical machine.

If not provided, default value is GridTcpCommunicationSpi.DFLT_PORT_RANGE.

Parameters:
localPortRange - New local port range.

getLocalPortRange

public int getLocalPortRange()
Gets maximum number of local ports tried if all previously tried ports are occupied.

Specified by:
getLocalPortRange in interface GridTcpCommunicationSpiMBean
Returns:
Local port range.

setIdleConnectionTimeout

@GridSpiConfiguration(optional=true)
public void setIdleConnectionTimeout(long idleConnTimeout)
Sets maximum idle connection timeout upon which a connection to client will be closed.

If not provided, default value is GridTcpCommunicationSpi.DFLT_IDLE_CONN_TIMEOUT.

Parameters:
idleConnTimeout - Maximum idle connection time.

getIdleConnectionTimeout

public long getIdleConnectionTimeout()
Gets maximum idle connection time upon which idle connections will be closed.

Specified by:
getIdleConnectionTimeout in interface GridTcpCommunicationSpiMBean
Returns:
Maximum idle connection time.

setDirectBuffer

@GridSpiConfiguration(optional=true)
public void setDirectBuffer(boolean directBuf)
Sets flag to allocate direct or heap buffer in SPI. If value is true, then SPI will use ByteBuffer.allocateDirect(int) call. Otherwise, SPI will use ByteBuffer.allocate(int) call.

If not provided, default value is true.

Parameters:
directBuf - Flag indicates to allocate direct or heap buffer in SPI.

isDirectBuffer

public boolean isDirectBuffer()
Gets flag that indicates whether direct or heap allocated buffer is used.

Specified by:
isDirectBuffer in interface GridTcpCommunicationSpiMBean
Returns:
Flag that indicates whether direct or heap allocated buffer is used.

setListener

public void setListener(GridMessageListener listener)
Set communication listener.

Specified by:
setListener in interface GridCommunicationSpi
Parameters:
listener - Listener to set or null to unset the listener.

getNioActiveThreadCount

public int getNioActiveThreadCount()
Returns the approximate number of threads that are actively processing NIO tasks.

Specified by:
getNioActiveThreadCount in interface GridTcpCommunicationSpiMBean
Returns:
Approximate number of threads that are actively processing NIO tasks.

getNioTotalCompletedTaskCount

public long getNioTotalCompletedTaskCount()
Returns the approximate total number of NIO tasks that have completed execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls.

Specified by:
getNioTotalCompletedTaskCount in interface GridTcpCommunicationSpiMBean
Returns:
Approximate total number of NIO tasks that have completed execution.

getNioCorePoolSize

public int getNioCorePoolSize()
Returns the core number of NIO threads.

Specified by:
getNioCorePoolSize in interface GridTcpCommunicationSpiMBean
Returns:
Core number of NIO threads.

getNioLargestPoolSize

public int getNioLargestPoolSize()
Returns the largest number of NIO threads that have ever simultaneously been in the pool.

Specified by:
getNioLargestPoolSize in interface GridTcpCommunicationSpiMBean
Returns:
Largest number of NIO threads that have ever simultaneously been in the pool.

getNioMaximumPoolSize

public int getNioMaximumPoolSize()
Returns the maximum allowed number of NIO threads.

Specified by:
getNioMaximumPoolSize in interface GridTcpCommunicationSpiMBean
Returns:
Maximum allowed number of NIO threads.

getNioPoolSize

public int getNioPoolSize()
Returns the current number of NIO threads in the pool.

Specified by:
getNioPoolSize in interface GridTcpCommunicationSpiMBean
Returns:
Current number of NIO threads in the pool.

getNioTotalScheduledTaskCount

public long getNioTotalScheduledTaskCount()
Returns the approximate total number of NIO tasks that have been scheduled for execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls.

Specified by:
getNioTotalScheduledTaskCount in interface GridTcpCommunicationSpiMBean
Returns:
Approximate total number of NIO tasks that have been scheduled for execution.

getNioTaskQueueSize

public int getNioTaskQueueSize()
Gets current size of the NIO queue size. NIO queue buffers NIO tasks when there are not threads available for processing in the pool.

Specified by:
getNioTaskQueueSize in interface GridTcpCommunicationSpiMBean
Returns:
Current size of the NIO queue size.

getNodeAttributes

public Map<String,Serializable> getNodeAttributes()
                                           throws GridSpiException
This method is called before SPI starts (before method GridSpi.spiStart(String) is called). It allows SPI implementation to add attributes to a local node. Kernel collects these attributes from all SPI implementations loaded up and then passes it to discovery SPI so that they can be exchanged with other nodes.

Specified by:
getNodeAttributes in interface GridSpi
Overrides:
getNodeAttributes in class GridSpiAdapter
Throws:
GridSpiException - Throws in case of any error.
Returns:
Map of local node attributes this SPI wants to add.

spiStart

public void spiStart(String gridName)
              throws GridSpiException
This method is called to start SPI. After this method returns successfully kernel assumes that SPI is fully operational.

Specified by:
spiStart in interface GridSpi
Throws:
GridSpiException - Throws in case of any error during SPI start.
Parameters:
gridName - Name of grid instance this SPI is being started for (null for default grid).

spiStop

public void spiStop()
             throws GridSpiException
This method is called to stop SPI. After this method returns kernel assumes that this SPI is finished and all resources acquired by it are released. Note that this method can be called at any point including during recovery of failed start. It should make no assumptions on what state SPI will be in when this method is called.

Specified by:
spiStop in interface GridSpi
Throws:
GridSpiException - Thrown in case of any error during SPI stop.

sendMessage

public void sendMessage(GridNode destNode,
                        Serializable msg)
                 throws GridSpiException
Sends given message to destination node. Note that characteristics of the exchange such as durability, guaranteed delivery or error notification is dependant on SPI implementation.

Specified by:
sendMessage in interface GridCommunicationSpi
Throws:
GridSpiException - Thrown in case of any error during sending the message. Note that this is not guaranteed that failed communication will result in thrown exception as this is dependant on SPI implementation.
Parameters:
destNode - Destination node.
msg - Message to send.

sendMessage

public void sendMessage(Collection<GridNode> destNodes,
                        Serializable msg)
                 throws GridSpiException
Sends given message to destination nodes. Note that characteristics of the exchange such as durability, guaranteed delivery or error notification is dependant on SPI implementation.

Specified by:
sendMessage in interface GridCommunicationSpi
Throws:
GridSpiException - Thrown in case of any error during sending the message. Note that this is not guaranteed that failed communication will result in thrown exception as this is dependant on SPI implementation.
Parameters:
destNodes - Destination nodes.
msg - Message to send.

getConsistentAttributeNames

protected List<String> getConsistentAttributeNames()
Returns back list of attributes that should be consistent for this SPI. Consistency means that remote node has to have the same attribute with the same value.

Overrides:
getConsistentAttributeNames in class GridSpiAdapter
Returns:
List or attribute names.

toString

public String toString()

Overrides:
toString in class Object

GridGain™ 2.1.0
Java API Specification

GridGain™ - Grid Computing Made Simple, ver. 2.1.0.19122008
2005-2008 Copyright © GridGain Systems. All Rights Reserved.