Class Connection

java.lang.Object
com.codingchili.core.listener.transport.Connection
All Implemented Interfaces:
Messageable

public class Connection extends Object implements Messageable
Represents the stateful connection over which requests may be passed. For stateless requests such as ClusterRequest and RestRequest the client may only live during a single request.
  • Constructor Details

    • Connection

      public Connection(Consumer<Object> writer, String id)
      Creates a new stateful connection that properly implements the ID method.
      Parameters:
      writer - a method that writes outbound messages to the remote peer.
      id - the unique id of this connection.
  • Method Details

    • write

      public void write(Object object)
      Description copied from interface: Messageable
      Writes an object to the connection that backs the current request.
      Specified by:
      write in interface Messageable
      Parameters:
      object - the object to be written.
    • id

      public String id()
      Returns:
      the unique identifier of this connection.
    • remote

      public String remote()
      A textual representation of the sending party. This is typically the IP address retrieved from the underlying connection.
      Returns:
      the sending party represented as a string.
    • onCloseHandler

      public Connection onCloseHandler(Runnable runnable)
      Creates a unnamed close handler that cannot be removed.
      Parameters:
      runnable - called after the connection is closed.
      Returns:
      fluent.
    • onCloseHandler

      public Connection onCloseHandler(String name, Runnable closeHandler)
      Adds a listener for the close event.
      Parameters:
      name - the name of the close handler so that it can be removed or updated.
      closeHandler - called after the connection is closed.
      Returns:
      fluent.
    • removeCloseHandler

      public Connection removeCloseHandler(String name)
      Parameters:
      name - the name of the close handler to remove.
      Returns:
      fluent.
    • runCloseHandlers

      public void runCloseHandlers()
      executes the close handlers registered on the connection. Does not actually close the connection itself. This method is primarily intended to be called by the listener which created the connection.
    • getProperty

      public Optional<String> getProperty(String key)
      Retrieves a value from the connection properties.
      Parameters:
      key - the property to retrieve.
      Returns:
      an optional of the property value.
    • setProperty

      public Connection setProperty(String key, String value)
      Sets a property that will exist for the connections duration.
      Parameters:
      key - the key of the property to set.
      value - the value of the property to set.
      Returns:
      fluent.