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.memory.list; 018 019import java.util.ArrayList; 020import java.util.Iterator; 021import java.util.LinkedList; 022import java.util.List; 023import org.apache.activemq.broker.region.MessageReference; 024import org.apache.activemq.command.ActiveMQDestination; 025import org.apache.activemq.command.Message; 026import org.apache.activemq.filter.DestinationFilter; 027 028/** 029 * A simple fixed size {@link MessageList} where there is a single, fixed size 030 * list that all messages are added to for simplicity. Though this will lead to 031 * possibly slow recovery times as many more messages than is necessary will 032 * have to be iterated through for each subscription. 033 * 034 * 035 */ 036public class SimpleMessageList implements MessageList { 037 private final LinkedList<MessageReference> list = new LinkedList<MessageReference>(); 038 private int maximumSize = 100 * 64 * 1024; 039 private int size; 040 private final Object lock = new Object(); 041 042 public SimpleMessageList() { 043 } 044 045 public SimpleMessageList(int maximumSize) { 046 this.maximumSize = maximumSize; 047 } 048 049 public void add(MessageReference node) { 050 int delta = node.getMessageHardRef().getSize(); 051 synchronized (lock) { 052 list.add(node); 053 size += delta; 054 while (size > maximumSize) { 055 MessageReference evicted = list.removeFirst(); 056 size -= evicted.getMessageHardRef().getSize(); 057 } 058 } 059 } 060 061 public List<MessageReference> getMessages(ActiveMQDestination destination) { 062 return getList(); 063 } 064 065 public Message[] browse(ActiveMQDestination destination) { 066 List<Message> result = new ArrayList<Message>(); 067 DestinationFilter filter = DestinationFilter.parseFilter(destination); 068 synchronized (lock) { 069 for (Iterator<MessageReference> i = list.iterator(); i.hasNext();) { 070 MessageReference ref = i.next(); 071 Message msg; 072 msg = ref.getMessage(); 073 if (filter.matches(msg.getDestination())) { 074 result.add(msg); 075 } 076 077 } 078 } 079 return result.toArray(new Message[result.size()]); 080 } 081 082 /** 083 * Returns a copy of the list 084 */ 085 public List<MessageReference> getList() { 086 synchronized (lock) { 087 return new ArrayList<MessageReference>(list); 088 } 089 } 090 091 public int getSize() { 092 synchronized (lock) { 093 return size; 094 } 095 } 096 097 public void clear() { 098 synchronized (lock) { 099 list.clear(); 100 size = 0; 101 } 102 } 103 104}