Code example for ConcurrentHashMap

Methods: ConcurrentHashMap.putIfAbsent, get

0
 
    public void subscribe(final String topic, final String group, final int maxSize,
            final MessageListener messageListener, final ConsumerMessageFilter consumerMessageFilter)
                    throws MetaClientException { 
        final ConcurrentHashMap<String, SubscriberInfo> topicSubsriberRegistry = this.getTopicSubscriberRegistry(group);
        SubscriberInfo info = topicSubsriberRegistry.get(topic);
        if (info == null) {
            info = new SubscriberInfo(messageListener, consumerMessageFilter, maxSize);
            final SubscriberInfo oldInfo = topicSubsriberRegistry.putIfAbsent(topic, info);
            if (oldInfo != null) {
                throw new MetaClientException("Topic=" + topic + " has been subscribered by group " + group);
            } 
        } 
        else { 
            throw new MetaClientException("Topic=" + topic + " has been subscribered by group " + group);
        } 
    } 
 
 
    private ConcurrentHashMap<String, SubscriberInfo> getTopicSubscriberRegistry(final String group)
            throws MetaClientException { 
        ConcurrentHashMap<String/* topic */, SubscriberInfo> topicSubsriberRegistry =
                this.groupTopicSubcriberRegistry.get(group);
        if (topicSubsriberRegistry == null) {
            topicSubsriberRegistry = new ConcurrentHashMap<String, SubscriberInfo>();
            final ConcurrentHashMap<String/* topic */, SubscriberInfo> old =
                    this.groupTopicSubcriberRegistry.putIfAbsent(group, topicSubsriberRegistry);
            if (old != null) {
                topicSubsriberRegistry = old;
            } 
        }