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