org.dellroad.stuff.net
Class SocketAcceptor

java.lang.Object
  extended by org.dellroad.stuff.net.SocketAcceptor
All Implemented Interfaces:
DisposableBean, InitializingBean

public abstract class SocketAcceptor
extends Object
implements InitializingBean, DisposableBean

Spring bean that listens for connections on a TCP socket and spawns a child thread to handle each new connection. Subclasses must implement getSocketHandler(java.net.Socket). Only the port property is required to be set.


Field Summary
static int DEFAULT_BACKLOG
          Default maximum incoming connection queue length (50).
protected  Logger log
           
 
Constructor Summary
SocketAcceptor()
           
 
Method Summary
 void afterPropertiesSet()
          Verifies configuration and invokes start().
protected  ServerSocket createServerSocket()
          Create the server's socket via which connections will be accepted.
 void destroy()
          Invokes stop().
 int getBacklog()
          Get maximum connect backlog.
 InetAddress getInetAddress()
          Get address to listen on.
 int getMaxConnections()
          Get maximum number of concurrent connections.
 int getPort()
          Get TCP port to listen on.
protected abstract  SocketHandler getSocketHandler(Socket socket)
          Get the SocketHandler that will handle a new connection using the given socket.
 void setBacklog(int backlog)
           
 void setInetAddress(InetAddress address)
           
 void setMaxConnections(int maxConnections)
           
 void setPort(int port)
           
 void start()
          Start accepting incoming connections.
 void stop()
          Stop accepting connections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BACKLOG

public static final int DEFAULT_BACKLOG
Default maximum incoming connection queue length (50).

See Also:
setBacklog(int), Constant Field Values

log

protected final Logger log
Constructor Detail

SocketAcceptor

public SocketAcceptor()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Verifies configuration and invokes start().

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

destroy

public void destroy()
             throws Exception
Invokes stop().

Specified by:
destroy in interface DisposableBean
Throws:
Exception

getInetAddress

public InetAddress getInetAddress()
Get address to listen on.

Returns:
address to listen on, or null for any

setInetAddress

public void setInetAddress(InetAddress address)

getBacklog

public int getBacklog()
Get maximum connect backlog.


setBacklog

public void setBacklog(int backlog)

getMaxConnections

public int getMaxConnections()
Get maximum number of concurrent connections.

Returns:
max conncurrent connections, or zero for unlimited

setMaxConnections

public void setMaxConnections(int maxConnections)

getPort

public int getPort()
Get TCP port to listen on.


setPort

public void setPort(int port)

start

public void start()
           throws IOException
Start accepting incoming connections. Does nothing if already started.

Throws:
IOException

stop

public void stop()
Stop accepting connections. Does nothing if already stopped.

Any currently active connections are stopped via SocketHandler.stop(), and this method waits until all such connections have returned from SocketHandler.handleConnection() before returning.


createServerSocket

protected ServerSocket createServerSocket()
                                   throws IOException
Create the server's socket via which connections will be accepted.

The implementation in SocketAcceptor creates the socket and sets the "reuse address" flag. Subclasses may override.

Throws:
IOException

getSocketHandler

protected abstract SocketHandler getSocketHandler(Socket socket)
                                           throws IOException
Get the SocketHandler that will handle a new connection using the given socket.

Returns:
new handler, or null to disconnect the socket immediately
Throws:
IOException