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

com.gateway.invoke.TopicManager Maven / Gradle / Ivy

package com.gateway.invoke;

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

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

import com.gateway.utils.TopicUtils;
import com.google.common.base.Strings;

/**
 * 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 (!Strings.isNullOrEmpty(sessionid)) {
			CopyOnWriteArraySet cas = topicHm.get(sessionid);
			if (cas == null) {
				cas = new CopyOnWriteArraySet();
				topicHm.put(sessionid, cas);
			}
			for (String stopic : cas) {
				if(TopicUtils.matchTopic(stopic, topic)) {
					logger.warn("topic:{} match topic:{},so drop,sessionid:{} from sessions",topic,stopic,sessionid);
					flag = true;
				}
			}
			if (!flag) {
				logger.warn("add topic:{},sessionid:{} from sessions",topic,sessionid);
				cas.add(topic);
				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);
			logger.warn("remove topic:{},sessionid:{} from sessions",topic,sessionid);
		}

		return flag;
	}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy