Interface ConnectionHandler


public interface ConnectionHandler
Callback interface for handling connection pool acquisition events.

This interface receives notifications when connections are acquired from or fail to be acquired from a ConnectionPool. It enables asynchronous connection acquisition without blocking the calling thread.

Usage with Connection Pool

ConnectionPool pool = TransportFactory.createPool(config);

long token = pool.acquire(serverAddress, new ConnectionHandler() {
    @Override
    public void onConnectionAcquired(Transport transport, long token) {
        // Use the transport for I/O
        transport.send(data);
    }

    @Override
    public void onConnectionFailed(Throwable cause, long token) {
        logger.error("Failed to acquire connection", cause);
    }
});

Thread Safety

Callbacks may be invoked from the connection pool's I/O thread or from the thread that called ConnectionPool.acquire(SocketAddress, ConnectionHandler). Implementations must be thread-safe.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    onConnectionAcquired(Transport transport, long token)
    Called when a connection is successfully acquired from the pool.
    void
    onConnectionFailed(Throwable cause, long token)
    Called when connection acquisition fails.
  • Method Details

    • onConnectionAcquired

      void onConnectionAcquired(Transport transport, long token)
      Called when a connection is successfully acquired from the pool.

      The transport is ready for I/O operations. When finished, call ConnectionPool.release(Transport) to return it to the pool.

      Parameters:
      transport - the acquired transport instance, ready for use
      token - the operation token from the ConnectionPool.acquire(SocketAddress, ConnectionHandler) call
    • onConnectionFailed

      void onConnectionFailed(Throwable cause, long token)
      Called when connection acquisition fails.

      Common failure causes include:

      • Connection pool exhausted (max connections per host reached)
      • Connection refused by remote host
      • Connection timeout
      • Network unreachable
      Parameters:
      cause - the exception describing the failure reason
      token - the operation token from the ConnectionPool.acquire(SocketAddress, ConnectionHandler) call