Home / Class/ AbstractChannelPoolMap Class — netty Architecture

AbstractChannelPoolMap Class — netty Architecture

Architecture documentation for the AbstractChannelPoolMap class in AbstractChannelPoolMap.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec["AbstractChannelPoolMap"]
  9a52ffc8_8167_deec_7cfb_01b9e59a1e46["AbstractChannelPoolMap.java"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|defined in| 9a52ffc8_8167_deec_7cfb_01b9e59a1e46
  1e58479a_dcfb_c822_0599_f9335753d348["P()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 1e58479a_dcfb_c822_0599_f9335753d348
  979f94b2_e67e_490b_3cac_1bceb7ad4cdb["remove()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 979f94b2_e67e_490b_3cac_1bceb7ad4cdb
  c0c11a10_371e_2cad_4b08_83e1dfa3ce8f["removeAsyncIfSupported()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| c0c11a10_371e_2cad_4b08_83e1dfa3ce8f
  5cdb479c_dffc_4a55_7dac_6d05b82ff9be["poolCloseAsyncIfSupported()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 5cdb479c_dffc_4a55_7dac_6d05b82ff9be
  2fd115c2_f24e_2a1d_b502_36137de657ab["iterator()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 2fd115c2_f24e_2a1d_b502_36137de657ab
  992e29b7_5a1f_d6a5_31bd_3930932ed87b["size()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 992e29b7_5a1f_d6a5_31bd_3930932ed87b
  5ba42d32_e54c_b895_6ad6_a7174250bfb2["isEmpty()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 5ba42d32_e54c_b895_6ad6_a7174250bfb2
  a7428b20_77f9_3cae_2e48_1e091874614f["contains()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| a7428b20_77f9_3cae_2e48_1e091874614f
  5d77c1c4_7bff_ebd6_11b7_c38e5d60916e["close()"]
  dcdf1935_bc76_f2a3_9079_ea2dfd85abec -->|method| 5d77c1c4_7bff_ebd6_11b7_c38e5d60916e

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/pool/AbstractChannelPoolMap.java lines 35–149

public abstract class AbstractChannelPoolMap<K, P extends ChannelPool>
        implements ChannelPoolMap<K, P>, Iterable<Entry<K, P>>, Closeable {
    private final ConcurrentMap<K, P> map = new ConcurrentHashMap<>();

    @Override
    public final P get(K key) {
        P pool = map.get(checkNotNull(key, "key"));
        if (pool == null) {
            pool = newPool(key);
            P old = map.putIfAbsent(key, pool);
            if (old != null) {
                // We need to destroy the newly created pool as we not use it.
                poolCloseAsyncIfSupported(pool);
                pool = old;
            }
        }
        return pool;
    }
    /**
     * Remove the {@link ChannelPool} from this {@link AbstractChannelPoolMap}. Returns {@code true} if removed,
     * {@code false} otherwise.
     *
     * If the removed pool extends {@link SimpleChannelPool} it will be closed asynchronously to avoid blocking in
     * this method.
     *
     * Please note that {@code null} keys are not allowed.
     */
    public final boolean remove(K key) {
        P pool =  map.remove(checkNotNull(key, "key"));
        if (pool != null) {
            poolCloseAsyncIfSupported(pool);
            return true;
        }
        return false;
    }

    /**
     * Remove the {@link ChannelPool} from this {@link AbstractChannelPoolMap}. Returns a future that comletes with a
     * {@code true} result if the pool has been removed by this call, otherwise the result is {@code false}.
     *
     * If the removed pool extends {@link SimpleChannelPool} it will be closed asynchronously to avoid blocking in
     * this method. The returned future will be completed once this asynchronous pool close operation completes.
     */
    private Future<Boolean> removeAsyncIfSupported(K key) {
        P pool =  map.remove(checkNotNull(key, "key"));
        if (pool != null) {
            final Promise<Boolean> removePromise = GlobalEventExecutor.INSTANCE.newPromise();
            poolCloseAsyncIfSupported(pool).addListener(future -> {
                if (future.isSuccess()) {
                    removePromise.setSuccess(Boolean.TRUE);
                } else {
                    removePromise.setFailure(future.cause());
                }
            });
            return removePromise;
        }
        return GlobalEventExecutor.INSTANCE.newSucceededFuture(Boolean.FALSE);
    }

    /**
     * If the pool implementation supports asynchronous close, then use it to avoid a blocking close call in case
     * the ChannelPoolMap operations are called from an EventLoop.
     *
     * @param pool the ChannelPool to be closed
     */
    private static Future<Void> poolCloseAsyncIfSupported(ChannelPool pool) {
        if (pool instanceof SimpleChannelPool) {
            return ((SimpleChannelPool) pool).closeAsync();
        } else {
            try {
                pool.close();
                return GlobalEventExecutor.INSTANCE.newSucceededFuture(null);
            } catch (Exception e) {
                return GlobalEventExecutor.INSTANCE.newFailedFuture(e);
            }
        }
    }

    @Override
    public final Iterator<Entry<K, P>> iterator() {
        return new ReadOnlyIterator<Entry<K, P>>(map.entrySet().iterator());

Frequently Asked Questions

What is the AbstractChannelPoolMap class?
AbstractChannelPoolMap is a class in the netty codebase, defined in transport/src/main/java/io/netty/channel/pool/AbstractChannelPoolMap.java.
Where is AbstractChannelPoolMap defined?
AbstractChannelPoolMap is defined in transport/src/main/java/io/netty/channel/pool/AbstractChannelPoolMap.java at line 35.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free