Example usage for io.netty.util Timer newTimeout

List of usage examples for io.netty.util Timer newTimeout

Introduction

In this page you can find the example usage for io.netty.util Timer newTimeout.

Prototype

Timeout newTimeout(TimerTask task, long delay, TimeUnit unit);

Source Link

Document

Schedules the specified TimerTask for one-time execution after the specified delay.

Usage

From source file:com.king.platform.net.http.netty.pool.PoolingChannelPool.java

License:Apache License

public PoolingChannelPool(final Timer cleanupTimer, TimeProvider timeProvider, long timeoutInSeconds,
        final MetricCallback metricCallback) {
    this.timeProvider = timeProvider;
    this.metricCallback = metricCallback;

    maxTtl = TimeUnit.SECONDS.toMillis(timeoutInSeconds);

    cleanupTimer.newTimeout(new TimerTask() {
        @Override//from   w w w .j ava 2  s.  c o  m
        public void run(Timeout timeout) throws Exception {

            for (Map.Entry<ServerInfo, ServerPool> poolEntry : serverPoolMap.entrySet()) {
                ServerPool serverPool = poolEntry.getValue();
                ServerInfo serverInfo = poolEntry.getKey();

                serverPool.cleanExpiredConnections();
                if (serverPool.shouldRemovePool()) {
                    ServerPool remove = serverPoolMap.remove(serverInfo);
                    if (remove != null) {
                        metricCallback.onRemovedServerPool(serverInfo.getHost());
                    }
                }

            }

            cleanupTimer.newTimeout(timeout.task(), maxTtl, TimeUnit.MILLISECONDS);

        }
    }, maxTtl, TimeUnit.MILLISECONDS);
}