{
}
public void methodReceived(AMQStateManager stateManager, QueueBindBody body, int channelId) throws AMQException
{
AMQProtocolSession protocolConnection = stateManager.getProtocolSession();
VirtualHost virtualHost = protocolConnection.getVirtualHost();
ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
AMQChannel channel = protocolConnection.getChannel(channelId);
if (channel == null)
{
throw body.getChannelNotFoundException(channelId);
}
final AMQQueue queue;
final AMQShortString routingKey;
if (body.getQueue() == 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 = queue.getNameShortString();
}
else
{
routingKey = body.getRoutingKey().intern();
}
}
else
{
queue = queueRegistry.getQueue(body.getQueue());
routingKey = body.getRoutingKey() == null ? AMQShortString.EMPTY_STRING : body.getRoutingKey().intern();
}
if (queue == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Queue " + body.getQueue() + " does not exist.");
}
final Exchange exch = exchangeRegistry.getExchange(body.getExchange());
if (exch == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Exchange " + body.getExchange() + " 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.getNameShortString() + " is exclusive, but not created on this Connection.");
}
}
if (!exch.isBound(routingKey, body.getArguments(), queue))
{
String bindingKey = String.valueOf(routingKey);
Map<String,Object> arguments = FieldTable.convertToMap(body.getArguments());
if(!virtualHost.getBindingFactory().addBinding(bindingKey, queue, exch, arguments))
{
Binding oldBinding = virtualHost.getBindingFactory().getBinding(bindingKey, queue, exch, arguments);
Map<String, Object> oldArgs = oldBinding.getArguments();
if((oldArgs == null && !arguments.isEmpty()) || (oldArgs != null && !oldArgs.equals(arguments)))
{
virtualHost.getBindingFactory().replaceBinding(oldBinding.getId(), bindingKey, queue, exch, 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));
}
}