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

net.ymate.platform.serv.impl.DefaultSessionIdleChecker Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
/*
 * Copyright 2007-2018 the original author or authors.
 *
 * 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 net.ymate.platform.serv.impl;

import net.ymate.platform.serv.ISessionIdleChecker;
import net.ymate.platform.serv.ISessionManager;
import net.ymate.platform.serv.ISessionWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Iterator;
import java.util.Map;

/**
 * @author 刘镇 ([email protected]) on 2018/11/22 2:27 AM
 * @version 1.0
 */
public class DefaultSessionIdleChecker implements ISessionIdleChecker {

    private static final Log _LOG = LogFactory.getLog(DefaultSessionIdleChecker.class);

    private ISessionManager __sessionManager;

    private boolean __inited;

    @Override
    public void init(ISessionManager sessionManager) {
        __sessionManager = sessionManager;
        __inited = true;
    }

    @Override
    public ISessionManager getSessionManager() {
        return __sessionManager;
    }

    @Override
    public boolean isInited() {
        return __inited;
    }

    @Override
    public void processIdleSession(Map sessions, long idleTimeInMillis) {
        Iterator> _iterator = sessions.entrySet().iterator();
        while (_iterator.hasNext()) {
            Map.Entry _entry = _iterator.next();
            if (System.currentTimeMillis() - _entry.getValue().getLastTouchTime() > idleTimeInMillis) {
                _iterator.remove();
                //
                getSessionManager().closeSessionWrapper(_entry.getValue());
                //
                if (_LOG.isDebugEnabled()) {
                    _LOG.debug(_entry.getValue() + " - Session idle removed. Session count: " + this.getSessionManager().sessionCount());
                }
                this.getSessionManager().sessionListener().onSessionIdleRemoved(_entry.getValue());
            }
        }
    }

    @Override
    public void destroy() throws Exception {
        if (__inited) {
            __inited = false;
            __sessionManager = null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy