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   import org.apache.activemq.broker.ConnectionContext;
   22   import org.apache.activemq.broker.region.MessageReference;
   23   import org.apache.activemq.broker.region.SubscriptionRecovery;
   24   import org.apache.activemq.broker.region.Topic;
   25   import org.apache.activemq.command.ActiveMQDestination;
   26   import org.apache.activemq.command.Message;
   27   import org.apache.activemq.filter.DestinationFilter;
   28   
   29   /**
   30    * This implementation of {@link SubscriptionRecoveryPolicy} will only keep the
   31    * last message.
   32    * 
   33    * @org.apache.xbean.XBean
   34    * @version $Revision$
   35    */
   36   public class LastImageSubscriptionRecoveryPolicy implements SubscriptionRecoveryPolicy {
   37   
   38       private volatile MessageReference lastImage;
   39   
   40       public boolean add(ConnectionContext context, MessageReference node) throws Exception {
   41           lastImage = node;
   42           return true;
   43       }
   44   
   45       public void recover(ConnectionContext context, Topic topic, SubscriptionRecovery sub) throws Exception {
   46           // Re-dispatch the last message seen.
   47           MessageReference node = lastImage;
   48           if (node != null) {
   49               sub.addRecoveredMessage(context, node);
   50           }
   51       }
   52   
   53       public void start() throws Exception {
   54       }
   55   
   56       public void stop() throws Exception {
   57       }
   58   
   59       public Message[] browse(ActiveMQDestination destination) throws Exception {
   60           List<Message> result = new ArrayList<Message>();
   61           DestinationFilter filter = DestinationFilter.parseFilter(destination);
   62           if (filter.matches(lastImage.getMessage().getDestination())) {
   63               result.add(lastImage.getMessage());
   64           }
   65           return result.toArray(new Message[result.size()]);
   66       }
   67   
   68       public SubscriptionRecoveryPolicy copy() {
   69           return new LastImageSubscriptionRecoveryPolicy();
   70       }
   71   
   72   }

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