示例#1
0
  public void testSimpleBroadcastJGropus() throws Exception {
    final String nodeID = RandomUtil.randomString();

    bg =
        new BroadcastGroupImpl(
            new FakeNodeManager(nodeID),
            "broadcast",
            100,
            null,
            new JGroupsBroadcastGroupConfiguration("test-jgroups-file_ping.xml", "tst")
                .createBroadcastEndpointFactory());

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        new DiscoveryGroup(
            nodeID + "1",
            "broadcast",
            5000l,
            new JGroupsBroadcastGroupConfiguration("test-jgroups-file_ping.xml", "tst")
                .createBroadcastEndpointFactory(),
            null);

    dg.start();

    verifyBroadcast(bg, dg);
    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
  }
示例#2
0
  public void testIgnoreTrafficFromOwnNode() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String nodeID = RandomUtil.randomString();

    bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        newDiscoveryGroup(
            nodeID, RandomUtil.randomString(), null, groupAddress, groupPort, timeout);

    dg.start();

    verifyNonBroadcast(bg, dg);
    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();

    Assert.assertNotNull(entries);

    Assert.assertEquals(0, entries.size());
  }
示例#3
0
  public void testDiscoveryListenersCalled() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String nodeID = RandomUtil.randomString();

    bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    MyListener listener1 = new MyListener();
    MyListener listener2 = new MyListener();
    MyListener listener3 = new MyListener();

    dg.registerListener(listener1);
    dg.registerListener(listener2);
    dg.registerListener(listener3);

    dg.start();

    verifyBroadcast(bg, dg);

    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    Assert.assertTrue(listener3.called);

    listener1.called = false;
    listener2.called = false;
    listener3.called = false;

    verifyBroadcast(bg, dg);

    // Won't be called since connectors haven't changed
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    Assert.assertFalse(listener3.called);
  }
示例#4
0
  public void testSimpleBroadcastDifferentPort() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(getUDPDiscoveryAddress());
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    bg =
        newBroadcast(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            -1,
            groupAddress,
            groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    final int port2 = getUDPDiscoveryPort(1);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            port2,
            timeout);

    dg.start();

    verifyNonBroadcast(bg, dg);
  }
  private synchronized void initialise() throws HornetQException {
    if (readOnly) {
      return;
    }

    try {
      state = STATE.INITIALIZED;
      setThreadPools();

      instantiateLoadBalancingPolicy();

      if (discoveryGroupConfiguration != null) {
        InetAddress groupAddress =
            InetAddress.getByName(discoveryGroupConfiguration.getGroupAddress());

        InetAddress lbAddress;

        if (discoveryGroupConfiguration.getLocalBindAddress() != null) {
          lbAddress = InetAddress.getByName(discoveryGroupConfiguration.getLocalBindAddress());
        } else {
          lbAddress = null;
        }

        discoveryGroup =
            new DiscoveryGroupImpl(
                nodeID,
                discoveryGroupConfiguration.getName(),
                lbAddress,
                groupAddress,
                discoveryGroupConfiguration.getGroupPort(),
                discoveryGroupConfiguration.getRefreshTimeout());

        discoveryGroup.registerListener(this);

        discoveryGroup.start();
      }

      readOnly = true;
    } catch (Exception e) {
      state = null;
      throw HornetQMessageBundle.BUNDLE.failedToInitialiseSessionFactory(e);
    }
  }
示例#6
0
  public void testSimpleBroadcastWithStopStartDiscoveryGroup() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    final String nodeID = RandomUtil.randomString();

    bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg.start();

    verifyBroadcast(bg, dg);
    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    bg.stop();

    dg.stop();

    dg.start();

    bg.start();

    verifyBroadcast(bg, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
  }
示例#7
0
  public void testDiscoveryGroupNotifications() throws Exception {
    SimpleNotificationService notifService = new SimpleNotificationService();
    SimpleNotificationService.Listener notifListener = new SimpleNotificationService.Listener();
    notifService.addNotificationListener(notifListener);

    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout,
            notifService);

    Assert.assertEquals(0, notifListener.getNotifications().size());

    dg.start();

    Assert.assertEquals(1, notifListener.getNotifications().size());
    Notification notif = notifListener.getNotifications().get(0);
    Assert.assertEquals(NotificationType.DISCOVERY_GROUP_STARTED, notif.getType());
    Assert.assertEquals(
        dg.getName(),
        notif.getProperties().getSimpleStringProperty(new SimpleString("name")).toString());

    dg.stop();

    Assert.assertEquals(2, notifListener.getNotifications().size());
    notif = notifListener.getNotifications().get(1);
    Assert.assertEquals(NotificationType.DISCOVERY_GROUP_STOPPED, notif.getType());
    Assert.assertEquals(
        dg.getName(),
        notif.getProperties().getSimpleStringProperty(new SimpleString("name")).toString());
  }
示例#8
0
  public void testSimpleBroadcast() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    final String nodeID = RandomUtil.randomString();

    bg =
        new BroadcastGroupImpl(
            new FakeNodeManager(nodeID),
            RandomUtil.randomString(),
            0,
            null,
            new UDPBroadcastGroupConfiguration(address1, groupPort, null, -1)
                .createBroadcastEndpointFactory());

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg.start();

    verifyBroadcast(bg, dg);

    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
  }
  public synchronized void connectorsChanged() {
    List<DiscoveryEntry> newConnectors = discoveryGroup.getDiscoveryEntries();

    TransportConfiguration[] newInitialconnectors =
        (TransportConfiguration[])
            Array.newInstance(TransportConfiguration.class, newConnectors.size());

    int count = 0;
    for (DiscoveryEntry entry : newConnectors) {
      newInitialconnectors[count++] = entry.getConnector();

      if (ha && topology.getMember(entry.getNodeID()) == null) {
        TopologyMember member = new TopologyMember(entry.getConnector(), null);
        // on this case we set it as zero as any update coming from server should be accepted
        topology.updateMember(0, entry.getNodeID(), member);
      }
    }

    this.initialConnectors = newInitialconnectors;

    if (clusterConnection && !receivedTopology && initialConnectors.length > 0) {
      // The node is alone in the cluster. We create a connection to the new node
      // to trigger the node notification to form the cluster.

      Runnable connectRunnable =
          new Runnable() {
            public void run() {
              try {
                connect();
              } catch (HornetQException e) {
                HornetQLogger.LOGGER.errorConnectingToNodes(e);
              }
            }
          };
      if (startExecutor != null) {
        startExecutor.execute(connectRunnable);
      } else {
        connectRunnable.run();
      }
    }
  }
示例#10
0
  public void testMultipleDiscoveryGroups() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String nodeID = RandomUtil.randomString();

    bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg1 =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg2 =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg3 =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg1.start();
    dg2.start();
    dg3.start();

    bg.broadcastConnectors();

    boolean ok = dg1.waitForBroadcast(1000);
    Assert.assertTrue(ok);
    List<DiscoveryEntry> entries = dg1.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    ok = dg2.waitForBroadcast(1000);
    Assert.assertTrue(ok);
    entries = dg2.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    ok = dg3.waitForBroadcast(1000);
    Assert.assertTrue(ok);
    entries = dg3.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    bg.stop();

    dg1.stop();
    dg2.stop();
    dg3.stop();
  }
示例#11
0
  public void testConnectorsUpdatedMultipleBroadcasters() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String node1 = RandomUtil.randomString();
    String node2 = RandomUtil.randomString();
    String node3 = RandomUtil.randomString();

    bg1 = newBroadcast(node1, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg1.start();

    bg2 = newBroadcast(node2, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg2.start();

    bg3 = newBroadcast(node3, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg3.start();

    TransportConfiguration live1 = generateTC();
    bg1.addConnector(live1);

    TransportConfiguration live2 = generateTC();
    bg2.addConnector(live2);

    TransportConfiguration live3 = generateTC();
    bg3.addConnector(live3);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    MyListener listener1 = new MyListener();
    dg.registerListener(listener1);
    MyListener listener2 = new MyListener();
    dg.registerListener(listener2);

    dg.start();

    verifyBroadcast(bg1, dg);
    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg2, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg3, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg1, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg2, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg3, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    bg2.removeConnector(live2);
    verifyBroadcast(bg2, dg);

    // Connector2 should still be there since not timed out yet

    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    Thread.sleep(timeout * 2);

    bg1.broadcastConnectors();
    boolean ok = dg.waitForBroadcast(1000);
    bg2.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg3.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);

    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live3), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    bg1.removeConnector(live1);
    bg3.removeConnector(live3);

    Thread.sleep(timeout * 2);

    bg1.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg2.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg3.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);

    entries = dg.getDiscoveryEntries();
    Assert.assertNotNull(entries);
    Assert.assertEquals(0, entries.size());
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    bg1.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg2.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg3.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);

    entries = dg.getDiscoveryEntries();
    Assert.assertNotNull(entries);
    Assert.assertEquals(0, entries.size());
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
  }
示例#12
0
 /**
  * @param discoveryGroup
  * @throws Exception
  */
 private static void verifyNonBroadcast(
     BroadcastGroup broadcastGroup, DiscoveryGroup discoveryGroup) throws Exception {
   broadcastGroup.broadcastConnectors();
   Assert.assertFalse("NO broadcast received", discoveryGroup.waitForBroadcast(2000));
 }
示例#13
0
  public void testMultipleGroups() throws Exception {
    final int groupPort1 = getUDPDiscoveryPort();

    final int groupPort2 = getUDPDiscoveryPort(1);

    final int groupPort3 = getUDPDiscoveryPort(2);

    final InetAddress groupAddress1 = InetAddress.getByName(address1);

    final InetAddress groupAddress2 = InetAddress.getByName(address2);

    final InetAddress groupAddress3 = InetAddress.getByName(address3);

    final int timeout = 5000;

    String node1 = UUIDGenerator.getInstance().generateStringUUID();

    String node2 = UUIDGenerator.getInstance().generateStringUUID();

    String node3 = UUIDGenerator.getInstance().generateStringUUID();

    bg1 = newBroadcast(node1, RandomUtil.randomString(), null, -1, groupAddress1, groupPort1);

    bg2 = newBroadcast(node2, RandomUtil.randomString(), null, -1, groupAddress2, groupPort2);

    bg3 = newBroadcast(node3, RandomUtil.randomString(), null, -1, groupAddress3, groupPort3);

    bg2.start();
    bg1.start();
    bg3.start();

    TransportConfiguration live1 = generateTC("live1");

    TransportConfiguration live2 = generateTC("live2");

    TransportConfiguration live3 = generateTC("live3");

    bg1.addConnector(live1);
    bg2.addConnector(live2);
    bg3.addConnector(live3);

    dg1 =
        newDiscoveryGroup(
            "group-1::" + RandomUtil.randomString(),
            "group-1::" + RandomUtil.randomString(),
            null,
            groupAddress1,
            groupPort1,
            timeout);
    dg1.start();

    dg2 =
        newDiscoveryGroup(
            "group-2::" + RandomUtil.randomString(),
            "group-2::" + RandomUtil.randomString(),
            null,
            groupAddress2,
            groupPort2,
            timeout);
    dg2.start();

    dg3 =
        newDiscoveryGroup(
            "group-3::" + RandomUtil.randomString(),
            "group-3::" + RandomUtil.randomString(),
            null,
            groupAddress3,
            groupPort3,
            timeout);
    dg3.start();

    bg1.broadcastConnectors();

    bg2.broadcastConnectors();

    bg3.broadcastConnectors();

    boolean ok = dg1.waitForBroadcast(timeout);
    Assert.assertTrue(ok);
    List<DiscoveryEntry> entries = dg1.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    ok = dg2.waitForBroadcast(timeout);
    Assert.assertTrue(ok);
    entries = dg2.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live2), entries);

    ok = dg3.waitForBroadcast(timeout);
    Assert.assertTrue(ok);
    entries = dg3.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live3), entries);
  }
示例#14
0
  public void testSimpleBroadcastSpecificNIC() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    final String nodeID = RandomUtil.randomString();

    // We need to choose a real NIC on the local machine - note this will silently pass if the
    // machine
    // has no usable NIC!

    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

    InetAddress localAddress = null;

    outer:
    while (networkInterfaces.hasMoreElements()) {
      NetworkInterface networkInterface = networkInterfaces.nextElement();
      if (networkInterface.isLoopback()
          || networkInterface.isVirtual()
          || !networkInterface.isUp()
          || !networkInterface.supportsMulticast()) {
        continue;
      }

      Enumeration<InetAddress> en = networkInterface.getInetAddresses();

      while (en.hasMoreElements()) {
        InetAddress ia = en.nextElement();

        if (ia.getAddress().length == 4) {
          localAddress = ia;

          break outer;
        }
      }
    }

    if (localAddress == null) {
      log.warn("Can't find address to use");

      return;
    }

    log.info("Local address is " + localAddress);

    bg =
        newBroadcast(
            nodeID, RandomUtil.randomString(), localAddress, 6552, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            localAddress,
            groupAddress,
            groupPort,
            timeout);

    dg.start();

    verifyBroadcast(bg, dg);

    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
  }
示例#15
0
  public ClientSessionFactory createSessionFactory() throws HornetQException {
    assertOpen();

    initialise();

    if (initialConnectors == null && discoveryGroup != null) {
      // Wait for an initial broadcast to give us at least one node in the cluster
      long timeout =
          clusterConnection ? 0 : discoveryGroupConfiguration.getDiscoveryInitialWaitTimeout();
      boolean ok = discoveryGroup.waitForBroadcast(timeout);

      if (!ok) {
        throw HornetQMessageBundle.BUNDLE.connectionTimedOutInInitialBroadcast();
      }
    }

    ClientSessionFactoryInternal factory = null;

    synchronized (this) {
      boolean retry;
      int attempts = 0;
      do {
        retry = false;

        TransportConfiguration tc = selectConnector();
        if (tc == null) {
          throw HornetQMessageBundle.BUNDLE.noTCForSessionFactory();
        }

        // try each factory in the list until we find one which works

        try {
          factory =
              new ClientSessionFactoryImpl(
                  this,
                  tc,
                  callTimeout,
                  callFailoverTimeout,
                  clientFailureCheckPeriod,
                  connectionTTL,
                  retryInterval,
                  retryIntervalMultiplier,
                  maxRetryInterval,
                  reconnectAttempts,
                  threadPool,
                  scheduledThreadPool,
                  interceptors);
          try {
            addToConnecting(factory);
            factory.connect(initialConnectAttempts, failoverOnInitialConnection);
          } finally {
            removeFromConnecting(factory);
          }
        } catch (HornetQException e) {
          factory.close();
          factory = null;
          if (e.getType() == HornetQExceptionType.NOT_CONNECTED) {
            attempts++;

            if (topologyArray != null && attempts == topologyArray.length) {
              throw HornetQMessageBundle.BUNDLE.cannotConnectToServers();
            }
            if (topologyArray == null
                && initialConnectors != null
                && attempts == initialConnectors.length) {
              throw HornetQMessageBundle.BUNDLE.cannotConnectToServers();
            }
            retry = true;
          } else {
            throw e;
          }
        }
      } while (retry);

      if (ha || clusterConnection) {
        final long timeout = System.currentTimeMillis() + 30000;
        while (!isClosed() && !receivedTopology && timeout > System.currentTimeMillis()) {
          // Now wait for the topology

          try {
            wait(1000);
          } catch (InterruptedException ignore) {
          }
        }

        if (System.currentTimeMillis() > timeout && !receivedTopology) {
          throw HornetQMessageBundle.BUNDLE.connectionTimedOutOnReceiveTopology(discoveryGroup);
        }
      }

      addFactory(factory);

      return factory;
    }
  }
示例#16
0
  private void doClose(final boolean sendClose) {
    if (state == STATE.CLOSED) {
      if (HornetQLogger.LOGGER.isDebugEnabled()) {
        HornetQLogger.LOGGER.debug(this + " is already closed when calling closed");
      }
      return;
    }

    state = STATE.CLOSING;

    if (discoveryGroup != null) {
      synchronized (this) {
        try {
          discoveryGroup.stop();
        } catch (Exception e) {
          HornetQLogger.LOGGER.failedToStopDiscovery(e);
        }
      }
    } else {
      staticConnector.disconnect();
    }

    synchronized (connectingFactories) {
      for (ClientSessionFactoryInternal csf : connectingFactories) {
        csf.close();
      }
      connectingFactories.clear();
    }

    Set<ClientSessionFactoryInternal> clonedFactory;
    synchronized (factories) {
      clonedFactory = new HashSet<ClientSessionFactoryInternal>(factories);

      factories.clear();
    }

    for (ClientSessionFactory factory : clonedFactory) {
      if (sendClose) {
        factory.close();
      } else {
        factory.cleanup();
      }
    }

    if (shutdownPool) {
      if (threadPool != null) {
        threadPool.shutdown();

        try {
          if (!threadPool.awaitTermination(10000, TimeUnit.MILLISECONDS)) {
            HornetQLogger.LOGGER.timedOutWaitingForTermination();
          }
        } catch (InterruptedException ignore) {
        }
      }

      if (scheduledThreadPool != null) {
        scheduledThreadPool.shutdown();

        try {
          if (!scheduledThreadPool.awaitTermination(10000, TimeUnit.MILLISECONDS)) {
            HornetQLogger.LOGGER.timedOutWaitingForScheduledPoolTermination();
          }
        } catch (InterruptedException ignore) {
        }
      }
    }
    readOnly = false;

    state = STATE.CLOSED;
  }