public void destroyBridge(final String name) throws Exception {
    Bridge bridge;

    synchronized (this) {
      bridge = bridges.remove(name);
      if (bridge != null) {
        bridge.stop();
        managementService.unregisterBridge(name);
      }
    }
    if (bridge != null) {
      bridge.flushExecutor();
    }
  }
 // for testing
 public void clear() {
   for (Bridge bridge : bridges.values()) {
     try {
       bridge.stop();
     } catch (Exception e) {
       log.warn(e.getMessage(), e);
     }
   }
   bridges.clear();
   for (ClusterConnection clusterConnection : clusterConnections.values()) {
     try {
       clusterConnection.stop();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   clearClusterConnections();
 }
  public void stop() throws Exception {
    synchronized (this) {
      if (!started) {
        return;
      }

      if (clustered) {
        for (BroadcastGroup group : broadcastGroups.values()) {
          group.stop();
          managementService.unregisterBroadcastGroup(group.getName());
        }

        broadcastGroups.clear();

        for (ClusterConnection clusterConnection : clusterConnections.values()) {
          clusterConnection.stop();
          managementService.unregisterCluster(clusterConnection.getName().toString());
        }
      }

      for (Bridge bridge : bridges.values()) {
        bridge.stop();
        managementService.unregisterBridge(bridge.getName().toString());
      }

      bridges.clear();
    }

    for (ServerLocatorInternal clusterLocator : clusterLocators) {
      try {
        clusterLocator.close();
      } catch (Exception e) {
        log.warn(
            "Error closing serverLocator=" + clusterLocator + ", message=" + e.getMessage(), e);
      }
    }
    clusterLocators.clear();
    started = false;

    clearClusterConnections();
  }
  @Test
  public void testStartStop() throws Exception {
    Map<String, Object> server0Params = new HashMap<String, Object>();
    HornetQServer server0 = createClusteredServerWithParams(isNetty(), 0, true, server0Params);

    Map<String, Object> server1Params = new HashMap<String, Object>();
    if (isNetty()) {
      server1Params.put(
          "port", org.hornetq.core.remoting.impl.netty.TransportConstants.DEFAULT_PORT + 1);
    } else {
      server1Params.put(TransportConstants.SERVER_ID_PROP_NAME, 1);
    }
    HornetQServer server1 = createClusteredServerWithParams(isNetty(), 1, true, server1Params);
    ServerLocator locator = null;
    try {
      final String testAddress = "testAddress";
      final String queueName0 = "queue0";
      final String forwardAddress = "forwardAddress";
      final String queueName1 = "queue1";

      Map<String, TransportConfiguration> connectors =
          new HashMap<String, TransportConfiguration>();
      TransportConfiguration server0tc = new TransportConfiguration(getConnector(), server0Params);
      TransportConfiguration server1tc = new TransportConfiguration(getConnector(), server1Params);
      connectors.put(server1tc.getName(), server1tc);

      server0.getConfiguration().setConnectorConfigurations(connectors);

      ArrayList<String> staticConnectors = new ArrayList<String>();
      staticConnectors.add(server1tc.getName());

      final String bridgeName = "bridge1";

      BridgeConfiguration bridgeConfiguration =
          new BridgeConfiguration()
              .setName(bridgeName)
              .setQueueName(queueName0)
              .setForwardingAddress(forwardAddress)
              .setRetryInterval(1000)
              .setReconnectAttempts(0)
              .setReconnectAttemptsOnSameNode(0)
              .setConfirmationWindowSize(1024)
              .setStaticConnectors(staticConnectors);

      List<BridgeConfiguration> bridgeConfigs = new ArrayList<BridgeConfiguration>();
      bridgeConfigs.add(bridgeConfiguration);
      server0.getConfiguration().setBridgeConfigurations(bridgeConfigs);

      CoreQueueConfiguration queueConfig0 =
          new CoreQueueConfiguration().setAddress(testAddress).setName(queueName0);
      List<CoreQueueConfiguration> queueConfigs0 = new ArrayList<CoreQueueConfiguration>();
      queueConfigs0.add(queueConfig0);
      server0.getConfiguration().setQueueConfigurations(queueConfigs0);

      CoreQueueConfiguration queueConfig1 =
          new CoreQueueConfiguration().setAddress(forwardAddress).setName(queueName1);
      List<CoreQueueConfiguration> queueConfigs1 = new ArrayList<CoreQueueConfiguration>();
      queueConfigs1.add(queueConfig1);
      server1.getConfiguration().setQueueConfigurations(queueConfigs1);

      server1.start();
      waitForServer(server1);

      server0.start();
      waitForServer(server0);

      locator = HornetQClient.createServerLocatorWithoutHA(server0tc, server1tc);
      ClientSessionFactory sf0 = locator.createSessionFactory(server0tc);

      ClientSessionFactory sf1 = locator.createSessionFactory(server1tc);

      ClientSession session0 = sf0.createSession(false, true, true);

      ClientSession session1 = sf1.createSession(false, true, true);

      ClientProducer producer0 = session0.createProducer(new SimpleString(testAddress));

      ClientConsumer consumer1 = session1.createConsumer(queueName1);

      session1.start();

      final int numMessages = 10;

      final SimpleString propKey = new SimpleString("testkey");

      for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);

        message.putIntProperty(propKey, i);

        producer0.send(message);
      }

      for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer1.receive(200);

        Assert.assertNotNull(message);

        Assert.assertEquals(i, message.getObjectProperty(propKey));

        message.acknowledge();
      }

      Assert.assertNull(consumer1.receiveImmediate());

      Bridge bridge = server0.getClusterManager().getBridges().get(bridgeName);

      bridge.stop();

      bridge.flushExecutor();

      for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session0.createMessage(false);

        message.putIntProperty(propKey, i);

        producer0.send(message);
      }

      Assert.assertNull(consumer1.receiveImmediate());

      bridge.start();

      for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer1.receive(1000);

        Assert.assertNotNull(message);

        Assert.assertEquals(i, message.getObjectProperty(propKey));

        message.acknowledge();
      }

      Assert.assertNull(consumer1.receiveImmediate());

      session0.close();

      session1.close();

      sf0.close();

      sf1.close();
    } finally {
      if (locator != null) {
        locator.close();
      }

      server0.stop();

      server1.stop();
    }
  }