if(createDLQ)
{
final String dlExchangeName = getDeadLetterExchangeName(queueName);
final String dlQueueName = getDeadLetterQueueName(queueName);
Exchange dlExchange = null;
final UUID dlExchangeId = UUIDGenerator.generateExchangeUUID(dlExchangeName, _virtualHost.getName());
try
{
dlExchange = _virtualHost.createExchange(dlExchangeId,
dlExchangeName,
ExchangeDefaults.FANOUT_EXCHANGE_CLASS,
true, false, null);
}
catch(ExchangeExistsException e)
{
// We're ok if the exchange already exists
dlExchange = e.getExistingExchange();
}
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(Queue.CREATE_DLQ_ON_CREATION, false);
args.put(Queue.MAXIMUM_DELIVERY_ATTEMPTS, 0);
dlQueue = _virtualHost.createQueue(UUIDGenerator.generateQueueUUID(dlQueueName, _virtualHost.getName()), dlQueueName, true, owner, false, exclusive,
false, 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.
dlExchange.addBinding(DLQ_ROUTING_KEY, dlQueue, null);
}
q.setAlternateExchange(dlExchange);
}
else if(arguments != null && arguments.get(Queue.ALTERNATE_EXCHANGE) instanceof String)
{
final String altExchangeAttr = (String) arguments.get(Queue.ALTERNATE_EXCHANGE);
Exchange altExchange;
try
{
altExchange = _virtualHost.getExchange(UUID.fromString(altExchangeAttr));
}
catch(IllegalArgumentException e)