public ConnectionEntry createConnectionEntry(
      final Acceptor acceptorUsed, final Connection connection) {
    final Configuration config = server.getConfiguration();

    Executor connectionExecutor = server.getExecutorFactory().getExecutor();

    final CoreRemotingConnection rc =
        new RemotingConnectionImpl(
            connection,
            interceptors,
            config.isAsyncConnectionExecutionEnabled() ? connectionExecutor : null,
            server.getNodeID());

    Channel channel1 = rc.getChannel(CHANNEL_ID.SESSION.id, -1);

    ChannelHandler handler = new HornetQPacketHandler(this, server, channel1, rc);

    channel1.setHandler(handler);

    long ttl = HornetQClient.DEFAULT_CONNECTION_TTL;

    if (config.getConnectionTTLOverride() != -1) {
      ttl = config.getConnectionTTLOverride();
    }

    final ConnectionEntry entry =
        new ConnectionEntry(rc, connectionExecutor, System.currentTimeMillis(), ttl);

    final Channel channel0 = rc.getChannel(0, -1);

    channel0.setHandler(new LocalChannelHandler(config, entry, channel0, acceptorUsed, rc));

    return entry;
  }
  public void testSinglePersistentBlockingNonSync() throws Exception {
    HornetQServer server = createServer(true);
    ClientSession session = null;
    ClientSessionFactory factory = null;

    ServerLocator locator = null;

    server.getConfiguration().setJournalSyncNonTransactional(false);
    server.getConfiguration().setJournalBufferTimeout_AIO(15000);

    server.start();

    System.out.println("sync = " + server.getConfiguration().isJournalSyncNonTransactional());
    locator = createFactory(false);
    locator.setBlockOnDurableSend(true);
    factory = locator.createSessionFactory();

    session = factory.createSession();

    session.createQueue("address", "queue");

    ClientProducer prod = session.createProducer("address");

    ClientMessage message = session.createMessage(true);

    prod.send(message);

    ClientConsumer consumer = session.createConsumer("queue");

    session.start();

    ClientMessage msg = consumer.receive(5000);

    Assert.assertNotNull(msg);

    msg.acknowledge();
  }
Exemple #3
0
  /**
   * @param connectorConfigs
   * @return
   */
  protected ArrayList<String> registerConnectors(
      final HornetQServer server, final List<TransportConfiguration> connectorConfigs) {
    // The connectors need to be pre-configured at main config object but this method is taking
    // TransportConfigurations directly
    // So this will first register them at the config and then generate a list of objects
    ArrayList<String> connectors = new ArrayList<String>();
    for (TransportConfiguration tnsp : connectorConfigs) {
      String name = RandomUtil.randomString();

      server.getConfiguration().getConnectorConfigurations().put(name, tnsp);

      connectors.add(name);
    }
    return connectors;
  }
  private void deployClusterConnection(final ClusterConnectionConfiguration config)
      throws Exception {
    if (config.getName() == null) {
      ClusterManagerImpl.log.warn(
          "Must specify a unique name for each cluster connection. This one will not be deployed.");

      return;
    }

    if (config.getAddress() == null) {
      ClusterManagerImpl.log.warn(
          "Must specify an address for each cluster connection. This one will not be deployed.");

      return;
    }

    TransportConfiguration connector =
        configuration.getConnectorConfigurations().get(config.getConnectorName());

    if (connector == null) {
      log.warn(
          "No connector with name '"
              + config.getConnectorName()
              + "'. The cluster connection will not be deployed.");
      return;
    }

    if (clusterConnections.containsKey(config.getName())) {
      log.warn(
          "Cluster Configuration  '"
              + config.getConnectorName()
              + "' already exists. The cluster connection will not be deployed.",
          new Exception("trace"));
      return;
    }

    ClusterConnectionImpl clusterConnection;

    if (config.getDiscoveryGroupName() != null) {
      DiscoveryGroupConfiguration dg =
          configuration.getDiscoveryGroupConfigurations().get(config.getDiscoveryGroupName());

      if (dg == null) {
        ClusterManagerImpl.log.warn(
            "No discovery group with name '"
                + config.getDiscoveryGroupName()
                + "'. The cluster connection will not be deployed.");
        return;
      }

      if (log.isDebugEnabled()) {
        log.debug(
            this
                + " Starting a Discovery Group Cluster Connection, name="
                + config.getDiscoveryGroupName()
                + ", dg="
                + dg);
      }

      clusterConnection =
          new ClusterConnectionImpl(
              this,
              dg,
              connector,
              new SimpleString(config.getName()),
              new SimpleString(config.getAddress()),
              config.getMinLargeMessageSize(),
              config.getClientFailureCheckPeriod(),
              config.getConnectionTTL(),
              config.getRetryInterval(),
              config.getRetryIntervalMultiplier(),
              config.getMaxRetryInterval(),
              config.getReconnectAttempts(),
              config.getCallTimeout(),
              config.getCallFailoverTimeout(),
              config.isDuplicateDetection(),
              config.isForwardWhenNoConsumers(),
              config.getConfirmationWindowSize(),
              executorFactory,
              server,
              postOffice,
              managementService,
              scheduledExecutor,
              config.getMaxHops(),
              nodeUUID,
              backup,
              server.getConfiguration().getClusterUser(),
              server.getConfiguration().getClusterPassword(),
              config.isAllowDirectConnectionsOnly());
    } else {
      TransportConfiguration[] tcConfigs =
          config.getStaticConnectors() != null
              ? connectorNameListToArray(config.getStaticConnectors())
              : null;

      if (log.isDebugEnabled()) {
        log.debug(this + " defining cluster connection towards " + Arrays.toString(tcConfigs));
      }

      clusterConnection =
          new ClusterConnectionImpl(
              this,
              tcConfigs,
              connector,
              new SimpleString(config.getName()),
              new SimpleString(config.getAddress()),
              config.getMinLargeMessageSize(),
              config.getClientFailureCheckPeriod(),
              config.getConnectionTTL(),
              config.getRetryInterval(),
              config.getRetryIntervalMultiplier(),
              config.getMaxRetryInterval(),
              config.getReconnectAttempts(),
              config.getCallTimeout(),
              config.getCallFailoverTimeout(),
              config.isDuplicateDetection(),
              config.isForwardWhenNoConsumers(),
              config.getConfirmationWindowSize(),
              executorFactory,
              server,
              postOffice,
              managementService,
              scheduledExecutor,
              config.getMaxHops(),
              nodeUUID,
              backup,
              server.getConfiguration().getClusterUser(),
              server.getConfiguration().getClusterPassword(),
              config.isAllowDirectConnectionsOnly());
    }

    if (defaultClusterConnection == null) {
      defaultClusterConnection = clusterConnection;
    }

    managementService.registerCluster(clusterConnection, config);

    clusterConnections.put(config.getName(), clusterConnection);

    if (log.isDebugEnabled()) {
      log.debug("ClusterConnection.start at " + clusterConnection, new Exception("trace"));
    }
  }
  @Test
  public void testSendPackets() throws Exception {
    setupServer(true);

    StorageManager storage = getStorage();

    manager = liveServer.getReplicationManager();
    waitForComponent(manager);

    Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);

    replicatedJournal.appendPrepareRecord(1, new FakeData(), false);

    replicatedJournal.appendAddRecord(1, (byte) 1, new FakeData(), false);
    replicatedJournal.appendUpdateRecord(1, (byte) 2, new FakeData(), false);
    replicatedJournal.appendDeleteRecord(1, false);
    replicatedJournal.appendAddRecordTransactional(2, 2, (byte) 1, new FakeData());
    replicatedJournal.appendUpdateRecordTransactional(2, 2, (byte) 2, new FakeData());
    replicatedJournal.appendCommitRecord(2, false);

    replicatedJournal.appendDeleteRecordTransactional(3, 4, new FakeData());
    replicatedJournal.appendPrepareRecord(3, new FakeData(), false);
    replicatedJournal.appendRollbackRecord(3, false);

    blockOnReplication(storage, manager);

    Assert.assertTrue(
        "Expecting no active tokens:" + manager.getActiveTokens(),
        manager.getActiveTokens().isEmpty());

    ServerMessage msg = new ServerMessageImpl(1, 1024);

    SimpleString dummy = new SimpleString("dummy");
    msg.setAddress(dummy);

    replicatedJournal.appendAddRecordTransactional(23, 24, (byte) 1, new FakeData());

    PagedMessage pgmsg = new PagedMessageImpl(msg, new long[0]);
    manager.pageWrite(pgmsg, 1);
    manager.pageWrite(pgmsg, 2);
    manager.pageWrite(pgmsg, 3);
    manager.pageWrite(pgmsg, 4);

    blockOnReplication(storage, manager);

    PagingManager pagingManager =
        createPageManager(
            backupServer.getStorageManager(), backupServer.getConfiguration(),
            backupServer.getExecutorFactory(), backupServer.getAddressSettingsRepository());

    PagingStore store = pagingManager.getPageStore(dummy);
    store.start();
    Assert.assertEquals(4, store.getNumberOfPages());
    store.stop();

    manager.pageDeleted(dummy, 1);
    manager.pageDeleted(dummy, 2);
    manager.pageDeleted(dummy, 3);
    manager.pageDeleted(dummy, 4);
    manager.pageDeleted(dummy, 5);
    manager.pageDeleted(dummy, 6);

    blockOnReplication(storage, manager);

    ServerMessageImpl serverMsg = new ServerMessageImpl();
    serverMsg.setMessageID(500);
    serverMsg.setAddress(new SimpleString("tttt"));

    HornetQBuffer buffer = HornetQBuffers.dynamicBuffer(100);
    serverMsg.encodeHeadersAndProperties(buffer);

    manager.largeMessageBegin(500);

    manager.largeMessageWrite(500, new byte[1024]);

    manager.largeMessageDelete(Long.valueOf(500));

    blockOnReplication(storage, manager);

    store.start();

    Assert.assertEquals(0, store.getNumberOfPages());
  }
  @Test
  public void testGetNumberOfPages() throws Exception {
    session.close();
    server.stop();
    server.getConfiguration().setPersistenceEnabled(true);

    SimpleString address = RandomUtil.randomSimpleString();

    AddressSettings addressSettings = new AddressSettings();
    addressSettings.setPageSizeBytes(1024);
    addressSettings.setMaxSizeBytes(10 * 1024);
    final int NUMBER_MESSAGES_BEFORE_PAGING = 5;

    server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);
    server.start();
    ServerLocator locator2 =
        HornetQClient.createServerLocatorWithoutHA(
            new TransportConfiguration(UnitTestCase.INVM_CONNECTOR_FACTORY));
    addServerLocator(locator2);
    ClientSessionFactory sf2 = createSessionFactory(locator2);

    session = sf2.createSession(false, true, false);
    session.start();
    session.createQueue(address, address, true);

    QueueImpl serverQueue = (QueueImpl) server.locateQueue(address);

    ClientProducer producer = session.createProducer(address);

    for (int i = 0; i < NUMBER_MESSAGES_BEFORE_PAGING; i++) {
      ClientMessage msg = session.createMessage(true);
      msg.getBodyBuffer().writeBytes(new byte[512]);
      producer.send(msg);
    }
    session.commit();

    AddressControl addressControl = createManagementControl(address);
    Assert.assertEquals(0, addressControl.getNumberOfPages());

    ClientMessage msg = session.createMessage(true);
    msg.getBodyBuffer().writeBytes(new byte[512]);
    producer.send(msg);

    session.commit();
    Assert.assertEquals(1, addressControl.getNumberOfPages());

    msg = session.createMessage(true);
    msg.getBodyBuffer().writeBytes(new byte[512]);
    producer.send(msg);

    session.commit();
    Assert.assertEquals(1, addressControl.getNumberOfPages());

    msg = session.createMessage(true);
    msg.getBodyBuffer().writeBytes(new byte[512]);
    producer.send(msg);

    session.commit();

    Assert.assertEquals("# of pages is 2", 2, addressControl.getNumberOfPages());

    System.out.println("Address size=" + addressControl.getAddressSize());

    Assert.assertEquals(
        serverQueue.getPageSubscription().getPagingStore().getAddressSize(),
        addressControl.getAddressSize());
  }
Exemple #7
0
  private void deployClusterConnection(final ClusterConnectionConfiguration config)
      throws Exception {
    if (config.getName() == null) {
      HornetQServerLogger.LOGGER.clusterConnectionNotUnique();

      return;
    }

    if (config.getAddress() == null) {
      HornetQServerLogger.LOGGER.clusterConnectionNoForwardAddress();

      return;
    }

    TransportConfiguration connector =
        configuration.getConnectorConfigurations().get(config.getConnectorName());

    if (connector == null) {
      HornetQServerLogger.LOGGER.clusterConnectionNoConnector(config.getConnectorName());
      return;
    }

    if (clusterConnections.containsKey(config.getName())) {
      HornetQServerLogger.LOGGER.clusterConnectionAlreadyExists(config.getConnectorName());
      return;
    }

    ClusterConnectionImpl clusterConnection;
    DiscoveryGroupConfiguration dg;

    if (config.getDiscoveryGroupName() != null) {
      dg = configuration.getDiscoveryGroupConfigurations().get(config.getDiscoveryGroupName());

      if (dg == null) {
        HornetQServerLogger.LOGGER.clusterConnectionNoDiscoveryGroup(
            config.getDiscoveryGroupName());
        return;
      }

      if (HornetQServerLogger.LOGGER.isDebugEnabled()) {
        HornetQServerLogger.LOGGER.debug(
            this
                + " Starting a Discovery Group Cluster Connection, name="
                + config.getDiscoveryGroupName()
                + ", dg="
                + dg);
      }

      clusterConnection =
          new ClusterConnectionImpl(
              this,
              dg,
              connector,
              new SimpleString(config.getName()),
              new SimpleString(config.getAddress()),
              config.getMinLargeMessageSize(),
              config.getClientFailureCheckPeriod(),
              config.getConnectionTTL(),
              config.getRetryInterval(),
              config.getRetryIntervalMultiplier(),
              config.getMaxRetryInterval(),
              config.getReconnectAttempts(),
              config.getCallTimeout(),
              config.getCallFailoverTimeout(),
              config.isDuplicateDetection(),
              config.isForwardWhenNoConsumers(),
              config.getConfirmationWindowSize(),
              executorFactory,
              threadPool,
              server,
              postOffice,
              managementService,
              scheduledExecutor,
              config.getMaxHops(),
              nodeManager,
              backup,
              server.getConfiguration().getClusterUser(),
              server.getConfiguration().getClusterPassword(),
              config.isAllowDirectConnectionsOnly(),
              config.getClusterNotificationInterval(),
              config.getClusterNotificationAttempts());
    } else {
      TransportConfiguration[] tcConfigs =
          config.getStaticConnectors() != null
              ? connectorNameListToArray(config.getStaticConnectors())
              : null;

      if (HornetQServerLogger.LOGGER.isDebugEnabled()) {
        HornetQServerLogger.LOGGER.debug(
            this + " defining cluster connection towards " + Arrays.toString(tcConfigs));
      }

      clusterConnection =
          new ClusterConnectionImpl(
              this,
              tcConfigs,
              connector,
              new SimpleString(config.getName()),
              new SimpleString(config.getAddress()),
              config.getMinLargeMessageSize(),
              config.getClientFailureCheckPeriod(),
              config.getConnectionTTL(),
              config.getRetryInterval(),
              config.getRetryIntervalMultiplier(),
              config.getMaxRetryInterval(),
              config.getReconnectAttempts(),
              config.getCallTimeout(),
              config.getCallFailoverTimeout(),
              config.isDuplicateDetection(),
              config.isForwardWhenNoConsumers(),
              config.getConfirmationWindowSize(),
              executorFactory,
              threadPool,
              server,
              postOffice,
              managementService,
              scheduledExecutor,
              config.getMaxHops(),
              nodeManager,
              backup,
              server.getConfiguration().getClusterUser(),
              server.getConfiguration().getClusterPassword(),
              config.isAllowDirectConnectionsOnly(),
              config.getClusterNotificationInterval(),
              config.getClusterNotificationAttempts());
    }

    if (defaultClusterConnection == null) {
      defaultClusterConnection = clusterConnection;
    }

    managementService.registerCluster(clusterConnection, config);

    clusterConnections.put(config.getName(), clusterConnection);

    if (HornetQServerLogger.LOGGER.isTraceEnabled()) {
      HornetQServerLogger.LOGGER.trace(
          "ClusterConnection.start at " + clusterConnection, new Exception("trace"));
    }
  }
  @Test
  public void testSimpleDistributionBackupStrategyFull() throws Exception {
    HornetQServer server0 = createServer(0, 1, BackupStrategy.FULL);
    HornetQServer server1 = createServer(1, 0, BackupStrategy.FULL);
    TransportConfiguration liveConnector0 =
        getConnectorTransportConfiguration("liveConnector" + 0, 0);
    TransportConfiguration liveConnector1 =
        getConnectorTransportConfiguration("liveConnector" + 1, 1);

    try (ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(liveConnector0)) {
      server0.start();
      server1.start();
      ClientSessionFactory sessionFactory0 = serverLocator.createSessionFactory(liveConnector0);
      waitForRemoteBackup(sessionFactory0, 10);
      ClientSessionFactory sessionFactory1 = serverLocator.createSessionFactory(liveConnector1);
      waitForRemoteBackup(sessionFactory1, 10);
      Topology topology = serverLocator.getTopology();
      Collection<TopologyMemberImpl> members = topology.getMembers();
      assertEquals(members.size(), 2);
      Map<String, HornetQServer> backupServers0 =
          server0.getClusterManager().getHAManager().getBackupServers();
      assertEquals(backupServers0.size(), 1);
      Map<String, HornetQServer> backupServers1 =
          server1.getClusterManager().getHAManager().getBackupServers();
      assertEquals(backupServers1.size(), 1);
      HornetQServer backupServer0 = backupServers0.values().iterator().next();
      HornetQServer backupServer1 = backupServers1.values().iterator().next();
      waitForRemoteBackupSynchronization(backupServer0);
      waitForRemoteBackupSynchronization(backupServer1);
      assertEquals(server0.getNodeID(), backupServer1.getNodeID());
      assertEquals(server1.getNodeID(), backupServer0.getNodeID());
      Set<TransportConfiguration> backupAcceptors0 =
          backupServer0.getConfiguration().getAcceptorConfigurations();
      assertEquals(1, backupAcceptors0.size());
      assertEquals("5545", backupAcceptors0.iterator().next().getParams().get("port"));
      Set<TransportConfiguration> backupAcceptors1 =
          backupServer1.getConfiguration().getAcceptorConfigurations();
      assertEquals(1, backupAcceptors1.size());
      assertEquals("5546", backupAcceptors1.iterator().next().getParams().get("port"));
      Map<String, TransportConfiguration> connectorConfigurations0 =
          backupServer0.getConfiguration().getConnectorConfigurations();
      assertEquals(2, connectorConfigurations0.size());
      assertEquals("5545", connectorConfigurations0.get("liveConnector0").getParams().get("port"));
      assertEquals(
          "5446", connectorConfigurations0.get("remoteConnector0").getParams().get("port"));
      Map<String, TransportConfiguration> connectorConfigurations1 =
          backupServer1.getConfiguration().getConnectorConfigurations();
      assertEquals(2, connectorConfigurations1.size());
      assertEquals("5546", connectorConfigurations1.get("liveConnector1").getParams().get("port"));
      assertEquals(
          "5445", connectorConfigurations1.get("remoteConnector1").getParams().get("port"));
      if (policyType == HAPolicy.POLICY_TYPE.COLOCATED_SHARED_STORE) {
        assertEquals(
            server0.getConfiguration().getJournalDirectory(),
            backupServer1.getConfiguration().getJournalDirectory());
        assertEquals(
            server0.getConfiguration().getBindingsDirectory(),
            backupServer1.getConfiguration().getBindingsDirectory());
        assertEquals(
            server0.getConfiguration().getLargeMessagesDirectory(),
            backupServer1.getConfiguration().getLargeMessagesDirectory());
        assertEquals(
            server0.getConfiguration().getPagingDirectory(),
            backupServer1.getConfiguration().getPagingDirectory());
        assertEquals(
            server1.getConfiguration().getJournalDirectory(),
            backupServer0.getConfiguration().getJournalDirectory());
        assertEquals(
            server1.getConfiguration().getBindingsDirectory(),
            backupServer0.getConfiguration().getBindingsDirectory());
        assertEquals(
            server1.getConfiguration().getLargeMessagesDirectory(),
            backupServer0.getConfiguration().getLargeMessagesDirectory());
        assertEquals(
            server1.getConfiguration().getPagingDirectory(),
            backupServer0.getConfiguration().getPagingDirectory());
      } else {
        assertNotEquals(
            server0.getConfiguration().getJournalDirectory(),
            backupServer1.getConfiguration().getJournalDirectory());
        assertNotEquals(
            server0.getConfiguration().getBindingsDirectory(),
            backupServer1.getConfiguration().getBindingsDirectory());
        assertNotEquals(
            server0.getConfiguration().getLargeMessagesDirectory(),
            backupServer1.getConfiguration().getLargeMessagesDirectory());
        assertNotEquals(
            server0.getConfiguration().getPagingDirectory(),
            backupServer1.getConfiguration().getPagingDirectory());
        assertNotEquals(
            server1.getConfiguration().getJournalDirectory(),
            backupServer0.getConfiguration().getJournalDirectory());
        assertNotEquals(
            server1.getConfiguration().getBindingsDirectory(),
            backupServer0.getConfiguration().getBindingsDirectory());
        assertNotEquals(
            server1.getConfiguration().getLargeMessagesDirectory(),
            backupServer0.getConfiguration().getLargeMessagesDirectory());
        assertNotEquals(
            server1.getConfiguration().getPagingDirectory(),
            backupServer0.getConfiguration().getPagingDirectory());
      }
    } finally {
      server0.stop();
      server1.stop();
    }
  }
  public void doTestStressSend(final boolean netty) throws Exception {
    // first set up the server
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(TransportConstants.PORT_PROP_NAME, 5445);
    params.put(TransportConstants.HOST_PROP_NAME, "localhost");
    params.put(TransportConstants.USE_NIO_PROP_NAME, true);
    // minimize threads to maximize possibility for deadlock
    params.put(TransportConstants.NIO_REMOTING_THREADS_PROPNAME, 1);
    params.put(TransportConstants.BATCH_DELAY, 50);
    Configuration config = createDefaultConfig(params, ServiceTestBase.NETTY_ACCEPTOR_FACTORY);
    HornetQServer server = createServer(true, config);
    server.getConfiguration().setThreadPoolMaxSize(2);
    server.start();

    // now the client side
    Map<String, Object> connectionParams = new HashMap<String, Object>();
    connectionParams.put(TransportConstants.PORT_PROP_NAME, 5445);
    connectionParams.put(TransportConstants.HOST_PROP_NAME, "localhost");
    connectionParams.put(TransportConstants.USE_NIO_PROP_NAME, true);
    connectionParams.put(TransportConstants.BATCH_DELAY, 50);
    connectionParams.put(TransportConstants.NIO_REMOTING_THREADS_PROPNAME, 6);
    final TransportConfiguration transpConf =
        new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams);
    final ServerLocator locator = createNonHALocator(netty);

    // each thread will do this number of transactions
    final int numberOfMessages = 100;

    // these must all be the same
    final int numProducers = 30;
    final int numConsumerProducers = 30;
    final int numConsumers = 30;

    // each produce, consume+produce and consume increments this counter
    final AtomicInteger totalCount = new AtomicInteger(0);

    // the total we expect if all producers, consumer-producers and
    // consumers complete normally
    int totalExpectedCount =
        (numProducers + numConsumerProducers + numConsumerProducers) * numberOfMessages;

    // each group gets a separate connection
    final Connection connectionProducer;
    final Connection connectionConsumerProducer;
    final Connection connectionConsumer;

    // create the 2 queues used in the test
    ClientSessionFactory sf = locator.createSessionFactory(transpConf);
    ClientSession session = sf.createTransactedSession();
    session.createQueue("jms.queue.queue", "jms.queue.queue");
    session.createQueue("jms.queue.queue2", "jms.queue.queue2");
    session.commit();
    sf.close();
    session.close();
    locator.close();

    // create and start JMS connections
    HornetQConnectionFactory cf =
        HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transpConf);
    connectionProducer = cf.createConnection();
    connectionProducer.start();

    connectionConsumerProducer = cf.createConnection();
    connectionConsumerProducer.start();

    connectionConsumer = cf.createConnection();
    connectionConsumer.start();

    // these threads produce messages on the the first queue
    for (int i = 0; i < numProducers; i++) {
      new Thread() {
        @Override
        public void run() {

          Session session = null;
          try {
            session = connectionProducer.createSession(true, Session.SESSION_TRANSACTED);
            MessageProducer messageProducer =
                session.createProducer(HornetQDestination.createQueue("queue"));
            messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

            for (int i = 0; i < numberOfMessages; i++) {
              BytesMessage message = session.createBytesMessage();
              message.writeBytes(new byte[3000]);
              message.setStringProperty("Service", "LoadShedService");
              message.setStringProperty("Action", "testAction");

              messageProducer.send(message);
              session.commit();

              totalCount.incrementAndGet();
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          } finally {
            if (session != null) {
              try {
                session.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
        }
      }.start();
    }

    // these threads just consume from the one and produce on a second queue
    for (int i = 0; i < numConsumerProducers; i++) {
      new Thread() {
        @Override
        public void run() {
          Session session = null;
          try {
            session = connectionConsumerProducer.createSession(true, Session.SESSION_TRANSACTED);
            MessageConsumer consumer =
                session.createConsumer(HornetQDestination.createQueue("queue"));
            MessageProducer messageProducer =
                session.createProducer(HornetQDestination.createQueue("queue2"));
            messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
            for (int i = 0; i < numberOfMessages; i++) {
              BytesMessage message = (BytesMessage) consumer.receive(5000);
              if (message == null) {
                return;
              }
              message = session.createBytesMessage();
              message.writeBytes(new byte[3000]);
              message.setStringProperty("Service", "LoadShedService");
              message.setStringProperty("Action", "testAction");
              messageProducer.send(message);
              session.commit();

              totalCount.incrementAndGet();
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          } finally {
            if (session != null) {
              try {
                session.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
        }
      }.start();
    }

    // these threads consume from the second queue
    for (int i = 0; i < numConsumers; i++) {
      new Thread() {
        @Override
        public void run() {
          Session session = null;
          try {
            session = connectionConsumer.createSession(true, Session.SESSION_TRANSACTED);
            MessageConsumer consumer =
                session.createConsumer(HornetQDestination.createQueue("queue2"));
            for (int i = 0; i < numberOfMessages; i++) {
              BytesMessage message = (BytesMessage) consumer.receive(5000);
              if (message == null) {
                return;
              }
              session.commit();

              totalCount.incrementAndGet();
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          } finally {
            if (session != null) {
              try {
                session.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
        }
      }.start();
    }

    // check that the overall transaction count reaches the expected number,
    // which would indicate that the system didn't stall
    int timeoutCounter = 0;
    int maxSecondsToWait = 60;
    while (timeoutCounter < maxSecondsToWait && totalCount.get() < totalExpectedCount) {
      timeoutCounter++;
      Thread.sleep(1000);
      System.out.println(
          "Not done yet.. " + (maxSecondsToWait - timeoutCounter) + "; " + totalCount.get());
    }
    System.out.println("Done.." + totalCount.get() + ", expected " + totalExpectedCount);
    Assert.assertEquals("Possible deadlock", totalExpectedCount, totalCount.get());
    System.out.println("After assert");

    // attempt cleaning up (this is not in a finally, still needs some work)
    connectionProducer.close();
    connectionConsumerProducer.close();
    connectionConsumer.close();

    server.stop();
  }
  @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();
    }
  }
  @Test
  public void testTargetServerUpAndDown() throws Exception {
    // This test needs to use real files, since it requires duplicate detection, since when the
    // target server is
    // shutdown, messages will get resent when it is started, so the dup id cache needs
    // to be persisted

    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);

    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(500)
            .setReconnectAttempts(-1)
            .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);
    ServerLocator locator = null;
    try {
      // Don't start server 1 yet

      server0.start();
      waitForServer(server0);

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

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

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

      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);
      }

      // Wait a bit
      Thread.sleep(1000);

      server1.start();
      waitForServer(server1);

      ClientSessionFactory sf1 = locator.createSessionFactory(server1tc);

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

      ClientConsumer consumer1 = session1.createConsumer(queueName1);

      session1.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());

      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(1000);

        Assert.assertNotNull(message);

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

        message.acknowledge();
      }

      Assert.assertNull(consumer1.receiveImmediate());

      session1.close();

      sf1.close();

      BridgeStartTest.log.info("stopping server 1");

      server1.stop();

      BridgeStartTest.log.info("stopped server 1");

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

        message.putIntProperty(propKey, i);

        producer0.send(message);
      }

      BridgeStartTest.log.info("sent some more messages");

      server1.start();
      waitForServer(server1);

      BridgeStartTest.log.info("started server1");

      sf1 = locator.createSessionFactory(server1tc);

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

      consumer1 = session1.createConsumer(queueName1);

      session1.start();

      BridgeStartTest.log.info("started session");

      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());

      session1.close();

      sf1.close();

      session0.close();

      sf0.close();

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

      server0.stop();

      server1.stop();
    }
  }