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 */ 017 package org.apache.activemq.network; 018 019 import java.io.IOException; 020 021 import org.apache.activemq.command.BrokerId; 022 import org.apache.activemq.command.BrokerInfo; 023 import org.apache.activemq.command.Command; 024 import org.apache.activemq.command.ConsumerInfo; 025 import org.apache.activemq.command.NetworkBridgeFilter; 026 import org.apache.activemq.transport.Transport; 027 import org.apache.activemq.util.ServiceSupport; 028 import org.apache.commons.logging.Log; 029 import org.apache.commons.logging.LogFactory; 030 031 /** 032 * Forwards messages from the local broker to the remote broker based on demand. 033 * 034 * @org.apache.xbean.XBean 035 * 036 * @version $Revision$ 037 */ 038 public class DemandForwardingBridge extends DemandForwardingBridgeSupport { 039 private static final Log LOG = LogFactory.getLog(DemandForwardingBridge.class); 040 041 protected final BrokerId remoteBrokerPath[] = new BrokerId[] {null}; 042 protected Object brokerInfoMutex = new Object(); 043 protected BrokerId remoteBrokerId; 044 045 public DemandForwardingBridge(NetworkBridgeConfiguration configuration, Transport localBroker, 046 Transport remoteBroker) { 047 super(configuration, localBroker, remoteBroker); 048 } 049 050 protected void serviceRemoteBrokerInfo(Command command) throws IOException { 051 synchronized (brokerInfoMutex) { 052 BrokerInfo remoteBrokerInfo = (BrokerInfo)command; 053 remoteBrokerId = remoteBrokerInfo.getBrokerId(); 054 remoteBrokerPath[0] = remoteBrokerId; 055 remoteBrokerName = remoteBrokerInfo.getBrokerName(); 056 if (localBrokerId != null) { 057 if (localBrokerId.equals(remoteBrokerId)) { 058 if (LOG.isTraceEnabled()) { 059 LOG.trace(configuration.getBrokerName() + " disconnecting remote loop back connection: " + remoteBrokerName); 060 } 061 ServiceSupport.dispose(this); 062 } 063 } 064 if (LOG.isTraceEnabled()) { 065 LOG.trace("counting down remoteBrokerNameKnownLatch with: " + command); 066 } 067 remoteBrokerNameKnownLatch.countDown(); 068 } 069 } 070 071 protected void addRemoteBrokerToBrokerPath(ConsumerInfo info) { 072 info.setBrokerPath(appendToBrokerPath(info.getBrokerPath(), getRemoteBrokerPath())); 073 } 074 075 protected void serviceLocalBrokerInfo(Command command) throws InterruptedException { 076 synchronized (brokerInfoMutex) { 077 localBrokerId = ((BrokerInfo)command).getBrokerId(); 078 localBrokerPath[0] = localBrokerId; 079 localBrokerIdKnownLatch.countDown(); 080 if (remoteBrokerId != null) { 081 if (remoteBrokerId.equals(localBrokerId)) { 082 if (LOG.isTraceEnabled()) { 083 LOG.trace(configuration.getBrokerName() + " disconnecting local loop back connection."); 084 } 085 waitStarted(); 086 ServiceSupport.dispose(this); 087 } 088 } 089 } 090 } 091 092 protected NetworkBridgeFilter createNetworkBridgeFilter(ConsumerInfo info) throws IOException { 093 return new NetworkBridgeFilter(remoteBrokerPath[0], configuration.getNetworkTTL()); 094 } 095 096 protected BrokerId[] getRemoteBrokerPath() { 097 return remoteBrokerPath; 098 } 099 }