Class SocketAcceptor

java.lang.Object
org.dellroad.stuff.net.SocketAcceptor

public abstract class SocketAcceptor extends Object
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 Details

  • Constructor Details

    • SocketAcceptor

      public SocketAcceptor()
  • Method Details

    • 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.
      Returns:
      maximum backlogged connections
    • 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.
      Returns:
      TCP listening port
    • setPort

      public void setPort(int port)
    • start

      public void start()
      Verifies configuration and starts accepting incoming connections. Does nothing if already started.
      Throws:
      RuntimeException - if an I/O error occurs setting up the server socket
      IllegalArgumentException - if configuration is invalid
    • 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.

      Returns:
      server socket
      Throws:
      IOException - if an I/O error occurs
    • getSocketHandler

      protected abstract SocketHandler getSocketHandler(Socket socket) throws IOException
      Get the SocketHandler that will handle a new connection using the given socket.
      Parameters:
      socket - connection socket
      Returns:
      new handler, or null to disconnect the socket immediately
      Throws:
      IOException - if an I/O error occurs