View Javadoc

    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.codehaus.activemq;
    20
    21  import org.codehaus.activemq.message.Packet;
    22  import org.codehaus.activemq.util.BitArrayBin;
    23  import org.codehaus.activemq.util.IdGenerator;
    24  import org.codehaus.activemq.util.LRUCache;
    25
    26  import javax.jms.Message;
    27  import java.util.LinkedHashMap;
    28
    29  /***
    30   * Provides basic audit functions for Messages
    31   *
    32   * @version $Revision: 1.4 $
    33   */
    34  public class ActiveMQMessageAudit {
    35      private static final int DEFAULT_WINDOW_SIZE = 1024;
    36      private static final int MAXIMUM_PRODUCER_COUNT = 128;
    37      private int windowSize;
    38      private LinkedHashMap map;
    39
    40      /***
    41       * Default Constructor windowSize = 1024, maximumNumberOfProducersToTrack = 128
    42       */
    43      public ActiveMQMessageAudit() {
    44          this(DEFAULT_WINDOW_SIZE, MAXIMUM_PRODUCER_COUNT);
    45      }
    46
    47      /***
    48       * Construct a MessageAudit
    49       *
    50       * @param windowSize range of ids to track
    51       * @param maximumNumberOfProducersToTrack
    52       *                   number of producers expected in the system
    53       */
    54      public ActiveMQMessageAudit(int windowSize, final int maximumNumberOfProducersToTrack) {
    55          this.windowSize = windowSize;
    56          map = new LRUCache(maximumNumberOfProducersToTrack);
    57      }
    58
    59      /***
    60       * Checks if this message has beeb seen before
    61       *
    62       * @param message
    63       * @return true if the message is a duplicate
    64       */
    65      public boolean isDuplicate(Message message) {
    66          if (message instanceof Packet) {
    67              return isDuplicate((Packet) message);
    68          }
    69          return false;
    70      }
    71
    72      /***
    73       * checks whether this messageId has been seen before
    74       *
    75       * @param packet
    76       * @return true if the message is a duplicate
    77       */
    78      public boolean isDuplicate(Packet packet) {
    79          return isDuplicate(packet.getId());
    80      }
    81
    82      /***
    83       * checks whether this messageId has been seen before and adds this messageId to the list
    84       *
    85       * @param id
    86       * @return true if the message is a duplicate
    87       */
    88      public boolean isDuplicate(String id) {
    89          boolean answer = false;
    90          String seed = IdGenerator.getSeedFromId(id);
    91          if (seed != null) {
    92              BitArrayBin bab = (BitArrayBin) map.get(seed);
    93              if (bab == null) {
    94                  bab = new BitArrayBin(windowSize);
    95                  map.put(seed, bab);
    96              }
    97              long index = IdGenerator.getCountFromId(id);
    98              if (index >= 0) {
    99                  answer = bab.setBit(index, true);
    100             }
    101         }
    102         return answer;
    103     }
    104 }