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.concurrent.atomic.AtomicLong;
   20   
   21   import javax.jms.JMSException;
   22   import javax.jms.Message;
   23   import javax.jms.MessageListener;
   24   
   25   import org.apache.activemq.ActiveMQMessageTransformation;
   26   import org.apache.activemq.broker.ConnectionContext;
   27   import org.apache.activemq.broker.region.Destination;
   28   import org.apache.activemq.broker.region.MessageReference;
   29   import org.apache.activemq.broker.region.SubscriptionRecovery;
   30   import org.apache.activemq.broker.region.Topic;
   31   import org.apache.activemq.command.ActiveMQDestination;
   32   import org.apache.activemq.command.ActiveMQMessage;
   33   import org.apache.activemq.command.ConnectionId;
   34   import org.apache.activemq.command.MessageId;
   35   import org.apache.activemq.command.ProducerId;
   36   import org.apache.activemq.command.SessionId;
   37   import org.apache.activemq.util.IdGenerator;
   38   import org.apache.commons.logging.Log;
   39   import org.apache.commons.logging.LogFactory;
   40   
   41   /**
   42    * This implementation of {@link SubscriptionRecoveryPolicy} will perform a user
   43    * specific query mechanism to load any messages they may have missed.
   44    * 
   45    * @org.apache.xbean.XBean
   46    * @version $Revision: 564271 $
   47    */
   48   public class QueryBasedSubscriptionRecoveryPolicy implements SubscriptionRecoveryPolicy {
   49   
   50       private static final Log LOG = LogFactory.getLog(QueryBasedSubscriptionRecoveryPolicy.class);
   51   
   52       private MessageQuery query;
   53       private AtomicLong messageSequence = new AtomicLong(0);
   54       private IdGenerator idGenerator = new IdGenerator();
   55       private ProducerId producerId = createProducerId();
   56   
   57       public SubscriptionRecoveryPolicy copy() {
   58           QueryBasedSubscriptionRecoveryPolicy rc = new QueryBasedSubscriptionRecoveryPolicy();
   59           rc.setQuery(query);
   60           return rc;
   61       }
   62   
   63       public boolean add(ConnectionContext context, MessageReference message) throws Exception {
   64           return query.validateUpdate(message.getMessage());
   65       }
   66   
   67       public void recover(final ConnectionContext context, final Topic topic, final SubscriptionRecovery sub) throws Exception {
   68           if (query != null) {
   69               ActiveMQDestination destination = sub.getActiveMQDestination();
   70               query.execute(destination, new MessageListener() {
   71   
   72                   public void onMessage(Message message) {
   73                       dispatchInitialMessage(message, topic, context, sub);
   74                   }
   75               });
   76           }
   77       }
   78   
   79       public void start() throws Exception {
   80           if (query == null) {
   81               throw new IllegalArgumentException("No query property configured");
   82           }
   83       }
   84   
   85       public void stop() throws Exception {
   86       }
   87   
   88       public MessageQuery getQuery() {
   89           return query;
   90       }
   91   
   92       /**
   93        * Sets the query strategy to load initial messages
   94        */
   95       public void setQuery(MessageQuery query) {
   96           this.query = query;
   97       }
   98   
   99       public org.apache.activemq.command.Message[] browse(ActiveMQDestination dest) throws Exception {
  100           return new org.apache.activemq.command.Message[0];
  101       }
  102   
  103       protected void dispatchInitialMessage(Message message, Destination regionDestination, ConnectionContext context, SubscriptionRecovery sub) {
  104           try {
  105               ActiveMQMessage activeMessage = ActiveMQMessageTransformation.transformMessage(message, null);
  106               ActiveMQDestination destination = activeMessage.getDestination();
  107               if (destination == null) {
  108                   destination = sub.getActiveMQDestination();
  109                   activeMessage.setDestination(destination);
  110               }
  111               activeMessage.setRegionDestination(regionDestination);
  112               configure(activeMessage);
  113               sub.addRecoveredMessage(context, activeMessage);
  114           } catch (Throwable e) {
  115               LOG.warn("Failed to dispatch initial message: " + message + " into subscription. Reason: " + e, e);
  116           }
  117       }
  118   
  119       protected void configure(ActiveMQMessage msg) throws JMSException {
  120           long sequenceNumber = messageSequence.incrementAndGet();
  121           msg.setMessageId(new MessageId(producerId, sequenceNumber));
  122           msg.onSend();
  123           msg.setProducerId(producerId);
  124       }
  125   
  126       protected ProducerId createProducerId() {
  127           String id = idGenerator.generateId();
  128           ConnectionId connectionId = new ConnectionId(id);
  129           SessionId sessionId = new SessionId(connectionId, 1);
  130           return new ProducerId(sessionId, 1);
  131       }
  132   }

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