/**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
   // TODO Auto-generated method stub
   EmbeddedHornetQ embedded = new EmbeddedHornetQ();
   embedded.start();
   Thread.sleep(10000);
   embedded.stop();
 }
  /**
   * Remove any old message queues that have a 0 message count in them. This lets us not worry about
   * changing around the registered listeners.
   */
  private void cleanupOldQueues() {
    log.debug("Cleaning old message queues");
    try {
      String[] queues = hornetqServer.getHornetQServer().getHornetQServerControl().getQueueNames();

      ServerLocator locator =
          HornetQClient.createServerLocatorWithoutHA(
              new TransportConfiguration(InVMConnectorFactory.class.getName()));

      ClientSessionFactory factory = locator.createSessionFactory();
      ClientSession session = factory.createSession(true, true);
      session.start();

      for (int i = 0; i < queues.length; i++) {
        long msgCount = session.queueQuery(new SimpleString(queues[i])).getMessageCount();
        if (msgCount == 0) {
          log.debug(String.format("found queue '%s' with 0 messages. deleting", queues[i]));
          session.deleteQueue(queues[i]);
        } else {
          log.debug(String.format("found queue '%s' with %d messages. kept", queues[i], msgCount));
        }
      }

      session.stop();
      session.close();
    } catch (HornetQException e) {
      log.error("Problem cleaning old message queues:", e);
      throw new RuntimeException(e);
    } catch (Exception e) {
      log.error("Problem cleaning old message queues:", e);
      throw new RuntimeException(e);
    }
  }
 public void contextDestroyed() {
   if (hornetqServer != null) {
     eventSource.shutDown();
     try {
       hornetqServer.stop();
       log.info("Hornetq server stopped.");
     } catch (Exception e) {
       log.error("Error stopping hornetq server", e);
     }
   }
 }
  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);
    }
  }