final ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
final ExchangeFactory exchangeFactory = virtualHost.getExchangeFactory();
final QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
Exchange dlExchange = null;
synchronized(exchangeRegistry)
{
dlExchange = exchangeRegistry.getExchange(dlExchangeName);
if(dlExchange == null)
{
dlExchange = exchangeFactory.createExchange(UUIDGenerator.generateExchangeUUID(dlExchangeName, virtualHost.getName()), new AMQShortString(dlExchangeName), ExchangeDefaults.FANOUT_EXCHANGE_CLASS, true, false, 0);
exchangeRegistry.registerExchange(dlExchange);
//enter the dle in the persistent store
virtualHost.getMessageStore().createExchange(dlExchange);
}
}
AMQQueue dlQueue = null;
synchronized(queueRegistry)
{
dlQueue = queueRegistry.getQueue(dlQueueName);
if(dlQueue == null)
{
//set args to disable DLQ'ing/MDC from the DLQ itself, preventing loops etc
final Map<String, Object> args = new HashMap<String, Object>();
args.put(X_QPID_DLQ_ENABLED, false);
args.put(X_QPID_MAXIMUM_DELIVERY_COUNT, 0);
dlQueue = createAMQQueueImpl(UUIDGenerator.generateQueueUUID(dlQueueName, virtualHost.getName()), dlQueueName, true, owner, false, exclusive, virtualHost, args);
//enter the dlq in the persistent store
virtualHost.getMessageStore().createQueue(dlQueue, FieldTable.convertToFieldTable(args));
}
}
//ensure the queue is bound to the exchange
if(!dlExchange.isBound(DLQ_ROUTING_KEY, dlQueue))
{
//actual routing key used does not matter due to use of fanout exchange,
//but we will make the key 'dlq' as it can be logged at creation.
virtualHost.getBindingFactory().addBinding(DLQ_ROUTING_KEY, dlQueue, dlExchange, null);
}