VirtualHost virtualHost = session.getVirtualHost();
ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
MessageStore store = virtualHost.getMessageStore();
QueueDeclareBody body = evt.getMethod();
// if we aren't given a queue name, we create one which we return to the client
if (body.queue == null)
{
body.queue = createName();
}
AMQQueue queue;
//TODO: do we need to check that the queue already exists with exactly the same "configuration"?
synchronized (queueRegistry)
{
if (((queue = queueRegistry.getQueue(body.queue)) == null))
{
if(body.queue != null)
{
body.queue = body.queue.intern();
}
if (body.passive)
{
String msg = "Queue: " + body.queue + " not found on VirtualHost(" + virtualHost + ").";
throw body.getChannelException(AMQConstant.NOT_FOUND, msg);
}
else
{
queue = createQueue(body, virtualHost, session);
if (queue.isDurable() && !queue.isAutoDelete())
{
store.createQueue(queue);
}
queueRegistry.registerQueue(queue);
if (autoRegister)
{
Exchange defaultExchange = exchangeRegistry.getDefaultExchange();
queue.bind(body.queue, null, defaultExchange);
_log.info("Queue " + body.queue + " bound to default exchange(" + defaultExchange.getName() + ")");
}
}
}
else if (queue.getOwner() != null && !session.getContextKey().equals(queue.getOwner()))
{
throw body.getChannelException(AMQConstant.ALREADY_EXISTS, "Cannot declare queue('" + body.queue + "'),"
+ " as exclusive queue with same name "
+ "declared on another client ID('"
+ queue.getOwner() + "')");
}
AMQChannel channel = session.getChannel(evt.getChannelId());
if (channel == null)
{
throw body.getChannelNotFoundException(evt.getChannelId());
}
//set this as the default queue on the channel:
channel.setDefaultQueue(queue);
}