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

one.nio.server.CleanupThread Maven / Gradle / Ivy

/*
 * Copyright 2015-2016 Odnoklassniki Ltd, Mail.Ru Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package one.nio.server;

import one.nio.net.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CleanupThread extends Thread {
    private static final Log log = LogFactory.getLog(CleanupThread.class);

    private volatile SelectorThread[] selectors;
    private volatile long keepAlive;

    public CleanupThread(SelectorThread[] selectors, long keepAlive) {
        super("NIO Cleanup");
        this.selectors = selectors;
        setKeepAlive(keepAlive);
    }

    public void shutdown() {
        interrupt();
        try {
            join();
        } catch (InterruptedException e) {
            // Ignore
        }
    }

    public long getKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(long keepAlive) {
        if (keepAlive > 0 && keepAlive < 1000) {
            log.warn("Suspicious keepAlive! Consider specifying time units (ms, s)");
            keepAlive *= 1000;
        }
        this.keepAlive = keepAlive;
    }

    public synchronized void update(SelectorThread[] selectors, long keepAlive) {
        this.selectors = selectors;
        setKeepAlive(keepAlive);
        notify();
    }

    private synchronized long waitKeepAlive() throws InterruptedException {
        long keepAlive = this.keepAlive;
        wait(keepAlive);
        return keepAlive;
    }

    @Override
    public void run() {
        while (!isInterrupted()) {
            try {
                long keepAlive = waitKeepAlive();
                if (keepAlive == 0) {
                    continue;
                }

                long cleanTime = System.currentTimeMillis();
                int idleCount = 0;
                int staleCount = 0;

                for (SelectorThread selector : selectors) {
                    for (Session session : selector.selector) {
                        int status = session.checkStatus(cleanTime, keepAlive);
                        if (status != Session.ACTIVE) {
                            if (status == Session.IDLE) {
                                idleCount++;
                            } else {
                                staleCount++;
                            }
                            session.close();
                        }
                    }
                }

                if (log.isInfoEnabled() && idleCount + staleCount > 0) {
                    log.info(idleCount + " idle + " + staleCount + " stale sessions closed in " +
                            (System.currentTimeMillis() - cleanTime) + " ms");
                }
            } catch (InterruptedException e) {
                break;
            } catch (Throwable e) {
                log.error("Uncaught exception in CleanupThread", e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy