001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.broker.region.group;
018    
019    import java.util.Iterator;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import org.apache.activemq.command.ConsumerId;
024    
025    /**
026     * A simple implementation which tracks every individual GroupID value but
027     * which can become a memory leak if clients die before they complete a message
028     * group.
029     * 
030     * 
031     */
032    public class SimpleMessageGroupMap implements MessageGroupMap {
033        private Map<String, ConsumerId> map = new ConcurrentHashMap<String, ConsumerId>();
034        
035        public void put(String groupId, ConsumerId consumerId) {
036            map.put(groupId, consumerId);
037        }
038    
039        public ConsumerId get(String groupId) {
040            return map.get(groupId);
041        }
042    
043        public ConsumerId removeGroup(String groupId) {
044            return map.remove(groupId);
045        }
046    
047        public MessageGroupSet removeConsumer(ConsumerId consumerId) {
048            SimpleMessageGroupSet ownedGroups = new SimpleMessageGroupSet();
049            for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) {
050                String group = iter.next();
051                ConsumerId owner = map.get(group);
052                if (owner.equals(consumerId)) {
053                    ownedGroups.add(group);
054                    iter.remove();
055                }
056            }
057            return ownedGroups;
058        }
059    
060        public String toString() {
061            return "message groups: " + map.size();
062        }
063    
064    }