{
AMQProtocolSession protocolConnection = stateManager.getProtocolSession();
VirtualHost virtualHost = protocolConnection.getVirtualHost();
QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
AMQChannel channel = protocolConnection.getChannel(channelId);
if (channel == null)
{
throw body.getChannelNotFoundException(channelId);
}
AMQQueue queue;
if(body.getQueue() == null)
{
//get the default queue on the channel:
queue = channel.getDefaultQueue();
if(queue == null)
{
if(_failIfNotFound)
{
throw body.getConnectionException(AMQConstant.NOT_ALLOWED,"No queue specified.");
}
}
}
else
{
queue = queueRegistry.getQueue(body.getQueue());
}
if(queue == null)
{
if(_failIfNotFound)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Queue " + body.getQueue() + " does not exist.");
}
}
else
{
AMQSessionModel session = queue.getExclusiveOwningSession();
if (queue.isExclusive() && (session == null || session.getConnectionModel() != protocolConnection))
{
throw body.getConnectionException(AMQConstant.NOT_ALLOWED,
"Queue is exclusive, but not created on this Connection.");
}
long purged = queue.clearQueue();
if(!body.getNowait())
{
channel.sync();
MethodRegistry methodRegistry = protocolConnection.getMethodRegistry();
AMQMethodBody responseBody = methodRegistry.createQueuePurgeOkBody(purged);
protocolConnection.writeFrame(responseBody.generateFrame(channelId));
}