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.policy; 018 019import org.apache.activemq.broker.region.MessageReference; 020 021import java.io.IOException; 022import java.util.HashMap; 023import java.util.Iterator; 024import java.util.LinkedList; 025 026 027/** 028 * An eviction strategy which evicts the oldest message within messages with the same property value 029 * 030 * 031 * @org.apache.xbean.XBean 032 * 033 */ 034public class UniquePropertyMessageEvictionStrategy extends MessageEvictionStrategySupport { 035 036 protected String propertyName; 037 038 public String getPropertyName() { 039 return propertyName; 040 } 041 042 public void setPropertyName(String propertyName) { 043 this.propertyName = propertyName; 044 } 045 046 @Override 047 public MessageReference[] evictMessages(LinkedList messages) throws IOException { 048 MessageReference oldest = (MessageReference)messages.getFirst(); 049 HashMap<Object, MessageReference> pivots = new HashMap<Object, MessageReference>(); 050 Iterator iter = messages.iterator(); 051 052 for (int i = 0; iter.hasNext(); i++) { 053 MessageReference reference = (MessageReference) iter.next(); 054 if (propertyName != null && reference.getMessage().getProperty(propertyName) != null) { 055 Object key = reference.getMessage().getProperty(propertyName); 056 if (pivots.containsKey(key)) { 057 MessageReference pivot = pivots.get(key); 058 if (reference.getMessage().getTimestamp() > pivot.getMessage().getTimestamp()) { 059 pivots.put(key, reference); 060 } 061 } else { 062 pivots.put(key, reference); 063 } 064 } 065 } 066 067 if (!pivots.isEmpty()) { 068 for (MessageReference ref : pivots.values()) { 069 messages.remove(ref); 070 } 071 072 if (messages.size() != 0) { 073 return (MessageReference[])messages.toArray(new MessageReference[messages.size()]); 074 } 075 } 076 return new MessageReference[] {oldest}; 077 078 } 079}