Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/ActiveMQMessageAudit.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  
19  package org.activemq;
20  
21  import java.util.LinkedHashMap;
22  
23  import javax.jms.JMSException;
24  import javax.jms.Message;
25  
26  import org.activemq.util.BitArrayBin;
27  import org.activemq.util.IdGenerator;
28  import org.activemq.util.LRUCache;
29  
30  /**
31   * Provides basic audit functions for Messages
32   *
33   * @version $Revision: 1.1.1.1 $
34   */
35  public class ActiveMQMessageAudit {
36      private static final int DEFAULT_WINDOW_SIZE = 1024;
37      private static final int MAXIMUM_PRODUCER_COUNT = 128;
38      private int windowSize;
39      private LinkedHashMap map;
40  
41      /**
42       * Default Constructor windowSize = 1024, maximumNumberOfProducersToTrack = 128
43       */
44      public ActiveMQMessageAudit() {
45          this(DEFAULT_WINDOW_SIZE, MAXIMUM_PRODUCER_COUNT);
46      }
47  
48      /**
49       * Construct a MessageAudit
50       *
51       * @param windowSize range of ids to track
52       * @param maximumNumberOfProducersToTrack
53       *                   number of producers expected in the system
54       */
55      public ActiveMQMessageAudit(int windowSize, final int maximumNumberOfProducersToTrack) {
56          this.windowSize = windowSize;
57          map = new LRUCache(maximumNumberOfProducersToTrack);
58      }
59  
60      /**
61       * Checks if this message has beeb seen before
62       *
63       * @param message
64       * @return true if the message is a duplicate
65       * @throws JMSException
66       */
67      public boolean isDuplicate(Message message) throws JMSException {
68         return isDuplicate(message.getJMSMessageID());
69      }
70  
71  
72      /**
73       * checks whether this messageId has been seen before and adds this messageId to the list
74       *
75       * @param id
76       * @return true if the message is a duplicate
77       */
78      public boolean isDuplicate(String id) {
79          boolean answer = false;
80          String seed = IdGenerator.getSeedFromId(id);
81          if (seed != null) {
82              BitArrayBin bab = (BitArrayBin) map.get(seed);
83              if (bab == null) {
84                  bab = new BitArrayBin(windowSize);
85                  map.put(seed, bab);
86              }
87              long index = IdGenerator.getCountFromId(id);
88              if (index >= 0) {
89                  answer = bab.setBit(index, true);
90              }
91          }
92          return answer;
93      }
94  }