001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.broker.region.virtual; 018 019import java.util.Set; 020 021import org.apache.activemq.broker.ConnectionContext; 022import org.apache.activemq.broker.region.BaseDestination; 023import org.apache.activemq.broker.region.Destination; 024import org.apache.activemq.broker.region.DestinationFilter; 025import org.apache.activemq.broker.region.IndirectMessageReference; 026import org.apache.activemq.broker.region.RegionBroker; 027import org.apache.activemq.broker.region.Subscription; 028import org.apache.activemq.broker.region.Topic; 029import org.apache.activemq.command.ActiveMQDestination; 030import org.apache.activemq.command.Message; 031import org.apache.activemq.util.SubscriptionKey; 032 033/** 034 * Creates a mapped Queue that can recover messages from subscription recovery 035 * policy of its Virtual Topic. 036 */ 037public class MappedQueueFilter extends DestinationFilter { 038 039 private final ActiveMQDestination virtualDestination; 040 041 public MappedQueueFilter(ActiveMQDestination virtualDestination, Destination destination) { 042 super(destination); 043 this.virtualDestination = virtualDestination; 044 } 045 046 @Override 047 public synchronized void addSubscription(ConnectionContext context, Subscription sub) throws Exception { 048 // recover messages for first consumer only 049 boolean noSubs = getConsumers().isEmpty(); 050 051 // for virtual consumer wildcard dests, only subscribe to exact match to ensure no duplicates 052 if (sub.getActiveMQDestination().compareTo(next.getActiveMQDestination()) == 0) { 053 super.addSubscription(context, sub); 054 } 055 if (noSubs && !getConsumers().isEmpty()) { 056 // new subscription added, recover retroactive messages 057 final RegionBroker regionBroker = (RegionBroker) context.getBroker().getAdaptor(RegionBroker.class); 058 final Set<Destination> virtualDests = regionBroker.getDestinations(virtualDestination); 059 060 final ActiveMQDestination newDestination = sub.getActiveMQDestination(); 061 final BaseDestination regionDest = getBaseDestination((Destination) regionBroker.getDestinations(newDestination).toArray()[0]); 062 063 for (Destination virtualDest : virtualDests) { 064 if (virtualDest.getActiveMQDestination().isTopic() && 065 (virtualDest.isAlwaysRetroactive() || sub.getConsumerInfo().isRetroactive())) { 066 067 Topic topic = (Topic) getBaseDestination(virtualDest); 068 if (topic != null) { 069 // re-use browse() to get recovered messages 070 final Message[] messages = topic.getSubscriptionRecoveryPolicy().browse(topic.getActiveMQDestination()); 071 072 // add recovered messages to subscription 073 for (Message message : messages) { 074 final Message copy = message.copy(); 075 copy.setOriginalDestination(message.getDestination()); 076 copy.setDestination(newDestination); 077 copy.setRegionDestination(regionDest); 078 sub.addRecoveredMessage(context, newDestination.isQueue() ? new IndirectMessageReference(copy) : copy); 079 } 080 } 081 } 082 } 083 } 084 } 085 086 private BaseDestination getBaseDestination(Destination virtualDest) { 087 if (virtualDest instanceof BaseDestination) { 088 return (BaseDestination) virtualDest; 089 } else if (virtualDest instanceof DestinationFilter) { 090 return ((DestinationFilter) virtualDest).getAdaptor(BaseDestination.class); 091 } 092 return null; 093 } 094 095 @Override 096 public synchronized void removeSubscription(ConnectionContext context, Subscription sub, long lastDeliveredSequenceId) throws Exception { 097 super.removeSubscription(context, sub, lastDeliveredSequenceId); 098 } 099 100 @Override 101 public synchronized void deleteSubscription(ConnectionContext context, SubscriptionKey key) throws Exception { 102 super.deleteSubscription(context, key); 103 } 104 105 @Override 106 public String toString() { 107 return "MappedQueueFilter[" + virtualDestination + ", " + next + "]"; 108 } 109}