private void doCreateBroker(boolean memoryLimit, int expireMessagesPeriod) throws Exception {
    broker = new BrokerService();
    broker.setBrokerName("localhost");
    broker.setUseJmx(true);
    broker.setDeleteAllMessagesOnStartup(true);
    broker.addConnector("tcp://localhost:0");

    PolicyMap policyMap = new PolicyMap();
    PolicyEntry defaultEntry = new PolicyEntry();
    defaultEntry.setOptimizedDispatch(optimizedDispatch);
    defaultEntry.setExpireMessagesPeriod(expireMessagesPeriod);
    defaultEntry.setMaxExpirePageSize(800);

    defaultEntry.setPendingQueuePolicy(pendingQueuePolicy);

    if (memoryLimit) {
      // so memory is not consumed by DLQ turn if off
      defaultEntry.setDeadLetterStrategy(null);
      defaultEntry.setMemoryLimit(200 * 1000);
    }

    policyMap.setDefaultEntry(defaultEntry);

    broker.setDestinationPolicy(policyMap);
    broker.start();
    broker.waitUntilStarted();

    connectionUri = broker.getTransportConnectors().get(0).getPublishableConnectString();
  }
  @Before
  public void startBroker() throws Exception {
    brokerService = new BrokerService();
    TestSupport.setPersistenceAdapter(brokerService, persistenceAdapterChoice);
    brokerService.setDeleteAllMessagesOnStartup(true);
    brokerService.setUseJmx(false);

    PolicyMap policyMap = new PolicyMap();
    PolicyEntry defaultEntry = new PolicyEntry();
    defaultEntry.setUseConsumerPriority(
        false); // java.lang.IllegalArgumentException: Comparison method violates its general
                // contract!
    defaultEntry.setMaxAuditDepth(publisherThreadCount);
    defaultEntry.setEnableAudit(true);
    defaultEntry.setUseCache(useCache);
    defaultEntry.setMaxPageSize(1000);
    defaultEntry.setOptimizedDispatch(optimizeDispatch);
    defaultEntry.setMemoryLimit(destMemoryLimit);
    defaultEntry.setExpireMessagesPeriod(0);
    policyMap.setDefaultEntry(defaultEntry);
    brokerService.setDestinationPolicy(policyMap);

    brokerService.getSystemUsage().getMemoryUsage().setLimit(512 * 1024 * 1024);

    TransportConnector transportConnector = brokerService.addConnector("tcp://0.0.0.0:0");
    brokerService.start();
    activemqURL = transportConnector.getPublishableConnectString();
  }
  protected void configureBroker(BrokerService brokerService) throws Exception {
    setPersistentAdapter(brokerService);
    brokerService.setAdvisorySupport(false);

    PolicyMap map = new PolicyMap();
    PolicyEntry defaultEntry = new PolicyEntry();
    defaultEntry.setMemoryLimit(1024 * 3);
    defaultEntry.setCursorMemoryHighWaterMark(68);
    defaultEntry.setExpireMessagesPeriod(0);
    map.setDefaultEntry(defaultEntry);
    brokerService.setDestinationPolicy(map);
  }
  protected void configureBroker(BrokerService answer) throws Exception {
    answer.setPersistent(false);
    answer.setMonitorConnectionSplits(true);
    final List<PolicyEntry> policyEntries = new ArrayList<PolicyEntry>();
    final PolicyEntry entry = new PolicyEntry();
    entry.setQueue(">");
    entry.setMemoryLimit(1024 * 1024 * 100); // Set to 1 MB
    entry.setOptimizedDispatch(true);
    entry.setProducerFlowControl(true);
    entry.setMaxPageSize(10);
    entry.setLazyDispatch(false);
    policyEntries.add(entry);

    final PolicyMap policyMap = new PolicyMap();
    policyMap.setPolicyEntries(policyEntries);
    answer.setDestinationPolicy(policyMap);
    super.configureBroker(answer);
  }