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

com.gateway.connector.api.ExchangeSessionManager Maven / Gradle / Ivy

 
package com.gateway.connector.api;

import com.gateway.connector.Session;
import com.gateway.connector.SessionManager;
import com.gateway.connector.api.listener.SessionListener;
import com.gateway.invoke.TopicManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

 
public abstract class ExchangeSessionManager implements SessionManager {

    private final static Logger logger = LoggerFactory.getLogger(ExchangeSessionManager.class);

    protected List sessionListeners = null;
    private TopicManager topicManager;

	public TopicManager getTopicManager() {
		return topicManager;
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}
    public void setSessionListeners(List sessionListeners) {
        this.sessionListeners = sessionListeners;
    }

    /**
     * The set of currently active Sessions for this Manager, keyed by session
     * identifier.
     */
    protected Map sessions = new ConcurrentHashMap();

    public synchronized void addSession(Session session) {
        if (null == session) {
            return;
        }
        sessions.put(session.getSessionId(), session);
        logger.info("put a session " + session.getSessionId() + " to sessions!");
    }

    public synchronized void updateSession(String sessionId) {
        Session session = sessions.get(sessionId);
        session.setLastAccessedTime(System.currentTimeMillis());

        sessions.put(sessionId, session);
    }

    /**
     * Remove this Session from the active Sessions for this Manager.
     */
    public synchronized void removeSession(Session session) {
        if (session == null) {
            throw new IllegalArgumentException("session is null!");
        }
        removeSession(session.getSessionId());
    }

    public synchronized void removeSession(String sessionId) {
        sessions.remove(sessionId);
        topicManager.clear(sessionId);
        logger.info("remove the session " + sessionId + " from sessions!");
    }

    public Session getSession(String sessionId) {
        return sessions.get(sessionId+"");
    }

    public Session[] getSessions() {
        return sessions.values().toArray(new Session[0]);
    }

    public Set getSessionKeys() {
        return sessions.keySet();
    }

    public int getSessionCount() {
        return sessions.size();
    }

    /**
     * define timeout 5min
     */
    private int maxInactiveInterval = 5 * 60;

    public int getMaxInactiveInterval() {
        return maxInactiveInterval;
    }

    public void setMaxInactiveInterval(int maxInactiveInterval) {
        this.maxInactiveInterval = maxInactiveInterval;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy