All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dubboclub.http.netty.IdleTask Maven / Gradle / Ivy

package net.dubboclub.http.netty;


import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by bieber on 16/3/8.
 */
public class IdleTask implements Runnable {

    private ConcurrentHashMap holderMapping;

    private long timeout;

    public IdleTask(long timeout, ConcurrentHashMap holderMapping) {
        this.timeout = timeout;
        this.holderMapping = holderMapping;
    }

    @Override
    public void run() {
        Collection channelHolderCollection = holderMapping.values();
        for (ChannelHolder channelHolder : channelHolderCollection) {
            long lastReadInterval =System.currentTimeMillis()-channelHolder.lastReadTime();
            long lastWriteInterval = System.currentTimeMillis()-channelHolder.lastWriteTime();
            if(lastReadInterval>=timeout&&lastWriteInterval>=timeout){
                channelHolder.channel().close().addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        holderMapping.remove(future.channel());
                    }
                });
            }
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy