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.network; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.apache.activemq.command.ConsumerId; 024import org.apache.activemq.command.ConsumerInfo; 025import org.apache.activemq.command.SubscriptionInfo; 026import org.apache.activemq.filter.DestinationFilter; 027import org.apache.activemq.transport.Transport; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * Consolidates subscriptions 033 */ 034public class ConduitBridge extends DemandForwardingBridge { 035 private static final Logger LOG = LoggerFactory.getLogger(ConduitBridge.class); 036 037 /** 038 * Constructor 039 * 040 * @param localBroker 041 * @param remoteBroker 042 */ 043 public ConduitBridge(NetworkBridgeConfiguration configuration, Transport localBroker, Transport remoteBroker) { 044 super(configuration, localBroker, remoteBroker); 045 } 046 047 @Override 048 protected DemandSubscription createDemandSubscription(ConsumerInfo info) throws IOException { 049 if (addToAlreadyInterestedConsumers(info)) { 050 return null; // don't want this subscription added 051 } 052 //add our original id to ourselves 053 info.addNetworkConsumerId(info.getConsumerId()); 054 info.setSelector(null); 055 return doCreateDemandSubscription(info); 056 } 057 058 protected boolean addToAlreadyInterestedConsumers(ConsumerInfo info) { 059 // search through existing subscriptions and see if we have a match 060 if (info.isNetworkSubscription()) { 061 return false; 062 } 063 boolean matched = false; 064 065 for (DemandSubscription ds : subscriptionMapByLocalId.values()) { 066 DestinationFilter filter = DestinationFilter.parseFilter(ds.getLocalInfo().getDestination()); 067 if (canConduit(ds) && filter.matches(info.getDestination())) { 068 LOG.debug("{} {} with ids {} matched (add interest) {}", new Object[]{ 069 configuration.getBrokerName(), info, info.getNetworkConsumerIds(), ds 070 }); 071 // add the interest in the subscription 072 if (!info.isDurable()) { 073 ds.add(info.getConsumerId()); 074 } else { 075 ds.getDurableRemoteSubs().add(new SubscriptionInfo(info.getClientId(), info.getSubscriptionName())); 076 } 077 matched = true; 078 // continue - we want interest to any existing DemandSubscriptions 079 } 080 } 081 return matched; 082 } 083 084 // we want to conduit statically included consumers which are local networkSubs 085 // but we don't want to conduit remote network subs i.e. (proxy proxy) consumers 086 private boolean canConduit(DemandSubscription ds) { 087 return ds.isStaticallyIncluded() || !ds.getRemoteInfo().isNetworkSubscription(); 088 } 089 090 @Override 091 protected void removeDemandSubscription(ConsumerId id) throws IOException { 092 List<DemandSubscription> tmpList = new ArrayList<DemandSubscription>(); 093 094 for (DemandSubscription ds : subscriptionMapByLocalId.values()) { 095 if (ds.remove(id)) { 096 LOG.debug("{} on {} from {} removed interest for: {} from {}", new Object[]{ 097 configuration.getBrokerName(), localBroker, remoteBrokerName, id, ds 098 }); 099 } 100 if (ds.isEmpty()) { 101 tmpList.add(ds); 102 } 103 } 104 105 for (DemandSubscription ds : tmpList) { 106 removeSubscription(ds); 107 LOG.debug("{} on {} from {} removed {}", new Object[]{ 108 configuration.getBrokerName(), localBroker, remoteBrokerName, ds 109 }); 110 } 111 } 112}