public void testConnection(
      final String acceptorHost, final String connectorHost, final boolean mustConnect)
      throws Exception {
    System.out.println(
        "acceptor="
            + acceptorHost
            + ", connector="
            + connectorHost
            + ", mustConnect="
            + mustConnect);

    Map<String, Object> params = new HashMap<String, Object>();
    params.put(getHostPropertyKey(), acceptorHost);
    TransportConfiguration acceptorConfig =
        new TransportConfiguration(getAcceptorFactoryClassName(), params);
    Set<TransportConfiguration> transportConfigs = new HashSet<TransportConfiguration>();
    transportConfigs.add(acceptorConfig);

    Configuration config = createDefaultConfig(true);
    config.setAcceptorConfigurations(transportConfigs);
    HornetQServer messagingService = createServer(false, config);
    messagingService.start();

    params = new HashMap<String, Object>();
    params.put(getHostPropertyKey(), connectorHost);
    TransportConfiguration connectorConfig =
        new TransportConfiguration(getConnectorFactoryClassName(), params);

    try {
      ClientSessionFactory sf = HornetQClient.createClientSessionFactory(connectorConfig);

      if (mustConnect) {
        ClientSession session = sf.createSession(false, true, true);
        session.close();
        System.out.println("connection OK");
      } else {
        try {
          sf.createSession(false, true, true);
          Assert.fail(
              "session creation must fail because connector must not be able to connect to the server bound to another network interface");
        } catch (Exception e) {
        }
      }
    } finally {
      if (messagingService != null) {
        messagingService.stop();
      }
    }
  }
  public void contextInitialized(Injector injector) {
    org.candlepin.common.config.Configuration candlepinConfig =
        injector.getInstance(org.candlepin.common.config.Configuration.class);

    if (hornetqServer == null) {
      Configuration config = new ConfigurationImpl();

      HashSet<TransportConfiguration> transports = new HashSet<TransportConfiguration>();
      transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
      config.setAcceptorConfigurations(transports);

      // alter the default pass to silence log output
      config.setClusterUser(null);
      config.setClusterPassword(null);

      // in vm, who needs security?
      config.setSecurityEnabled(false);

      config.setJournalType(JournalType.NIO);

      config.setCreateBindingsDir(true);
      config.setCreateJournalDir(true);

      String baseDir = candlepinConfig.getString(ConfigProperties.HORNETQ_BASE_DIR);

      config.setBindingsDirectory(new File(baseDir, "bindings").toString());
      config.setJournalDirectory(new File(baseDir, "journal").toString());
      config.setLargeMessagesDirectory(new File(baseDir, "largemsgs").toString());
      config.setPagingDirectory(new File(baseDir, "paging").toString());

      Map<String, AddressSettings> settings = new HashMap<String, AddressSettings>();
      AddressSettings pagingConfig = new AddressSettings();

      String addressPolicyString =
          candlepinConfig.getString(ConfigProperties.HORNETQ_ADDRESS_FULL_POLICY);
      long maxQueueSizeInMb = candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_QUEUE_SIZE);
      long maxPageSizeInMb = candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_PAGE_SIZE);

      AddressFullMessagePolicy addressPolicy = null;
      if (addressPolicyString.equals("PAGE")) {
        addressPolicy = AddressFullMessagePolicy.PAGE;
      } else if (addressPolicyString.equals("BLOCK")) {
        addressPolicy = AddressFullMessagePolicy.BLOCK;
      } else {
        throw new IllegalArgumentException(
            "Unknown HORNETQ_ADDRESS_FULL_POLICY: "
                + addressPolicyString
                + " . Please use one of: PAGE, BLOCK");
      }

      // Paging sizes need to be converted to bytes
      pagingConfig.setMaxSizeBytes(maxQueueSizeInMb * FileUtils.ONE_MB);
      if (addressPolicy == AddressFullMessagePolicy.PAGE) {
        pagingConfig.setPageSizeBytes(maxPageSizeInMb * FileUtils.ONE_MB);
      }
      pagingConfig.setAddressFullMessagePolicy(addressPolicy);
      // Enable for all the queues
      settings.put("#", pagingConfig);
      config.setAddressesSettings(settings);

      int maxScheduledThreads =
          candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_SCHEDULED_THREADS);
      int maxThreads = candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_THREADS);
      if (maxThreads != -1) {
        config.setThreadPoolMaxSize(maxThreads);
      }

      if (maxScheduledThreads != -1) {
        config.setScheduledThreadPoolMaxSize(maxScheduledThreads);
      }

      /**
       * Anything up to size of LARGE_MSG_SIZE may be needed to be written to the Journal, so we
       * must set buffer size accordingly.
       *
       * <p>If buffer size would be < LARGE_MSG_SIZE we may get exceptions such as this: Can't write
       * records bigger than the bufferSize(XXXYYY) on the journal
       */
      int largeMsgSize = candlepinConfig.getInt(ConfigProperties.HORNETQ_LARGE_MSG_SIZE);
      config.setJournalBufferSize_AIO(largeMsgSize);
      config.setJournalBufferSize_NIO(largeMsgSize);

      hornetqServer = new EmbeddedHornetQ();
      hornetqServer.setConfiguration(config);
    }
    try {
      hornetqServer.start();
      log.info("Hornetq server started");
    } catch (Exception e) {
      log.error("Failed to start hornetq message server:", e);
      throw new RuntimeException(e);
    }

    setupAmqp(injector, candlepinConfig);
    cleanupOldQueues();

    List<String> listeners = getHornetqListeners(candlepinConfig);

    eventSource = injector.getInstance(EventSource.class);
    for (int i = 0; i < listeners.size(); i++) {
      try {
        Class<?> clazz = this.getClass().getClassLoader().loadClass(listeners.get(i));
        eventSource.registerListener((EventListener) injector.getInstance(clazz));
      } catch (Exception e) {
        log.warn("Unable to register listener " + listeners.get(i), e);
      }
    }

    // Initialize the Event sink AFTER the internal server has been
    // created and started.
    EventSink sink = injector.getInstance(EventSink.class);
    try {
      sink.initialize();
    } catch (Exception e) {
      log.error("Failed to initialize EventSink:", e);
      throw new RuntimeException(e);
    }
  }