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

com.gateway.connector.tcp.server.TopicManager Maven / Gradle / Ivy

package com.gateway.connector.tcp.server;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * topic<->session管理
 * 
 * @author deshuai.kong
 *
 */
public class TopicManager {

	private final static Logger logger = LoggerFactory.getLogger(TopicManager.class);
	private ConcurrentHashMap> topicHm = new ConcurrentHashMap>();

	public ConcurrentHashMap> getTopics() {
		return topicHm;
	}

	public synchronized boolean add(String sessionid, String topic) {// 添加
		boolean flag = false;
		if (StringUtils.isNoneEmpty(sessionid)) {
			CopyOnWriteArraySet cas = topicHm.get(sessionid);
			if (cas == null) {
				cas = new CopyOnWriteArraySet();
				topicHm.put(sessionid, cas);
			}
			if (!cas.contains(topic)) {
				cas.add(topic);
				logger.debug("add topic " + sessionid+":"+topic + " from sessions!");
			}
			
			flag = true;
		}
		return flag;
	}

	public synchronized boolean remove(String sessionid, String topic) {// 删除
		boolean flag = true;
		CopyOnWriteArraySet cas = topicHm.get(sessionid);
		if (cas != null) {
			cas.remove(topic);
		}

		return flag;
	}

	public synchronized boolean clear(String sessionid) {// 清除topic
		boolean flag = false;
		topicHm.remove(sessionid);
		logger.debug("clear topic " + sessionid + " from sessions!");
		flag = true;
		return flag;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy