Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » region » policy » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.activemq.broker.region.policy;
   18   
   19   import java.util.ArrayList;
   20   import java.util.List;
   21   
   22   import org.apache.activemq.broker.ConnectionContext;
   23   import org.apache.activemq.broker.region.MessageReference;
   24   import org.apache.activemq.broker.region.SubscriptionRecovery;
   25   import org.apache.activemq.broker.region.Topic;
   26   import org.apache.activemq.command.ActiveMQDestination;
   27   import org.apache.activemq.command.Message;
   28   import org.apache.activemq.filter.DestinationFilter;
   29   
   30   /**
   31    * This implementation of {@link SubscriptionRecoveryPolicy} will keep a fixed
   32    * count of last messages.
   33    * 
   34    * @org.apache.xbean.XBean
   35    * @version $Revision$
   36    */
   37   public class FixedCountSubscriptionRecoveryPolicy implements SubscriptionRecoveryPolicy {
   38       private volatile MessageReference messages[];
   39       private int maximumSize = 100;
   40       private int tail;
   41   
   42       public SubscriptionRecoveryPolicy copy() {
   43           FixedCountSubscriptionRecoveryPolicy rc = new FixedCountSubscriptionRecoveryPolicy();
   44           rc.setMaximumSize(maximumSize);
   45           return rc;
   46       }
   47   
   48       public synchronized boolean add(ConnectionContext context, MessageReference node) throws Exception {
   49           messages[tail++] = node;
   50           if (tail >= messages.length) {
   51               tail = 0;
   52           }
   53           return true;
   54       }
   55   
   56       public synchronized void recover(ConnectionContext context, Topic topic, SubscriptionRecovery sub) throws Exception {
   57           // Re-dispatch the last message seen.
   58           int t = tail;
   59           // The buffer may not have rolled over yet..., start from the front
   60           if (messages[t] == null) {
   61               t = 0;
   62           }
   63           // Well the buffer is really empty then.
   64           if (messages[t] == null) {
   65               return;
   66           }
   67           // Keep dispatching until t hit's tail again.
   68           do {
   69               MessageReference node = messages[t];
   70               sub.addRecoveredMessage(context, node);
   71               t++;
   72               if (t >= messages.length) {
   73                   t = 0;
   74               }
   75           } while (t != tail);
   76       }
   77   
   78       public void start() throws Exception {
   79           messages = new MessageReference[maximumSize];
   80       }
   81   
   82       public void stop() throws Exception {
   83           messages = null;
   84       }
   85   
   86       public int getMaximumSize() {
   87           return maximumSize;
   88       }
   89   
   90       /**
   91        * Sets the maximum number of messages that this destination will hold
   92        * around in RAM
   93        */
   94       public void setMaximumSize(int maximumSize) {
   95           this.maximumSize = maximumSize;
   96       }
   97   
   98       public synchronized Message[] browse(ActiveMQDestination destination) throws Exception {
   99           List<Message> result = new ArrayList<Message>();
  100           DestinationFilter filter = DestinationFilter.parseFilter(destination);
  101           int t = tail;
  102           if (messages[t] == null) {
  103               t = 0;
  104           }
  105           if (messages[t] != null) {
  106               do {
  107                   MessageReference ref = messages[t];
  108                   Message message = ref.getMessage();
  109                   if (filter.matches(message.getDestination())) {
  110                       result.add(message);
  111                   }
  112                   t++;
  113                   if (t >= messages.length) {
  114                       t = 0;
  115                   }
  116               } while (t != tail);
  117           }
  118           return result.toArray(new Message[result.size()]);
  119       }
  120   
  121   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » region » policy » [javadoc | source]