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 */ 017package org.apache.activemq.broker.region.group; 018 019import java.util.HashMap; 020import java.util.Iterator; 021import java.util.Map; 022import java.util.concurrent.ConcurrentHashMap; 023 024import org.apache.activemq.broker.region.Destination; 025import org.apache.activemq.command.ConsumerId; 026 027/** 028 * A simple implementation which tracks every individual GroupID value but 029 * which can become a memory leak if clients die before they complete a message 030 * group. 031 * 032 * 033 */ 034public class SimpleMessageGroupMap implements MessageGroupMap { 035 private Map<String, ConsumerId> map = new ConcurrentHashMap<String, ConsumerId>(); 036 037 public void put(String groupId, ConsumerId consumerId) { 038 map.put(groupId, consumerId); 039 } 040 041 public ConsumerId get(String groupId) { 042 return map.get(groupId); 043 } 044 045 public ConsumerId removeGroup(String groupId) { 046 return map.remove(groupId); 047 } 048 049 public MessageGroupSet removeConsumer(ConsumerId consumerId) { 050 SimpleMessageGroupSet ownedGroups = new SimpleMessageGroupSet(); 051 for (Iterator<String> iter = map.keySet().iterator(); iter.hasNext();) { 052 String group = iter.next(); 053 ConsumerId owner = map.get(group); 054 if (owner.equals(consumerId)) { 055 ownedGroups.add(group); 056 iter.remove(); 057 } 058 } 059 return ownedGroups; 060 } 061 062 063 @Override 064 public void removeAll(){ 065 map.clear(); 066 } 067 068 @Override 069 public Map<String, String> getGroups() { 070 Map<String,String> result = new HashMap<String,String>(); 071 for (Map.Entry<String,ConsumerId>entry:map.entrySet()){ 072 result.put(entry.getKey(),entry.getValue().toString()); 073 } 074 return result; 075 } 076 077 @Override 078 public String getType() { 079 return "simple"; 080 } 081 082 public void setDestination(Destination destination) {} 083 084 public String toString() { 085 return "message groups: " + map.size(); 086 } 087 088}