1 18 package org.apache.activemq.memory.list; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import org.apache.activemq.broker.region.MessageReference; 26 import org.apache.activemq.command.ActiveMQDestination; 27 import org.apache.activemq.command.Message; 28 import org.apache.activemq.filter.DestinationFilter; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 40 public class SimpleMessageList implements MessageList { 41 static final private Log log=LogFactory.getLog(SimpleMessageList.class); 42 private LinkedList list = new LinkedList (); 43 private int maximumSize = 100 * 64 * 1024; 44 private int size; 45 private Object lock = new Object (); 46 47 public SimpleMessageList() { 48 } 49 50 public SimpleMessageList(int maximumSize) { 51 this.maximumSize = maximumSize; 52 } 53 54 public void add(MessageReference node) { 55 int delta = node.getMessageHardRef().getSize(); 56 synchronized (lock) { 57 list.add(node); 58 size += delta; 59 while (size > maximumSize) { 60 MessageReference evicted = (MessageReference) list.removeFirst(); 61 size -= evicted.getMessageHardRef().getSize(); 62 } 63 } 64 } 65 66 public List getMessages(ActiveMQDestination destination) { 67 return getList(); 68 } 69 70 public Message[] browse(ActiveMQDestination destination) { 71 List result = new ArrayList (); 72 DestinationFilter filter=DestinationFilter.parseFilter(destination); 73 synchronized(lock){ 74 for (Iterator i = list.iterator(); i.hasNext();){ 75 MessageReference ref = (MessageReference)i.next(); 76 Message msg; 77 try{ 78 msg=ref.getMessage(); 79 if (filter.matches(msg.getDestination())){ 80 result.add(msg); 81 } 82 }catch(IOException e){ 83 log.error("Failed to get Message from MessageReference: " + ref,e); 84 } 85 86 } 87 } 88 return (Message[])result.toArray(new Message[result.size()]); 89 } 90 91 94 public List getList() { 95 synchronized (lock) { 96 return new ArrayList (list); 97 } 98 } 99 100 public int getSize() { 101 synchronized (lock) { 102 return size; 103 } 104 } 105 106 public void clear() { 107 synchronized (lock) { 108 list.clear(); 109 size = 0; 110 } 111 } 112 113 } 114 | Popular Tags |