{
}
public void methodReceived(AMQStateManager stateManager, QueueBindBody body, int channelId) throws AMQException
{
AMQProtocolSession protocolConnection = stateManager.getProtocolSession();
VirtualHost virtualHost = protocolConnection.getVirtualHost();
AMQChannel channel = protocolConnection.getChannel(channelId);
if (channel == null)
{
throw body.getChannelNotFoundException(channelId);
}
final AMQQueue queue;
final AMQShortString routingKey;
final AMQShortString queueName = body.getQueue();
if (queueName == null)
{
queue = channel.getDefaultQueue();
if (queue == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "No default queue defined on channel and queue was null");
}
if (body.getRoutingKey() == null)
{
routingKey = AMQShortString.valueOf(queue.getName());
}
else
{
routingKey = body.getRoutingKey().intern();
}
}
else
{
queue = virtualHost.getQueue(queueName.toString());
routingKey = body.getRoutingKey() == null ? AMQShortString.EMPTY_STRING : body.getRoutingKey().intern();
}
if (queue == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Queue " + queueName + " does not exist.");
}
final String exchangeName = body.getExchange() == null ? null : body.getExchange().toString();
final Exchange exch = virtualHost.getExchange(exchangeName);
if (exch == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Exchange " + exchangeName + " does not exist.");
}
try
{
if (queue.isExclusive() && !queue.isDurable())
{
AMQSessionModel session = queue.getExclusiveOwningSession();
if (session == null || session.getConnectionModel() != protocolConnection)
{
throw body.getConnectionException(AMQConstant.NOT_ALLOWED,
"Queue " + queue.getName() + " is exclusive, but not created on this Connection.");
}
}
Map<String,Object> arguments = FieldTable.convertToMap(body.getArguments());
String bindingKey = String.valueOf(routingKey);
if (!exch.isBound(bindingKey, arguments, queue))
{
if(!exch.addBinding(bindingKey, queue, arguments) && TopicExchange.TYPE.equals(exch.getType()))
{
Binding oldBinding = exch.getBinding(bindingKey, queue, arguments);
Map<String, Object> oldArgs = oldBinding.getArguments();
if((oldArgs == null && !arguments.isEmpty()) || (oldArgs != null && !oldArgs.equals(arguments)))
{
exch.replaceBinding(oldBinding.getId(), bindingKey, queue, arguments);
}
}
}
}
catch (AMQException e)
{
throw body.getChannelException(AMQConstant.CHANNEL_ERROR, e.toString());
}
if (_log.isInfoEnabled())
{
_log.info("Binding queue " + queue + " to exchange " + exch + " with routing key " + routingKey);
}
if (!body.getNowait())
{
channel.sync();
MethodRegistry methodRegistry = protocolConnection.getMethodRegistry();
AMQMethodBody responseBody = methodRegistry.createQueueBindOkBody();
protocolConnection.writeFrame(responseBody.generateFrame(channelId));
}
}