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.security.GeneralSecurityException;
021import java.security.cert.X509Certificate;
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Properties;
027import java.util.concurrent.ConcurrentHashMap;
028import java.util.concurrent.ConcurrentMap;
029import java.util.concurrent.CountDownLatch;
030import java.util.concurrent.ExecutionException;
031import java.util.concurrent.ExecutorService;
032import java.util.concurrent.Executors;
033import java.util.concurrent.Future;
034import java.util.concurrent.TimeUnit;
035import java.util.concurrent.TimeoutException;
036import java.util.concurrent.atomic.AtomicBoolean;
037import java.util.concurrent.atomic.AtomicLong;
038
039import javax.management.ObjectName;
040
041import org.apache.activemq.DestinationDoesNotExistException;
042import org.apache.activemq.Service;
043import org.apache.activemq.advisory.AdvisoryBroker;
044import org.apache.activemq.advisory.AdvisorySupport;
045import org.apache.activemq.broker.BrokerService;
046import org.apache.activemq.broker.BrokerServiceAware;
047import org.apache.activemq.broker.ConnectionContext;
048import org.apache.activemq.broker.TransportConnection;
049import org.apache.activemq.broker.region.AbstractRegion;
050import org.apache.activemq.broker.region.DurableTopicSubscription;
051import org.apache.activemq.broker.region.Region;
052import org.apache.activemq.broker.region.RegionBroker;
053import org.apache.activemq.broker.region.Subscription;
054import org.apache.activemq.broker.region.policy.PolicyEntry;
055import org.apache.activemq.command.ActiveMQDestination;
056import org.apache.activemq.command.ActiveMQMessage;
057import org.apache.activemq.command.ActiveMQTempDestination;
058import org.apache.activemq.command.ActiveMQTopic;
059import org.apache.activemq.command.BrokerId;
060import org.apache.activemq.command.BrokerInfo;
061import org.apache.activemq.command.Command;
062import org.apache.activemq.command.ConnectionError;
063import org.apache.activemq.command.ConnectionId;
064import org.apache.activemq.command.ConnectionInfo;
065import org.apache.activemq.command.ConsumerId;
066import org.apache.activemq.command.ConsumerInfo;
067import org.apache.activemq.command.DataStructure;
068import org.apache.activemq.command.DestinationInfo;
069import org.apache.activemq.command.ExceptionResponse;
070import org.apache.activemq.command.KeepAliveInfo;
071import org.apache.activemq.command.Message;
072import org.apache.activemq.command.MessageAck;
073import org.apache.activemq.command.MessageDispatch;
074import org.apache.activemq.command.MessageId;
075import org.apache.activemq.command.NetworkBridgeFilter;
076import org.apache.activemq.command.ProducerInfo;
077import org.apache.activemq.command.RemoveInfo;
078import org.apache.activemq.command.RemoveSubscriptionInfo;
079import org.apache.activemq.command.Response;
080import org.apache.activemq.command.SessionInfo;
081import org.apache.activemq.command.ShutdownInfo;
082import org.apache.activemq.command.SubscriptionInfo;
083import org.apache.activemq.command.WireFormatInfo;
084import org.apache.activemq.filter.DestinationFilter;
085import org.apache.activemq.filter.MessageEvaluationContext;
086import org.apache.activemq.security.SecurityContext;
087import org.apache.activemq.transport.DefaultTransportListener;
088import org.apache.activemq.transport.FutureResponse;
089import org.apache.activemq.transport.ResponseCallback;
090import org.apache.activemq.transport.Transport;
091import org.apache.activemq.transport.TransportDisposedIOException;
092import org.apache.activemq.transport.TransportFilter;
093import org.apache.activemq.transport.tcp.SslTransport;
094import org.apache.activemq.util.IdGenerator;
095import org.apache.activemq.util.IntrospectionSupport;
096import org.apache.activemq.util.LongSequenceGenerator;
097import org.apache.activemq.util.MarshallingSupport;
098import org.apache.activemq.util.ServiceStopper;
099import org.apache.activemq.util.ServiceSupport;
100import org.slf4j.Logger;
101import org.slf4j.LoggerFactory;
102
103/**
104 * A useful base class for implementing demand forwarding bridges.
105 */
106public abstract class DemandForwardingBridgeSupport implements NetworkBridge, BrokerServiceAware {
107    private static final Logger LOG = LoggerFactory.getLogger(DemandForwardingBridgeSupport.class);
108    protected static final String DURABLE_SUB_PREFIX = "NC-DS_";
109    protected final Transport localBroker;
110    protected final Transport remoteBroker;
111    protected IdGenerator idGenerator = new IdGenerator();
112    protected final LongSequenceGenerator consumerIdGenerator = new LongSequenceGenerator();
113    protected ConnectionInfo localConnectionInfo;
114    protected ConnectionInfo remoteConnectionInfo;
115    protected SessionInfo localSessionInfo;
116    protected ProducerInfo producerInfo;
117    protected String remoteBrokerName = "Unknown";
118    protected String localClientId;
119    protected ConsumerInfo demandConsumerInfo;
120    protected int demandConsumerDispatched;
121    protected final AtomicBoolean localBridgeStarted = new AtomicBoolean(false);
122    protected final AtomicBoolean remoteBridgeStarted = new AtomicBoolean(false);
123    protected final AtomicBoolean bridgeFailed = new AtomicBoolean();
124    protected final AtomicBoolean disposed = new AtomicBoolean();
125    protected BrokerId localBrokerId;
126    protected ActiveMQDestination[] excludedDestinations;
127    protected ActiveMQDestination[] dynamicallyIncludedDestinations;
128    protected ActiveMQDestination[] staticallyIncludedDestinations;
129    protected ActiveMQDestination[] durableDestinations;
130    protected final ConcurrentMap<ConsumerId, DemandSubscription> subscriptionMapByLocalId = new ConcurrentHashMap<ConsumerId, DemandSubscription>();
131    protected final ConcurrentMap<ConsumerId, DemandSubscription> subscriptionMapByRemoteId = new ConcurrentHashMap<ConsumerId, DemandSubscription>();
132    protected final BrokerId localBrokerPath[] = new BrokerId[]{null};
133    protected final CountDownLatch startedLatch = new CountDownLatch(2);
134    protected final CountDownLatch localStartedLatch = new CountDownLatch(1);
135    protected final AtomicBoolean lastConnectSucceeded = new AtomicBoolean(false);
136    protected NetworkBridgeConfiguration configuration;
137    protected final NetworkBridgeFilterFactory defaultFilterFactory = new DefaultNetworkBridgeFilterFactory();
138
139    protected final BrokerId remoteBrokerPath[] = new BrokerId[]{null};
140    protected BrokerId remoteBrokerId;
141
142    final AtomicLong enqueueCounter = new AtomicLong();
143    final AtomicLong dequeueCounter = new AtomicLong();
144
145    private NetworkBridgeListener networkBridgeListener;
146    private boolean createdByDuplex;
147    private BrokerInfo localBrokerInfo;
148    private BrokerInfo remoteBrokerInfo;
149
150    private final FutureBrokerInfo futureRemoteBrokerInfo = new FutureBrokerInfo(remoteBrokerInfo, disposed);
151    private final FutureBrokerInfo futureLocalBrokerInfo = new FutureBrokerInfo(localBrokerInfo, disposed);
152
153    private final AtomicBoolean started = new AtomicBoolean();
154    private TransportConnection duplexInitiatingConnection;
155    private final AtomicBoolean duplexInitiatingConnectionInfoReceived = new AtomicBoolean();
156    protected BrokerService brokerService = null;
157    private ObjectName mbeanObjectName;
158    private final ExecutorService serialExecutor = Executors.newSingleThreadExecutor();
159    private Transport duplexInboundLocalBroker = null;
160    private ProducerInfo duplexInboundLocalProducerInfo;
161
162    public DemandForwardingBridgeSupport(NetworkBridgeConfiguration configuration, Transport localBroker, Transport remoteBroker) {
163        this.configuration = configuration;
164        this.localBroker = localBroker;
165        this.remoteBroker = remoteBroker;
166    }
167
168    public void duplexStart(TransportConnection connection, BrokerInfo localBrokerInfo, BrokerInfo remoteBrokerInfo) throws Exception {
169        this.localBrokerInfo = localBrokerInfo;
170        this.remoteBrokerInfo = remoteBrokerInfo;
171        this.duplexInitiatingConnection = connection;
172        start();
173        serviceRemoteCommand(remoteBrokerInfo);
174    }
175
176    @Override
177    public void start() throws Exception {
178        if (started.compareAndSet(false, true)) {
179
180            if (brokerService == null) {
181                throw new IllegalArgumentException("BrokerService is null on " + this);
182            }
183
184            if (isDuplex()) {
185                duplexInboundLocalBroker = NetworkBridgeFactory.createLocalTransport(brokerService.getBroker());
186                duplexInboundLocalBroker.setTransportListener(new DefaultTransportListener() {
187
188                    @Override
189                    public void onCommand(Object o) {
190                        Command command = (Command) o;
191                        serviceLocalCommand(command);
192                    }
193
194                    @Override
195                    public void onException(IOException error) {
196                        serviceLocalException(error);
197                    }
198                });
199                duplexInboundLocalBroker.start();
200            }
201
202            localBroker.setTransportListener(new DefaultTransportListener() {
203
204                @Override
205                public void onCommand(Object o) {
206                    Command command = (Command) o;
207                    serviceLocalCommand(command);
208                }
209
210                @Override
211                public void onException(IOException error) {
212                    if (!futureLocalBrokerInfo.isDone()) {
213                        futureLocalBrokerInfo.cancel(true);
214                        return;
215                    }
216                    serviceLocalException(error);
217                }
218            });
219
220            remoteBroker.setTransportListener(new DefaultTransportListener() {
221
222                @Override
223                public void onCommand(Object o) {
224                    Command command = (Command) o;
225                    serviceRemoteCommand(command);
226                }
227
228                @Override
229                public void onException(IOException error) {
230                    if (!futureRemoteBrokerInfo.isDone()) {
231                        futureRemoteBrokerInfo.cancel(true);
232                        return;
233                    }
234                    serviceRemoteException(error);
235                }
236            });
237
238            remoteBroker.start();
239            localBroker.start();
240
241            if (!disposed.get()) {
242                try {
243                    triggerStartAsyncNetworkBridgeCreation();
244                } catch (IOException e) {
245                    LOG.warn("Caught exception from remote start", e);
246                }
247            } else {
248                LOG.warn("Bridge was disposed before the start() method was fully executed.");
249                throw new TransportDisposedIOException();
250            }
251        }
252    }
253
254    @Override
255    public void stop() throws Exception {
256        if (started.compareAndSet(true, false)) {
257            if (disposed.compareAndSet(false, true)) {
258                LOG.debug(" stopping {} bridge to {}", configuration.getBrokerName(), remoteBrokerName);
259
260                futureRemoteBrokerInfo.cancel(true);
261                futureLocalBrokerInfo.cancel(true);
262
263                NetworkBridgeListener l = this.networkBridgeListener;
264                if (l != null) {
265                    l.onStop(this);
266                }
267                try {
268                    // local start complete
269                    if (startedLatch.getCount() < 2) {
270                        LOG.trace("{} unregister bridge ({}) to {}", new Object[]{
271                                configuration.getBrokerName(), this, remoteBrokerName
272                        });
273                        brokerService.getBroker().removeBroker(null, remoteBrokerInfo);
274                        brokerService.getBroker().networkBridgeStopped(remoteBrokerInfo);
275                    }
276
277                    remoteBridgeStarted.set(false);
278                    final CountDownLatch sendShutdown = new CountDownLatch(1);
279
280                    brokerService.getTaskRunnerFactory().execute(new Runnable() {
281                        @Override
282                        public void run() {
283                            try {
284                                serialExecutor.shutdown();
285                                if (!serialExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
286                                    List<Runnable> pendingTasks = serialExecutor.shutdownNow();
287                                    LOG.info("pending tasks on stop {}", pendingTasks);
288                                }
289                                localBroker.oneway(new ShutdownInfo());
290                                remoteBroker.oneway(new ShutdownInfo());
291                            } catch (Throwable e) {
292                                LOG.debug("Caught exception sending shutdown", e);
293                            } finally {
294                                sendShutdown.countDown();
295                            }
296
297                        }
298                    }, "ActiveMQ ForwardingBridge StopTask");
299
300                    if (!sendShutdown.await(10, TimeUnit.SECONDS)) {
301                        LOG.info("Network Could not shutdown in a timely manner");
302                    }
303                } finally {
304                    ServiceStopper ss = new ServiceStopper();
305                    ss.stop(remoteBroker);
306                    ss.stop(localBroker);
307                    ss.stop(duplexInboundLocalBroker);
308                    // Release the started Latch since another thread could be
309                    // stuck waiting for it to start up.
310                    startedLatch.countDown();
311                    startedLatch.countDown();
312                    localStartedLatch.countDown();
313
314                    ss.throwFirstException();
315                }
316            }
317
318            LOG.info("{} bridge to {} stopped", configuration.getBrokerName(), remoteBrokerName);
319        }
320    }
321
322    protected void triggerStartAsyncNetworkBridgeCreation() throws IOException {
323        brokerService.getTaskRunnerFactory().execute(new Runnable() {
324            @Override
325            public void run() {
326                final String originalName = Thread.currentThread().getName();
327                Thread.currentThread().setName("triggerStartAsyncNetworkBridgeCreation: " +
328                        "remoteBroker=" + remoteBroker + ", localBroker= " + localBroker);
329
330                try {
331                    // First we collect the info data from both the local and remote ends
332                    collectBrokerInfos();
333
334                    // Once we have all required broker info we can attempt to start
335                    // the local and then remote sides of the bridge.
336                    doStartLocalAndRemoteBridges();
337                } finally {
338                    Thread.currentThread().setName(originalName);
339                }
340            }
341        });
342    }
343
344    private void collectBrokerInfos() {
345
346        // First wait for the remote to feed us its BrokerInfo, then we can check on
347        // the LocalBrokerInfo and decide is this is a loop.
348        try {
349            remoteBrokerInfo = futureRemoteBrokerInfo.get();
350            if (remoteBrokerInfo == null) {
351                serviceLocalException(new Throwable("remoteBrokerInfo is null"));
352                return;
353            }
354        } catch (Exception e) {
355            serviceRemoteException(e);
356            return;
357        }
358
359        try {
360            localBrokerInfo = futureLocalBrokerInfo.get();
361            if (localBrokerInfo == null) {
362                serviceLocalException(new Throwable("localBrokerInfo is null"));
363                return;
364            }
365
366            // Before we try and build the bridge lets check if we are in a loop
367            // and if so just stop now before registering anything.
368            remoteBrokerId = remoteBrokerInfo.getBrokerId();
369            if (localBrokerId.equals(remoteBrokerId)) {
370                LOG.trace("{} disconnecting remote loop back connector for: {}, with id: {}", new Object[]{
371                        configuration.getBrokerName(), remoteBrokerName, remoteBrokerId
372                });
373                ServiceSupport.dispose(localBroker);
374                ServiceSupport.dispose(remoteBroker);
375                // the bridge is left in a bit of limbo, but it won't get retried
376                // in this state.
377                return;
378            }
379
380            // Fill in the remote broker's information now.
381            remoteBrokerPath[0] = remoteBrokerId;
382            remoteBrokerName = remoteBrokerInfo.getBrokerName();
383            if (configuration.isUseBrokerNamesAsIdSeed()) {
384                idGenerator = new IdGenerator(brokerService.getBrokerName() + "->" + remoteBrokerName);
385            }
386        } catch (Throwable e) {
387            serviceLocalException(e);
388        }
389    }
390
391    private void doStartLocalAndRemoteBridges() {
392
393        if (disposed.get()) {
394            return;
395        }
396
397        if (isCreatedByDuplex()) {
398            // apply remote (propagated) configuration to local duplex bridge before start
399            Properties props = null;
400            try {
401                props = MarshallingSupport.stringToProperties(remoteBrokerInfo.getNetworkProperties());
402                IntrospectionSupport.getProperties(configuration, props, null);
403                if (configuration.getExcludedDestinations() != null) {
404                    excludedDestinations = configuration.getExcludedDestinations().toArray(
405                            new ActiveMQDestination[configuration.getExcludedDestinations().size()]);
406                }
407                if (configuration.getStaticallyIncludedDestinations() != null) {
408                    staticallyIncludedDestinations = configuration.getStaticallyIncludedDestinations().toArray(
409                            new ActiveMQDestination[configuration.getStaticallyIncludedDestinations().size()]);
410                }
411                if (configuration.getDynamicallyIncludedDestinations() != null) {
412                    dynamicallyIncludedDestinations = configuration.getDynamicallyIncludedDestinations().toArray(
413                            new ActiveMQDestination[configuration.getDynamicallyIncludedDestinations().size()]);
414                }
415            } catch (Throwable t) {
416                LOG.error("Error mapping remote configuration: {}", props, t);
417            }
418        }
419
420        try {
421            startLocalBridge();
422        } catch (Throwable e) {
423            serviceLocalException(e);
424            return;
425        }
426
427        try {
428            startRemoteBridge();
429        } catch (Throwable e) {
430            serviceRemoteException(e);
431            return;
432        }
433
434        try {
435            if (safeWaitUntilStarted()) {
436                setupStaticDestinations();
437            }
438        } catch (Throwable e) {
439            serviceLocalException(e);
440        }
441    }
442
443    private void startLocalBridge() throws Throwable {
444        if (!bridgeFailed.get() && localBridgeStarted.compareAndSet(false, true)) {
445            synchronized (this) {
446                LOG.trace("{} starting local Bridge, localBroker={}", configuration.getBrokerName(), localBroker);
447                if (!disposed.get()) {
448
449                    if (idGenerator == null) {
450                        throw new IllegalStateException("Id Generator cannot be null");
451                    }
452
453                    localConnectionInfo = new ConnectionInfo();
454                    localConnectionInfo.setConnectionId(new ConnectionId(idGenerator.generateId()));
455                    localClientId = configuration.getName() + "_" + remoteBrokerName + "_inbound_" + configuration.getBrokerName();
456                    localConnectionInfo.setClientId(localClientId);
457                    localConnectionInfo.setUserName(configuration.getUserName());
458                    localConnectionInfo.setPassword(configuration.getPassword());
459                    Transport originalTransport = remoteBroker;
460                    while (originalTransport instanceof TransportFilter) {
461                        originalTransport = ((TransportFilter) originalTransport).getNext();
462                    }
463                    if (originalTransport instanceof SslTransport) {
464                        X509Certificate[] peerCerts = ((SslTransport) originalTransport).getPeerCertificates();
465                        localConnectionInfo.setTransportContext(peerCerts);
466                    }
467                    // sync requests that may fail
468                    Object resp = localBroker.request(localConnectionInfo);
469                    if (resp instanceof ExceptionResponse) {
470                        throw ((ExceptionResponse) resp).getException();
471                    }
472                    localSessionInfo = new SessionInfo(localConnectionInfo, 1);
473                    localBroker.oneway(localSessionInfo);
474
475                    if (configuration.isDuplex()) {
476                        // separate in-bound channel for forwards so we don't
477                        // contend with out-bound dispatch on same connection
478                        remoteBrokerInfo.setNetworkConnection(true);
479                        duplexInboundLocalBroker.oneway(remoteBrokerInfo);
480
481                        ConnectionInfo duplexLocalConnectionInfo = new ConnectionInfo();
482                        duplexLocalConnectionInfo.setConnectionId(new ConnectionId(idGenerator.generateId()));
483                        duplexLocalConnectionInfo.setClientId(configuration.getName() + "_" + remoteBrokerName + "_inbound_duplex_"
484                                + configuration.getBrokerName());
485                        duplexLocalConnectionInfo.setUserName(configuration.getUserName());
486                        duplexLocalConnectionInfo.setPassword(configuration.getPassword());
487
488                        if (originalTransport instanceof SslTransport) {
489                            X509Certificate[] peerCerts = ((SslTransport) originalTransport).getPeerCertificates();
490                            duplexLocalConnectionInfo.setTransportContext(peerCerts);
491                        }
492                        // sync requests that may fail
493                        resp = duplexInboundLocalBroker.request(duplexLocalConnectionInfo);
494                        if (resp instanceof ExceptionResponse) {
495                            throw ((ExceptionResponse) resp).getException();
496                        }
497                        SessionInfo duplexInboundSession = new SessionInfo(duplexLocalConnectionInfo, 1);
498                        duplexInboundLocalProducerInfo = new ProducerInfo(duplexInboundSession, 1);
499                        duplexInboundLocalBroker.oneway(duplexInboundSession);
500                        duplexInboundLocalBroker.oneway(duplexInboundLocalProducerInfo);
501                    }
502                    brokerService.getBroker().networkBridgeStarted(remoteBrokerInfo, this.createdByDuplex, remoteBroker.toString());
503                    NetworkBridgeListener l = this.networkBridgeListener;
504                    if (l != null) {
505                        l.onStart(this);
506                    }
507
508                    // Let the local broker know the remote broker's ID.
509                    localBroker.oneway(remoteBrokerInfo);
510                    // new peer broker (a consumer can work with remote broker also)
511                    brokerService.getBroker().addBroker(null, remoteBrokerInfo);
512
513                    LOG.info("Network connection between {} and {} ({}) has been established.", new Object[]{
514                            localBroker, remoteBroker, remoteBrokerName
515                    });
516                    LOG.trace("{} register bridge ({}) to {}", new Object[]{
517                            configuration.getBrokerName(), this, remoteBrokerName
518                    });
519                } else {
520                    LOG.warn("Bridge was disposed before the startLocalBridge() method was fully executed.");
521                }
522                startedLatch.countDown();
523                localStartedLatch.countDown();
524            }
525        }
526    }
527
528    protected void startRemoteBridge() throws Exception {
529        if (!bridgeFailed.get() && remoteBridgeStarted.compareAndSet(false, true)) {
530            LOG.trace("{} starting remote Bridge, remoteBroker={}", configuration.getBrokerName(), remoteBroker);
531            synchronized (this) {
532                if (!isCreatedByDuplex()) {
533                    BrokerInfo brokerInfo = new BrokerInfo();
534                    brokerInfo.setBrokerName(configuration.getBrokerName());
535                    brokerInfo.setBrokerURL(configuration.getBrokerURL());
536                    brokerInfo.setNetworkConnection(true);
537                    brokerInfo.setDuplexConnection(configuration.isDuplex());
538                    // set our properties
539                    Properties props = new Properties();
540                    IntrospectionSupport.getProperties(configuration, props, null);
541                    props.remove("networkTTL");
542                    String str = MarshallingSupport.propertiesToString(props);
543                    brokerInfo.setNetworkProperties(str);
544                    brokerInfo.setBrokerId(this.localBrokerId);
545                    remoteBroker.oneway(brokerInfo);
546                }
547                if (remoteConnectionInfo != null) {
548                    remoteBroker.oneway(remoteConnectionInfo.createRemoveCommand());
549                }
550                remoteConnectionInfo = new ConnectionInfo();
551                remoteConnectionInfo.setConnectionId(new ConnectionId(idGenerator.generateId()));
552                remoteConnectionInfo.setClientId(configuration.getName() + "_" + configuration.getBrokerName() + "_outbound");
553                remoteConnectionInfo.setUserName(configuration.getUserName());
554                remoteConnectionInfo.setPassword(configuration.getPassword());
555                remoteBroker.oneway(remoteConnectionInfo);
556
557                SessionInfo remoteSessionInfo = new SessionInfo(remoteConnectionInfo, 1);
558                remoteBroker.oneway(remoteSessionInfo);
559                producerInfo = new ProducerInfo(remoteSessionInfo, 1);
560                producerInfo.setResponseRequired(false);
561                remoteBroker.oneway(producerInfo);
562                // Listen to consumer advisory messages on the remote broker to determine demand.
563                if (!configuration.isStaticBridge()) {
564                    demandConsumerInfo = new ConsumerInfo(remoteSessionInfo, 1);
565                    // always dispatch advisory message asynchronously so that
566                    // we never block the producer broker if we are slow
567                    demandConsumerInfo.setDispatchAsync(true);
568                    String advisoryTopic = configuration.getDestinationFilter();
569                    if (configuration.isBridgeTempDestinations()) {
570                        advisoryTopic += "," + AdvisorySupport.TEMP_DESTINATION_COMPOSITE_ADVISORY_TOPIC;
571                    }
572                    demandConsumerInfo.setDestination(new ActiveMQTopic(advisoryTopic));
573                    demandConsumerInfo.setPrefetchSize(configuration.getPrefetchSize());
574                    remoteBroker.oneway(demandConsumerInfo);
575                }
576                startedLatch.countDown();
577            }
578        }
579    }
580
581    @Override
582    public void serviceRemoteException(Throwable error) {
583        if (!disposed.get()) {
584            if (error instanceof SecurityException || error instanceof GeneralSecurityException) {
585                LOG.error("Network connection between {} and {} shutdown due to a remote error: {}", new Object[]{
586                        localBroker, remoteBroker, error
587                });
588            } else {
589                LOG.warn("Network connection between {} and {} shutdown due to a remote error: {}", new Object[]{
590                        localBroker, remoteBroker, error
591                });
592            }
593            LOG.debug("The remote Exception was: {}", error, error);
594            brokerService.getTaskRunnerFactory().execute(new Runnable() {
595                @Override
596                public void run() {
597                    ServiceSupport.dispose(getControllingService());
598                }
599            });
600            fireBridgeFailed(error);
601        }
602    }
603
604    protected void serviceRemoteCommand(Command command) {
605        if (!disposed.get()) {
606            try {
607                if (command.isMessageDispatch()) {
608                    safeWaitUntilStarted();
609                    MessageDispatch md = (MessageDispatch) command;
610                    serviceRemoteConsumerAdvisory(md.getMessage().getDataStructure());
611                    ackAdvisory(md.getMessage());
612                } else if (command.isBrokerInfo()) {
613                    futureRemoteBrokerInfo.set((BrokerInfo) command);
614                } else if (command.getClass() == ConnectionError.class) {
615                    ConnectionError ce = (ConnectionError) command;
616                    serviceRemoteException(ce.getException());
617                } else {
618                    if (isDuplex()) {
619                        LOG.trace("{} duplex command type: {}", configuration.getBrokerName(), command.getDataStructureType());
620                        if (command.isMessage()) {
621                            final ActiveMQMessage message = (ActiveMQMessage) command;
622                            if (NetworkBridgeFilter.isAdvisoryInterpretedByNetworkBridge(message)) {
623                                serviceRemoteConsumerAdvisory(message.getDataStructure());
624                                ackAdvisory(message);
625                            } else {
626                                if (!isPermissableDestination(message.getDestination(), true)) {
627                                    return;
628                                }
629                                // message being forwarded - we need to
630                                // propagate the response to our local send
631                                if (canDuplexDispatch(message)) {
632                                    message.setProducerId(duplexInboundLocalProducerInfo.getProducerId());
633                                    if (message.isResponseRequired() || configuration.isAlwaysSyncSend()) {
634                                        duplexInboundLocalBroker.asyncRequest(message, new ResponseCallback() {
635                                            final int correlationId = message.getCommandId();
636
637                                            @Override
638                                            public void onCompletion(FutureResponse resp) {
639                                                try {
640                                                    Response reply = resp.getResult();
641                                                    reply.setCorrelationId(correlationId);
642                                                    remoteBroker.oneway(reply);
643                                                } catch (IOException error) {
644                                                    LOG.error("Exception: {} on duplex forward of: {}", error, message);
645                                                    serviceRemoteException(error);
646                                                }
647                                            }
648                                        });
649                                    } else {
650                                        duplexInboundLocalBroker.oneway(message);
651                                    }
652                                    serviceInboundMessage(message);
653                                } else {
654                                    if (message.isResponseRequired() || configuration.isAlwaysSyncSend()) {
655                                        Response reply = new Response();
656                                        reply.setCorrelationId(message.getCommandId());
657                                        remoteBroker.oneway(reply);
658                                    }
659                                }
660                            }
661                        } else {
662                            switch (command.getDataStructureType()) {
663                                case ConnectionInfo.DATA_STRUCTURE_TYPE:
664                                    if (duplexInitiatingConnection != null && duplexInitiatingConnectionInfoReceived.compareAndSet(false, true)) {
665                                        // end of initiating connection setup - propogate to initial connection to get mbean by clientid
666                                        duplexInitiatingConnection.processAddConnection((ConnectionInfo) command);
667                                    } else {
668                                        localBroker.oneway(command);
669                                    }
670                                    break;
671                                case SessionInfo.DATA_STRUCTURE_TYPE:
672                                    localBroker.oneway(command);
673                                    break;
674                                case ProducerInfo.DATA_STRUCTURE_TYPE:
675                                    // using duplexInboundLocalProducerInfo
676                                    break;
677                                case MessageAck.DATA_STRUCTURE_TYPE:
678                                    MessageAck ack = (MessageAck) command;
679                                    DemandSubscription localSub = subscriptionMapByRemoteId.get(ack.getConsumerId());
680                                    if (localSub != null) {
681                                        ack.setConsumerId(localSub.getLocalInfo().getConsumerId());
682                                        localBroker.oneway(ack);
683                                    } else {
684                                        LOG.warn("Matching local subscription not found for ack: {}", ack);
685                                    }
686                                    break;
687                                case ConsumerInfo.DATA_STRUCTURE_TYPE:
688                                    localStartedLatch.await();
689                                    if (started.get()) {
690                                        addConsumerInfo((ConsumerInfo) command);
691                                    } else {
692                                        // received a subscription whilst stopping
693                                        LOG.warn("Stopping - ignoring ConsumerInfo: {}", command);
694                                    }
695                                    break;
696                                case ShutdownInfo.DATA_STRUCTURE_TYPE:
697                                    // initiator is shutting down, controlled case
698                                    // abortive close dealt with by inactivity monitor
699                                    LOG.info("Stopping network bridge on shutdown of remote broker");
700                                    serviceRemoteException(new IOException(command.toString()));
701                                    break;
702                                default:
703                                    LOG.debug("Ignoring remote command: {}", command);
704                            }
705                        }
706                    } else {
707                        switch (command.getDataStructureType()) {
708                            case KeepAliveInfo.DATA_STRUCTURE_TYPE:
709                            case WireFormatInfo.DATA_STRUCTURE_TYPE:
710                            case ShutdownInfo.DATA_STRUCTURE_TYPE:
711                                break;
712                            default:
713                                LOG.warn("Unexpected remote command: {}", command);
714                        }
715                    }
716                }
717            } catch (Throwable e) {
718                LOG.debug("Exception processing remote command: {}", command, e);
719                serviceRemoteException(e);
720            }
721        }
722    }
723
724    private void ackAdvisory(Message message) throws IOException {
725        demandConsumerDispatched++;
726        if (demandConsumerDispatched > (demandConsumerInfo.getPrefetchSize() * .75)) {
727            MessageAck ack = new MessageAck(message, MessageAck.STANDARD_ACK_TYPE, demandConsumerDispatched);
728            ack.setConsumerId(demandConsumerInfo.getConsumerId());
729            remoteBroker.oneway(ack);
730            demandConsumerDispatched = 0;
731        }
732    }
733
734    private void serviceRemoteConsumerAdvisory(DataStructure data) throws IOException {
735        final int networkTTL = configuration.getConsumerTTL();
736        if (data.getClass() == ConsumerInfo.class) {
737            // Create a new local subscription
738            ConsumerInfo info = (ConsumerInfo) data;
739            BrokerId[] path = info.getBrokerPath();
740
741            if (info.isBrowser()) {
742                LOG.debug("{} Ignoring sub from {}, browsers explicitly suppressed", configuration.getBrokerName(), remoteBrokerName);
743                return;
744            }
745
746            if (path != null && networkTTL > -1 && path.length >= networkTTL) {
747                LOG.debug("{} Ignoring sub from {}, restricted to {} network hops only: {}", new Object[]{
748                        configuration.getBrokerName(), remoteBrokerName, networkTTL, info
749                });
750                return;
751            }
752
753            if (contains(path, localBrokerPath[0])) {
754                // Ignore this consumer as it's a consumer we locally sent to the broker.
755                LOG.debug("{} Ignoring sub from {}, already routed through this broker once: {}", new Object[]{
756                        configuration.getBrokerName(), remoteBrokerName, info
757                });
758                return;
759            }
760
761            if (!isPermissableDestination(info.getDestination())) {
762                // ignore if not in the permitted or in the excluded list
763                LOG.debug("{} Ignoring sub from {}, destination {} is not permitted: {}", new Object[]{
764                        configuration.getBrokerName(), remoteBrokerName, info.getDestination(), info
765                });
766                return;
767            }
768
769            // in a cyclic network there can be multiple bridges per broker that can propagate
770            // a network subscription so there is a need to synchronize on a shared entity
771            synchronized (brokerService.getVmConnectorURI()) {
772                addConsumerInfo(info);
773            }
774        } else if (data.getClass() == DestinationInfo.class) {
775            // It's a destination info - we want to pass up information about temporary destinations
776            final DestinationInfo destInfo = (DestinationInfo) data;
777            BrokerId[] path = destInfo.getBrokerPath();
778            if (path != null && networkTTL > -1 && path.length >= networkTTL) {
779                LOG.debug("{} Ignoring destination {} restricted to {} network hops only", new Object[]{
780                        configuration.getBrokerName(), destInfo, networkTTL
781                });
782                return;
783            }
784            if (contains(destInfo.getBrokerPath(), localBrokerPath[0])) {
785                LOG.debug("{} Ignoring destination {} already routed through this broker once", configuration.getBrokerName(), destInfo);
786                return;
787            }
788            destInfo.setConnectionId(localConnectionInfo.getConnectionId());
789            if (destInfo.getDestination() instanceof ActiveMQTempDestination) {
790                // re-set connection id so comes from here
791                ActiveMQTempDestination tempDest = (ActiveMQTempDestination) destInfo.getDestination();
792                tempDest.setConnectionId(localSessionInfo.getSessionId().getConnectionId());
793            }
794            destInfo.setBrokerPath(appendToBrokerPath(destInfo.getBrokerPath(), getRemoteBrokerPath()));
795            LOG.trace("{} bridging {} destination on {} from {}, destination: {}", new Object[]{
796                    configuration.getBrokerName(), (destInfo.isAddOperation() ? "add" : "remove"), localBroker, remoteBrokerName, destInfo
797            });
798            if (destInfo.isRemoveOperation()) {
799                // Serialize with removeSub operations such that all removeSub advisories
800                // are generated
801                serialExecutor.execute(new Runnable() {
802                    @Override
803                    public void run() {
804                        try {
805                            localBroker.oneway(destInfo);
806                        } catch (IOException e) {
807                            LOG.warn("failed to deliver remove command for destination: {}", destInfo.getDestination(), e);
808                        }
809                    }
810                });
811            } else {
812                localBroker.oneway(destInfo);
813            }
814        } else if (data.getClass() == RemoveInfo.class) {
815            ConsumerId id = (ConsumerId) ((RemoveInfo) data).getObjectId();
816            removeDemandSubscription(id);
817        } else if (data.getClass() == RemoveSubscriptionInfo.class) {
818            RemoveSubscriptionInfo info = ((RemoveSubscriptionInfo) data);
819            SubscriptionInfo subscriptionInfo = new SubscriptionInfo(info.getClientId(), info.getSubscriptionName());
820            for (Iterator<DemandSubscription> i = subscriptionMapByLocalId.values().iterator(); i.hasNext(); ) {
821                DemandSubscription ds = i.next();
822                boolean removed = ds.getDurableRemoteSubs().remove(subscriptionInfo);
823                if (removed) {
824                    if (ds.getDurableRemoteSubs().isEmpty()) {
825
826                        // deactivate subscriber
827                        RemoveInfo removeInfo = new RemoveInfo(ds.getLocalInfo().getConsumerId());
828                        localBroker.oneway(removeInfo);
829
830                        // remove subscriber
831                        RemoveSubscriptionInfo sending = new RemoveSubscriptionInfo();
832                        sending.setClientId(localClientId);
833                        sending.setSubscriptionName(ds.getLocalDurableSubscriber().getSubscriptionName());
834                        sending.setConnectionId(this.localConnectionInfo.getConnectionId());
835                        localBroker.oneway(sending);
836                    }
837                }
838            }
839        }
840    }
841
842    @Override
843    public void serviceLocalException(Throwable error) {
844        serviceLocalException(null, error);
845    }
846
847    public void serviceLocalException(MessageDispatch messageDispatch, Throwable error) {
848        LOG.trace("serviceLocalException: disposed {} ex", disposed.get(), error);
849        if (!disposed.get()) {
850            if (error instanceof DestinationDoesNotExistException && ((DestinationDoesNotExistException) error).isTemporary()) {
851                // not a reason to terminate the bridge - temps can disappear with
852                // pending sends as the demand sub may outlive the remote dest
853                if (messageDispatch != null) {
854                    LOG.warn("PoisonAck of {} on forwarding error: {}", messageDispatch.getMessage().getMessageId(), error);
855                    try {
856                        MessageAck poisonAck = new MessageAck(messageDispatch, MessageAck.POSION_ACK_TYPE, 1);
857                        poisonAck.setPoisonCause(error);
858                        localBroker.oneway(poisonAck);
859                    } catch (IOException ioe) {
860                        LOG.error("Failed to posion ack message following forward failure: ", ioe);
861                    }
862                    fireFailedForwardAdvisory(messageDispatch, error);
863                } else {
864                    LOG.warn("Ignoring exception on forwarding to non existent temp dest: ", error);
865                }
866                return;
867            }
868
869            LOG.info("Network connection between {} and {} shutdown due to a local error: {}", new Object[]{localBroker, remoteBroker, error});
870            LOG.debug("The local Exception was: {}", error, error);
871
872            brokerService.getTaskRunnerFactory().execute(new Runnable() {
873                @Override
874                public void run() {
875                    ServiceSupport.dispose(getControllingService());
876                }
877            });
878            fireBridgeFailed(error);
879        }
880    }
881
882    private void fireFailedForwardAdvisory(MessageDispatch messageDispatch, Throwable error) {
883        if (configuration.isAdvisoryForFailedForward()) {
884            AdvisoryBroker advisoryBroker = null;
885            try {
886                advisoryBroker = (AdvisoryBroker) brokerService.getBroker().getAdaptor(AdvisoryBroker.class);
887
888                if (advisoryBroker != null) {
889                    ConnectionContext context = new ConnectionContext();
890                    context.setSecurityContext(SecurityContext.BROKER_SECURITY_CONTEXT);
891                    context.setBroker(brokerService.getBroker());
892
893                    ActiveMQMessage advisoryMessage = new ActiveMQMessage();
894                    advisoryMessage.setStringProperty("cause", error.getLocalizedMessage());
895                    advisoryBroker.fireAdvisory(context, AdvisorySupport.getNetworkBridgeForwardFailureAdvisoryTopic(), messageDispatch.getMessage(), null,
896                            advisoryMessage);
897
898                }
899            } catch (Exception e) {
900                LOG.warn("failed to fire forward failure advisory, cause: {}", e);
901                LOG.debug("detail", e);
902            }
903        }
904    }
905
906    protected Service getControllingService() {
907        return duplexInitiatingConnection != null ? duplexInitiatingConnection : DemandForwardingBridgeSupport.this;
908    }
909
910    protected void addSubscription(DemandSubscription sub) throws IOException {
911        if (sub != null) {
912            if (isDuplex()) {
913                // async vm transport, need to wait for completion
914                localBroker.request(sub.getLocalInfo());
915            } else {
916                localBroker.oneway(sub.getLocalInfo());
917            }
918        }
919    }
920
921    protected void removeSubscription(final DemandSubscription sub) throws IOException {
922        if (sub != null) {
923            LOG.trace("{} remove local subscription: {} for remote {}", new Object[]{configuration.getBrokerName(), sub.getLocalInfo().getConsumerId(), sub.getRemoteInfo().getConsumerId()});
924
925            // ensure not available for conduit subs pending removal
926            subscriptionMapByLocalId.remove(sub.getLocalInfo().getConsumerId());
927            subscriptionMapByRemoteId.remove(sub.getRemoteInfo().getConsumerId());
928
929            // continue removal in separate thread to free up this thread for outstanding responses
930            // Serialize with removeDestination operations so that removeSubs are serialized with
931            // removeDestinations such that all removeSub advisories are generated
932            serialExecutor.execute(new Runnable() {
933                @Override
934                public void run() {
935                    sub.waitForCompletion();
936                    try {
937                        localBroker.oneway(sub.getLocalInfo().createRemoveCommand());
938                    } catch (IOException e) {
939                        LOG.warn("failed to deliver remove command for local subscription, for remote {}", sub.getRemoteInfo().getConsumerId(), e);
940                    }
941                }
942            });
943        }
944    }
945
946    protected Message configureMessage(MessageDispatch md) throws IOException {
947        Message message = md.getMessage().copy();
948        // Update the packet to show where it came from.
949        message.setBrokerPath(appendToBrokerPath(message.getBrokerPath(), localBrokerPath));
950        message.setProducerId(producerInfo.getProducerId());
951        message.setDestination(md.getDestination());
952        message.setMemoryUsage(null);
953        if (message.getOriginalTransactionId() == null) {
954            message.setOriginalTransactionId(message.getTransactionId());
955        }
956        message.setTransactionId(null);
957        if (configuration.isUseCompression()) {
958            message.compress();
959        }
960        return message;
961    }
962
963    protected void serviceLocalCommand(Command command) {
964        if (!disposed.get()) {
965            try {
966                if (command.isMessageDispatch()) {
967                    safeWaitUntilStarted();
968                    enqueueCounter.incrementAndGet();
969                    final MessageDispatch md = (MessageDispatch) command;
970                    final DemandSubscription sub = subscriptionMapByLocalId.get(md.getConsumerId());
971                    if (sub != null && md.getMessage() != null && sub.incrementOutstandingResponses()) {
972
973                        if (suppressMessageDispatch(md, sub)) {
974                            LOG.debug("{} message not forwarded to {} because message came from there or fails TTL, brokerPath: {}, message: {}", new Object[]{
975                                    configuration.getBrokerName(), remoteBrokerName, Arrays.toString(md.getMessage().getBrokerPath()), md.getMessage()
976                            });
977                            // still ack as it may be durable
978                            try {
979                                localBroker.oneway(new MessageAck(md, MessageAck.INDIVIDUAL_ACK_TYPE, 1));
980                            } finally {
981                                sub.decrementOutstandingResponses();
982                            }
983                            return;
984                        }
985
986                        Message message = configureMessage(md);
987                        LOG.debug("bridging ({} -> {}), consumer: {}, destination: {}, brokerPath: {}, message: {}", new Object[]{
988                                configuration.getBrokerName(), remoteBrokerName, (LOG.isTraceEnabled() ? message : message.getMessageId()), md.getConsumerId(), message.getDestination(), Arrays.toString(message.getBrokerPath()), message
989                        });
990
991                        if (isDuplex() && NetworkBridgeFilter.isAdvisoryInterpretedByNetworkBridge(message)) {
992                            try {
993                                // never request b/c they are eventually acked async
994                                remoteBroker.oneway(message);
995                            } finally {
996                                sub.decrementOutstandingResponses();
997                            }
998                            return;
999                        }
1000
1001                        if (message.isPersistent() || configuration.isAlwaysSyncSend()) {
1002
1003                            // The message was not sent using async send, so we should only
1004                            // ack the local broker when we get confirmation that the remote
1005                            // broker has received the message.
1006                            remoteBroker.asyncRequest(message, new ResponseCallback() {
1007                                @Override
1008                                public void onCompletion(FutureResponse future) {
1009                                    try {
1010                                        Response response = future.getResult();
1011                                        if (response.isException()) {
1012                                            ExceptionResponse er = (ExceptionResponse) response;
1013                                            serviceLocalException(md, er.getException());
1014                                        } else {
1015                                            localBroker.oneway(new MessageAck(md, MessageAck.INDIVIDUAL_ACK_TYPE, 1));
1016                                            dequeueCounter.incrementAndGet();
1017                                        }
1018                                    } catch (IOException e) {
1019                                        serviceLocalException(md, e);
1020                                    } finally {
1021                                        sub.decrementOutstandingResponses();
1022                                    }
1023                                }
1024                            });
1025
1026                        } else {
1027                            // If the message was originally sent using async send, we will
1028                            // preserve that QOS by bridging it using an async send (small chance
1029                            // of message loss).
1030                            try {
1031                                remoteBroker.oneway(message);
1032                                localBroker.oneway(new MessageAck(md, MessageAck.INDIVIDUAL_ACK_TYPE, 1));
1033                                dequeueCounter.incrementAndGet();
1034                            } finally {
1035                                sub.decrementOutstandingResponses();
1036                            }
1037                        }
1038                        serviceOutbound(message);
1039                    } else {
1040                        LOG.debug("No subscription registered with this network bridge for consumerId: {} for message: {}", md.getConsumerId(), md.getMessage());
1041                    }
1042                } else if (command.isBrokerInfo()) {
1043                    futureLocalBrokerInfo.set((BrokerInfo) command);
1044                } else if (command.isShutdownInfo()) {
1045                    LOG.info("{} Shutting down {}", configuration.getBrokerName(), configuration.getName());
1046                    stop();
1047                } else if (command.getClass() == ConnectionError.class) {
1048                    ConnectionError ce = (ConnectionError) command;
1049                    serviceLocalException(ce.getException());
1050                } else {
1051                    switch (command.getDataStructureType()) {
1052                        case WireFormatInfo.DATA_STRUCTURE_TYPE:
1053                            break;
1054                        default:
1055                            LOG.warn("Unexpected local command: {}", command);
1056                    }
1057                }
1058            } catch (Throwable e) {
1059                LOG.warn("Caught an exception processing local command", e);
1060                serviceLocalException(e);
1061            }
1062        }
1063    }
1064
1065    private boolean suppressMessageDispatch(MessageDispatch md, DemandSubscription sub) throws Exception {
1066        boolean suppress = false;
1067        // for durable subs, suppression via filter leaves dangling acks so we
1068        // need to check here and allow the ack irrespective
1069        if (sub.getLocalInfo().isDurable()) {
1070            MessageEvaluationContext messageEvalContext = new MessageEvaluationContext();
1071            messageEvalContext.setMessageReference(md.getMessage());
1072            messageEvalContext.setDestination(md.getDestination());
1073            suppress = !sub.getNetworkBridgeFilter().matches(messageEvalContext);
1074        }
1075        return suppress;
1076    }
1077
1078    public static boolean contains(BrokerId[] brokerPath, BrokerId brokerId) {
1079        if (brokerPath != null) {
1080            for (BrokerId id : brokerPath) {
1081                if (brokerId.equals(id)) {
1082                    return true;
1083                }
1084            }
1085        }
1086        return false;
1087    }
1088
1089    protected BrokerId[] appendToBrokerPath(BrokerId[] brokerPath, BrokerId[] pathsToAppend) {
1090        if (brokerPath == null || brokerPath.length == 0) {
1091            return pathsToAppend;
1092        }
1093        BrokerId rc[] = new BrokerId[brokerPath.length + pathsToAppend.length];
1094        System.arraycopy(brokerPath, 0, rc, 0, brokerPath.length);
1095        System.arraycopy(pathsToAppend, 0, rc, brokerPath.length, pathsToAppend.length);
1096        return rc;
1097    }
1098
1099    protected BrokerId[] appendToBrokerPath(BrokerId[] brokerPath, BrokerId idToAppend) {
1100        if (brokerPath == null || brokerPath.length == 0) {
1101            return new BrokerId[]{idToAppend};
1102        }
1103        BrokerId rc[] = new BrokerId[brokerPath.length + 1];
1104        System.arraycopy(brokerPath, 0, rc, 0, brokerPath.length);
1105        rc[brokerPath.length] = idToAppend;
1106        return rc;
1107    }
1108
1109    protected boolean isPermissableDestination(ActiveMQDestination destination) {
1110        return isPermissableDestination(destination, false);
1111    }
1112
1113    protected boolean isPermissableDestination(ActiveMQDestination destination, boolean allowTemporary) {
1114        // Are we not bridging temporary destinations?
1115        if (destination.isTemporary()) {
1116            if (allowTemporary) {
1117                return true;
1118            } else {
1119                return configuration.isBridgeTempDestinations();
1120            }
1121        }
1122
1123        ActiveMQDestination[] dests = staticallyIncludedDestinations;
1124        if (dests != null && dests.length > 0) {
1125            for (ActiveMQDestination dest : dests) {
1126                DestinationFilter inclusionFilter = DestinationFilter.parseFilter(dest);
1127                if (dest != null && inclusionFilter.matches(destination) && dest.getDestinationType() == destination.getDestinationType()) {
1128                    return true;
1129                }
1130            }
1131        }
1132
1133        dests = excludedDestinations;
1134        if (dests != null && dests.length > 0) {
1135            for (ActiveMQDestination dest : dests) {
1136                DestinationFilter exclusionFilter = DestinationFilter.parseFilter(dest);
1137                if (dest != null && exclusionFilter.matches(destination) && dest.getDestinationType() == destination.getDestinationType()) {
1138                    return false;
1139                }
1140            }
1141        }
1142
1143        dests = dynamicallyIncludedDestinations;
1144        if (dests != null && dests.length > 0) {
1145            for (ActiveMQDestination dest : dests) {
1146                DestinationFilter inclusionFilter = DestinationFilter.parseFilter(dest);
1147                if (dest != null && inclusionFilter.matches(destination) && dest.getDestinationType() == destination.getDestinationType()) {
1148                    return true;
1149                }
1150            }
1151
1152            return false;
1153        }
1154        return true;
1155    }
1156
1157    /**
1158     * Subscriptions for these destinations are always created
1159     */
1160    protected void setupStaticDestinations() {
1161        ActiveMQDestination[] dests = staticallyIncludedDestinations;
1162        if (dests != null) {
1163            for (ActiveMQDestination dest : dests) {
1164                DemandSubscription sub = createDemandSubscription(dest);
1165                sub.setStaticallyIncluded(true);
1166                try {
1167                    addSubscription(sub);
1168                } catch (IOException e) {
1169                    LOG.error("Failed to add static destination {}", dest, e);
1170                }
1171                LOG.trace("{}, bridging messages for static destination: {}", configuration.getBrokerName(), dest);
1172            }
1173        }
1174    }
1175
1176    protected void addConsumerInfo(final ConsumerInfo consumerInfo) throws IOException {
1177        ConsumerInfo info = consumerInfo.copy();
1178        addRemoteBrokerToBrokerPath(info);
1179        DemandSubscription sub = createDemandSubscription(info);
1180        if (sub != null) {
1181            if (duplicateSuppressionIsRequired(sub)) {
1182                undoMapRegistration(sub);
1183            } else {
1184                if (consumerInfo.isDurable()) {
1185                    sub.getDurableRemoteSubs().add(new SubscriptionInfo(sub.getRemoteInfo().getClientId(), consumerInfo.getSubscriptionName()));
1186                }
1187                addSubscription(sub);
1188                LOG.debug("{} new demand subscription: {}", configuration.getBrokerName(), sub);
1189            }
1190        }
1191    }
1192
1193    private void undoMapRegistration(DemandSubscription sub) {
1194        subscriptionMapByLocalId.remove(sub.getLocalInfo().getConsumerId());
1195        subscriptionMapByRemoteId.remove(sub.getRemoteInfo().getConsumerId());
1196    }
1197
1198    /*
1199     * check our existing subs networkConsumerIds against the list of network
1200     * ids in this subscription A match means a duplicate which we suppress for
1201     * topics and maybe for queues
1202     */
1203    private boolean duplicateSuppressionIsRequired(DemandSubscription candidate) {
1204        final ConsumerInfo consumerInfo = candidate.getRemoteInfo();
1205        boolean suppress = false;
1206
1207        if (consumerInfo.getDestination().isQueue() && !configuration.isSuppressDuplicateQueueSubscriptions() || consumerInfo.getDestination().isTopic()
1208                && !configuration.isSuppressDuplicateTopicSubscriptions()) {
1209            return suppress;
1210        }
1211
1212        List<ConsumerId> candidateConsumers = consumerInfo.getNetworkConsumerIds();
1213        Collection<Subscription> currentSubs = getRegionSubscriptions(consumerInfo.getDestination());
1214        for (Subscription sub : currentSubs) {
1215            List<ConsumerId> networkConsumers = sub.getConsumerInfo().getNetworkConsumerIds();
1216            if (!networkConsumers.isEmpty()) {
1217                if (matchFound(candidateConsumers, networkConsumers)) {
1218                    if (isInActiveDurableSub(sub)) {
1219                        suppress = false;
1220                    } else {
1221                        suppress = hasLowerPriority(sub, candidate.getLocalInfo());
1222                    }
1223                    break;
1224                }
1225            }
1226        }
1227        return suppress;
1228    }
1229
1230    private boolean isInActiveDurableSub(Subscription sub) {
1231        return (sub.getConsumerInfo().isDurable() && sub instanceof DurableTopicSubscription && !((DurableTopicSubscription) sub).isActive());
1232    }
1233
1234    private boolean hasLowerPriority(Subscription existingSub, ConsumerInfo candidateInfo) {
1235        boolean suppress = false;
1236
1237        if (existingSub.getConsumerInfo().getPriority() >= candidateInfo.getPriority()) {
1238            LOG.debug("{} Ignoring duplicate subscription from {}, sub: {} is duplicate by network subscription with equal or higher network priority: {}, networkConsumerIds: {}", new Object[]{
1239                    configuration.getBrokerName(), remoteBrokerName, candidateInfo, existingSub, existingSub.getConsumerInfo().getNetworkConsumerIds()
1240            });
1241            suppress = true;
1242        } else {
1243            // remove the existing lower priority duplicate and allow this candidate
1244            try {
1245                removeDuplicateSubscription(existingSub);
1246
1247                LOG.debug("{} Replacing duplicate subscription {} with sub from {}, which has a higher priority, new sub: {}, networkConsumerIds: {}", new Object[]{
1248                        configuration.getBrokerName(), existingSub.getConsumerInfo(), remoteBrokerName, candidateInfo, candidateInfo.getNetworkConsumerIds()
1249                });
1250            } catch (IOException e) {
1251                LOG.error("Failed to remove duplicated sub as a result of sub with higher priority, sub: {}", existingSub, e);
1252            }
1253        }
1254        return suppress;
1255    }
1256
1257    private void removeDuplicateSubscription(Subscription existingSub) throws IOException {
1258        for (NetworkConnector connector : brokerService.getNetworkConnectors()) {
1259            if (connector.removeDemandSubscription(existingSub.getConsumerInfo().getConsumerId())) {
1260                break;
1261            }
1262        }
1263    }
1264
1265    private boolean matchFound(List<ConsumerId> candidateConsumers, List<ConsumerId> networkConsumers) {
1266        boolean found = false;
1267        for (ConsumerId aliasConsumer : networkConsumers) {
1268            if (candidateConsumers.contains(aliasConsumer)) {
1269                found = true;
1270                break;
1271            }
1272        }
1273        return found;
1274    }
1275
1276    protected final Collection<Subscription> getRegionSubscriptions(ActiveMQDestination dest) {
1277        RegionBroker region_broker = (RegionBroker) brokerService.getRegionBroker();
1278        Region region;
1279        Collection<Subscription> subs;
1280
1281        region = null;
1282        switch (dest.getDestinationType()) {
1283            case ActiveMQDestination.QUEUE_TYPE:
1284                region = region_broker.getQueueRegion();
1285                break;
1286            case ActiveMQDestination.TOPIC_TYPE:
1287                region = region_broker.getTopicRegion();
1288                break;
1289            case ActiveMQDestination.TEMP_QUEUE_TYPE:
1290                region = region_broker.getTempQueueRegion();
1291                break;
1292            case ActiveMQDestination.TEMP_TOPIC_TYPE:
1293                region = region_broker.getTempTopicRegion();
1294                break;
1295        }
1296
1297        if (region instanceof AbstractRegion) {
1298            subs = ((AbstractRegion) region).getSubscriptions().values();
1299        } else {
1300            subs = null;
1301        }
1302
1303        return subs;
1304    }
1305
1306    protected DemandSubscription createDemandSubscription(ConsumerInfo info) throws IOException {
1307        // add our original id to ourselves
1308        info.addNetworkConsumerId(info.getConsumerId());
1309        return doCreateDemandSubscription(info);
1310    }
1311
1312    protected DemandSubscription doCreateDemandSubscription(ConsumerInfo info) throws IOException {
1313        DemandSubscription result = new DemandSubscription(info);
1314        result.getLocalInfo().setConsumerId(new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId()));
1315        if (info.getDestination().isTemporary()) {
1316            // reset the local connection Id
1317            ActiveMQTempDestination dest = (ActiveMQTempDestination) result.getLocalInfo().getDestination();
1318            dest.setConnectionId(localConnectionInfo.getConnectionId().toString());
1319        }
1320
1321        if (configuration.isDecreaseNetworkConsumerPriority()) {
1322            byte priority = (byte) configuration.getConsumerPriorityBase();
1323            if (info.getBrokerPath() != null && info.getBrokerPath().length > 1) {
1324                // The longer the path to the consumer, the less it's consumer priority.
1325                priority -= info.getBrokerPath().length + 1;
1326            }
1327            result.getLocalInfo().setPriority(priority);
1328            LOG.debug("{} using priority: {} for subscription: {}", new Object[]{configuration.getBrokerName(), priority, info});
1329        }
1330        configureDemandSubscription(info, result);
1331        return result;
1332    }
1333
1334    final protected DemandSubscription createDemandSubscription(ActiveMQDestination destination) {
1335        ConsumerInfo info = new ConsumerInfo();
1336        info.setNetworkSubscription(true);
1337        info.setDestination(destination);
1338
1339        // Indicate that this subscription is being made on behalf of the remote broker.
1340        info.setBrokerPath(new BrokerId[]{remoteBrokerId});
1341
1342        // the remote info held by the DemandSubscription holds the original
1343        // consumerId, the local info get's overwritten
1344        info.setConsumerId(new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId()));
1345        DemandSubscription result = null;
1346        try {
1347            result = createDemandSubscription(info);
1348        } catch (IOException e) {
1349            LOG.error("Failed to create DemandSubscription ", e);
1350        }
1351        return result;
1352    }
1353
1354    protected void configureDemandSubscription(ConsumerInfo info, DemandSubscription sub) throws IOException {
1355        if (AdvisorySupport.isConsumerAdvisoryTopic(info.getDestination())) {
1356            sub.getLocalInfo().setDispatchAsync(true);
1357        } else {
1358            sub.getLocalInfo().setDispatchAsync(configuration.isDispatchAsync());
1359        }
1360        sub.getLocalInfo().setPrefetchSize(configuration.getPrefetchSize());
1361        subscriptionMapByLocalId.put(sub.getLocalInfo().getConsumerId(), sub);
1362        subscriptionMapByRemoteId.put(sub.getRemoteInfo().getConsumerId(), sub);
1363
1364        sub.setNetworkBridgeFilter(createNetworkBridgeFilter(info));
1365        if (!info.isDurable()) {
1366            // This works for now since we use a VM connection to the local broker.
1367            // may need to change if we ever subscribe to a remote broker.
1368            sub.getLocalInfo().setAdditionalPredicate(sub.getNetworkBridgeFilter());
1369        } else {
1370            sub.setLocalDurableSubscriber(new SubscriptionInfo(info.getClientId(), info.getSubscriptionName()));
1371        }
1372    }
1373
1374    protected void removeDemandSubscription(ConsumerId id) throws IOException {
1375        DemandSubscription sub = subscriptionMapByRemoteId.remove(id);
1376        LOG.debug("{} remove request on {} from {}, consumer id: {}, matching sub: {}", new Object[]{
1377                configuration.getBrokerName(), localBroker, remoteBrokerName, id, sub
1378        });
1379        if (sub != null) {
1380            removeSubscription(sub);
1381            LOG.debug("{} removed sub on {} from {}: {}", new Object[]{
1382                    configuration.getBrokerName(), localBroker, remoteBrokerName, sub.getRemoteInfo()
1383            });
1384        }
1385    }
1386
1387    protected boolean removeDemandSubscriptionByLocalId(ConsumerId consumerId) {
1388        boolean removeDone = false;
1389        DemandSubscription sub = subscriptionMapByLocalId.get(consumerId);
1390        if (sub != null) {
1391            try {
1392                removeDemandSubscription(sub.getRemoteInfo().getConsumerId());
1393                removeDone = true;
1394            } catch (IOException e) {
1395                LOG.debug("removeDemandSubscriptionByLocalId failed for localId: {}", consumerId, e);
1396            }
1397        }
1398        return removeDone;
1399    }
1400
1401    /**
1402     * Performs a timed wait on the started latch and then checks for disposed
1403     * before performing another wait each time the the started wait times out.
1404     */
1405    protected boolean safeWaitUntilStarted() throws InterruptedException {
1406        while (!disposed.get()) {
1407            if (startedLatch.await(1, TimeUnit.SECONDS)) {
1408                break;
1409            }
1410        }
1411        return !disposed.get();
1412    }
1413
1414    protected NetworkBridgeFilter createNetworkBridgeFilter(ConsumerInfo info) throws IOException {
1415        NetworkBridgeFilterFactory filterFactory = defaultFilterFactory;
1416        if (brokerService != null && brokerService.getDestinationPolicy() != null) {
1417            PolicyEntry entry = brokerService.getDestinationPolicy().getEntryFor(info.getDestination());
1418            if (entry != null && entry.getNetworkBridgeFilterFactory() != null) {
1419                filterFactory = entry.getNetworkBridgeFilterFactory();
1420            }
1421        }
1422        return filterFactory.create(info, getRemoteBrokerPath(), configuration.getMessageTTL(), configuration.getConsumerTTL());
1423    }
1424
1425    protected void addRemoteBrokerToBrokerPath(ConsumerInfo info) throws IOException {
1426        info.setBrokerPath(appendToBrokerPath(info.getBrokerPath(), getRemoteBrokerPath()));
1427    }
1428
1429    protected BrokerId[] getRemoteBrokerPath() {
1430        return remoteBrokerPath;
1431    }
1432
1433    @Override
1434    public void setNetworkBridgeListener(NetworkBridgeListener listener) {
1435        this.networkBridgeListener = listener;
1436    }
1437
1438    private void fireBridgeFailed(Throwable reason) {
1439        LOG.trace("fire bridge failed, listener: {}", this.networkBridgeListener, reason);
1440        NetworkBridgeListener l = this.networkBridgeListener;
1441        if (l != null && this.bridgeFailed.compareAndSet(false, true)) {
1442            l.bridgeFailed();
1443        }
1444    }
1445
1446    /**
1447     * @return Returns the dynamicallyIncludedDestinations.
1448     */
1449    public ActiveMQDestination[] getDynamicallyIncludedDestinations() {
1450        return dynamicallyIncludedDestinations;
1451    }
1452
1453    /**
1454     * @param dynamicallyIncludedDestinations
1455     *         The dynamicallyIncludedDestinations to set.
1456     */
1457    public void setDynamicallyIncludedDestinations(ActiveMQDestination[] dynamicallyIncludedDestinations) {
1458        this.dynamicallyIncludedDestinations = dynamicallyIncludedDestinations;
1459    }
1460
1461    /**
1462     * @return Returns the excludedDestinations.
1463     */
1464    public ActiveMQDestination[] getExcludedDestinations() {
1465        return excludedDestinations;
1466    }
1467
1468    /**
1469     * @param excludedDestinations The excludedDestinations to set.
1470     */
1471    public void setExcludedDestinations(ActiveMQDestination[] excludedDestinations) {
1472        this.excludedDestinations = excludedDestinations;
1473    }
1474
1475    /**
1476     * @return Returns the staticallyIncludedDestinations.
1477     */
1478    public ActiveMQDestination[] getStaticallyIncludedDestinations() {
1479        return staticallyIncludedDestinations;
1480    }
1481
1482    /**
1483     * @param staticallyIncludedDestinations The staticallyIncludedDestinations to set.
1484     */
1485    public void setStaticallyIncludedDestinations(ActiveMQDestination[] staticallyIncludedDestinations) {
1486        this.staticallyIncludedDestinations = staticallyIncludedDestinations;
1487    }
1488
1489    /**
1490     * @return Returns the durableDestinations.
1491     */
1492    public ActiveMQDestination[] getDurableDestinations() {
1493        return durableDestinations;
1494    }
1495
1496    /**
1497     * @param durableDestinations The durableDestinations to set.
1498     */
1499    public void setDurableDestinations(ActiveMQDestination[] durableDestinations) {
1500        this.durableDestinations = durableDestinations;
1501    }
1502
1503    /**
1504     * @return Returns the localBroker.
1505     */
1506    public Transport getLocalBroker() {
1507        return localBroker;
1508    }
1509
1510    /**
1511     * @return Returns the remoteBroker.
1512     */
1513    public Transport getRemoteBroker() {
1514        return remoteBroker;
1515    }
1516
1517    /**
1518     * @return the createdByDuplex
1519     */
1520    public boolean isCreatedByDuplex() {
1521        return this.createdByDuplex;
1522    }
1523
1524    /**
1525     * @param createdByDuplex the createdByDuplex to set
1526     */
1527    public void setCreatedByDuplex(boolean createdByDuplex) {
1528        this.createdByDuplex = createdByDuplex;
1529    }
1530
1531    @Override
1532    public String getRemoteAddress() {
1533        return remoteBroker.getRemoteAddress();
1534    }
1535
1536    @Override
1537    public String getLocalAddress() {
1538        return localBroker.getRemoteAddress();
1539    }
1540
1541    @Override
1542    public String getRemoteBrokerName() {
1543        return remoteBrokerInfo == null ? null : remoteBrokerInfo.getBrokerName();
1544    }
1545
1546    @Override
1547    public String getRemoteBrokerId() {
1548        return (remoteBrokerInfo == null || remoteBrokerInfo.getBrokerId() == null) ? null : remoteBrokerInfo.getBrokerId().toString();
1549    }
1550
1551    @Override
1552    public String getLocalBrokerName() {
1553        return localBrokerInfo == null ? null : localBrokerInfo.getBrokerName();
1554    }
1555
1556    @Override
1557    public long getDequeueCounter() {
1558        return dequeueCounter.get();
1559    }
1560
1561    @Override
1562    public long getEnqueueCounter() {
1563        return enqueueCounter.get();
1564    }
1565
1566    protected boolean isDuplex() {
1567        return configuration.isDuplex() || createdByDuplex;
1568    }
1569
1570    public ConcurrentMap<ConsumerId, DemandSubscription> getLocalSubscriptionMap() {
1571        return subscriptionMapByRemoteId;
1572    }
1573
1574    @Override
1575    public void setBrokerService(BrokerService brokerService) {
1576        this.brokerService = brokerService;
1577        this.localBrokerId = brokerService.getRegionBroker().getBrokerId();
1578        localBrokerPath[0] = localBrokerId;
1579    }
1580
1581    @Override
1582    public void setMbeanObjectName(ObjectName objectName) {
1583        this.mbeanObjectName = objectName;
1584    }
1585
1586    @Override
1587    public ObjectName getMbeanObjectName() {
1588        return mbeanObjectName;
1589    }
1590
1591    @Override
1592    public void resetStats() {
1593        enqueueCounter.set(0);
1594        dequeueCounter.set(0);
1595    }
1596
1597    /*
1598     * Used to allow for async tasks to await receipt of the BrokerInfo from the local and
1599     * remote sides of the network bridge.
1600     */
1601    private static class FutureBrokerInfo implements Future<BrokerInfo> {
1602
1603        private final CountDownLatch slot = new CountDownLatch(1);
1604        private final AtomicBoolean disposed;
1605        private volatile BrokerInfo info = null;
1606
1607        public FutureBrokerInfo(BrokerInfo info, AtomicBoolean disposed) {
1608            this.info = info;
1609            this.disposed = disposed;
1610        }
1611
1612        @Override
1613        public boolean cancel(boolean mayInterruptIfRunning) {
1614            slot.countDown();
1615            return true;
1616        }
1617
1618        @Override
1619        public boolean isCancelled() {
1620            return slot.getCount() == 0 && info == null;
1621        }
1622
1623        @Override
1624        public boolean isDone() {
1625            return info != null;
1626        }
1627
1628        @Override
1629        public BrokerInfo get() throws InterruptedException, ExecutionException {
1630            try {
1631                if (info == null) {
1632                    while (!disposed.get()) {
1633                        if (slot.await(1, TimeUnit.SECONDS)) {
1634                            break;
1635                        }
1636                    }
1637                }
1638                return info;
1639            } catch (InterruptedException e) {
1640                Thread.currentThread().interrupt();
1641                LOG.debug("Operation interrupted: {}", e, e);
1642                throw new InterruptedException("Interrupted.");
1643            }
1644        }
1645
1646        @Override
1647        public BrokerInfo get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
1648            try {
1649                if (info == null) {
1650                    long deadline = System.currentTimeMillis() + unit.toMillis(timeout);
1651
1652                    while (!disposed.get() || System.currentTimeMillis() < deadline) {
1653                        if (slot.await(1, TimeUnit.MILLISECONDS)) {
1654                            break;
1655                        }
1656                    }
1657                    if (info == null) {
1658                        throw new TimeoutException();
1659                    }
1660                }
1661                return info;
1662            } catch (InterruptedException e) {
1663                throw new InterruptedException("Interrupted.");
1664            }
1665        }
1666
1667        public void set(BrokerInfo info) {
1668            this.info = info;
1669            this.slot.countDown();
1670        }
1671    }
1672
1673    protected void serviceOutbound(Message message) {
1674        NetworkBridgeListener l = this.networkBridgeListener;
1675        if (l != null) {
1676            l.onOutboundMessage(this, message);
1677        }
1678    }
1679
1680    protected void serviceInboundMessage(Message message) {
1681        NetworkBridgeListener l = this.networkBridgeListener;
1682        if (l != null) {
1683            l.onInboundMessage(this, message);
1684        }
1685    }
1686
1687    protected boolean canDuplexDispatch(Message message) {
1688        boolean result = true;
1689        if (configuration.isCheckDuplicateMessagesOnDuplex()){
1690            final long producerSequenceId = message.getMessageId().getProducerSequenceId();
1691            //  messages are multiplexed on this producer so we need to query the persistenceAdapter
1692            long lastStoredForMessageProducer = getStoredSequenceIdForMessage(message.getMessageId());
1693            if (producerSequenceId <= lastStoredForMessageProducer) {
1694                result = false;
1695                LOG.debug("suppressing duplicate message send [{}] from network producer with producerSequence [{}] less than last stored: {}", new Object[]{
1696                        (LOG.isTraceEnabled() ? message : message.getMessageId()), producerSequenceId, lastStoredForMessageProducer
1697                });
1698            }
1699        }
1700        return result;
1701    }
1702
1703    protected long getStoredSequenceIdForMessage(MessageId messageId) {
1704        try {
1705            return brokerService.getPersistenceAdapter().getLastProducerSequenceId(messageId.getProducerId());
1706        } catch (IOException ignored) {
1707            LOG.debug("Failed to determine last producer sequence id for: {}", messageId, ignored);
1708        }
1709        return -1;
1710    }
1711
1712}