Package org.jgroups.blocks.cs
Class NioServer
- java.lang.Object
-
- org.jgroups.blocks.cs.BaseServer
-
- org.jgroups.blocks.cs.NioBaseServer
-
- org.jgroups.blocks.cs.NioServer
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,ConnectionListener
public class NioServer extends NioBaseServer
Server for sending and receiving messages via NIO channels. Uses only a single thread to accept, connect, write and read connections. Read messages are passed to a receiver, which typically uses a thread pool to process messages. Note that writes can get dropped, e.g. in the case where we have a previous write pending and a new write is received. This is typically not an issue as JGroups retransmits messages, but might become one when using NioServer standalone, ie. outside of JGroups.- Since:
- 3.6.5
- Author:
- Bela Ban
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.jgroups.blocks.cs.NioBaseServer
NioBaseServer.Acceptor
-
Nested classes/interfaces inherited from class org.jgroups.blocks.cs.BaseServer
BaseServer.Reaper
-
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.channels.ServerSocketChannel
channel
-
Fields inherited from class org.jgroups.blocks.cs.NioBaseServer
acceptor, copy_on_partial_write, max_send_buffers, num_selects, reader_idle_time, reg_lock, registration, selector
-
Fields inherited from class org.jgroups.blocks.cs.BaseServer
client_bind_addr, client_bind_port, conn_expire_time, conn_listeners, conns, defer_client_binding, factory, linger, local_addr, log, max_length, reaper, reaperInterval, receiver, recv_buf_size, running, send_buf_size, sock_conn_timeout, sock_creation_lock, socket_factory, tcp_nodelay, time_service, use_peer_connections
-
-
Constructor Summary
Constructors Modifier Constructor Description NioServer(java.net.InetAddress bind_addr, int port)
Creates an instance ofNioServer
that opens a server channel and listens for connections.NioServer(IpAddress bind_addr)
Creates an instance of NioServer.protected
NioServer(ThreadFactory f, SocketFactory sf)
NioServer(ThreadFactory thread_factory, SocketFactory socket_factory, java.net.InetAddress bind_addr, int srv_port, int end_port, java.net.InetAddress external_addr, int external_port)
Creates an instance ofNioServer
that opens a server channel and listens for connections.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
acceptorDone()
protected void
handleAccept(java.nio.channels.SelectionKey key)
void
start()
Starts accepting connections.void
stop()
Stops listening for connections and handling traffic.-
Methods inherited from class org.jgroups.blocks.cs.NioBaseServer
acceptorRunning, copyOnPartialWrite, copyOnPartialWrite, createConnection, maxSendBuffers, maxSendBuffers, numPartialWrites, numSelects, printBuffers, readerIdleTime, readerIdleTime, register, selectorOpen
-
Methods inherited from class org.jgroups.blocks.cs.BaseServer
addConnection, addConnectionListener, clearConnections, clientBindAddress, clientBindAddress, clientBindPort, clientBindPort, close, closeConnection, connectionClosed, connectionEstablished, connectionEstablishedTo, connExpireTime, connExpireTimeout, deferClientBinding, deferClientBinding, explanation, getConnection, getMaxLength, getNumConnections, getNumOpenConnections, hasConnection, linger, linger, localAddress, localAddress, log, log, notifyConnectionClosed, notifyConnectionEstablished, printConnections, reaperInterval, reaperInterval, receive, receive, receive, receiveBufferSize, receiveBufferSize, receiver, receiver, removeConnectionIfPresent, removeConnectionListener, replaceConnection, retainAll, running, send, send, sendBufferSize, sendBufferSize, sendToAll, sendToAll, setMaxLength, socketConnectionTimeout, socketConnectionTimeout, socketFactory, socketFactory, tcpNodelay, tcpNodelay, timeService, timeService, toString, usePeerConnections, usePeerConnections, validateArgs
-
-
-
-
Constructor Detail
-
NioServer
public NioServer(IpAddress bind_addr) throws java.lang.Exception
Creates an instance of NioServer.- Parameters:
bind_addr
- The local bind address and port. If null, a bind address and port will be picked by the OS.- Throws:
java.lang.Exception
-
NioServer
public NioServer(java.net.InetAddress bind_addr, int port) throws java.lang.Exception
Creates an instance ofNioServer
that opens a server channel and listens for connections. Needs to be started next.- Parameters:
bind_addr
- The local address to bind to. If null, the address will be picked by the OSport
- The local port to bind to- Throws:
java.lang.Exception
- Thrown if the creation failed
-
NioServer
public NioServer(ThreadFactory thread_factory, SocketFactory socket_factory, java.net.InetAddress bind_addr, int srv_port, int end_port, java.net.InetAddress external_addr, int external_port) throws java.lang.Exception
Creates an instance ofNioServer
that opens a server channel and listens for connections. Needs to be started next.- Parameters:
thread_factory
- The thread factory used to create new threadssocket_factory
- The socket factory used to create socket channelsbind_addr
- The local address to bind to. If null, the address will be picked by the OSsrv_port
- The local port to bind to If 0, the port will be picked by the OS.end_port
- If srv_port is taken, the next port is tried, until end_port has been reached, in which case an exception will be thrown. If srv_port == end_port, only 1 port will be tried.external_addr
- The external address in case of NAT. Ignored if null.external_port
- The external port on the NA. If 0, srv_port is used.- Throws:
java.lang.Exception
- Thrown if the creation failed
-
NioServer
protected NioServer(ThreadFactory f, SocketFactory sf)
-
-
Method Detail
-
handleAccept
protected void handleAccept(java.nio.channels.SelectionKey key) throws java.lang.Exception
- Overrides:
handleAccept
in classNioBaseServer
- Throws:
java.lang.Exception
-
start
public void start() throws java.lang.Exception
Description copied from class:BaseServer
Starts accepting connections. Typically, socket handler or selectors thread are started here.- Overrides:
start
in classBaseServer
- Throws:
java.lang.Exception
-
stop
public void stop()
Description copied from class:BaseServer
Stops listening for connections and handling traffic. Typically, socket handler or selector threads are stopped, and server sockets or channels are closed.- Overrides:
stop
in classBaseServer
-
acceptorDone
protected void acceptorDone()
- Overrides:
acceptorDone
in classNioBaseServer
-
-