我们在 Windows Azure 上以 Web 角色托管了一个 API,其中有 2 个实例接收请求,验证它们,然后将它们添加到 Azure 服务总线队列。
最近我们开始对此进行负载测试,发现我们当前的代码抛出了以下异常:
无法将命令添加到命令队列异常:Microsoft.ServiceBus.Messaging.QuotaExceededException:已达到或超过队列活动连接数的最大允许连接数:100,最大允许连接数:100。
假设我们维护了该类的单个实例,下面的客户端代码是否会保持单个连接打开?我试图确定问题出在 ServiceBusClient 代码还是我们的依赖注册为每个请求初始化一个新客户端。
public class ServiceBusClient : IDisposable
{
#region Constants and Fields
protected readonly NamespaceManager NamespaceManager;
private const int DEFAULT_LOCK_DURATION_IN_SECONDS = 300; // 5 minutes
private const string SERVICE_BUS_CONNECTION_STRING_KEY
= "service.bus.connection.string";
private readonly IMessageBodySerializer _messageBodySerializer;
private readonly MessagingFactory _messagingFactory;
private readonly QueueClient _queueClient;
private readonly string _queueName;
private readonly ISettingsManager _settingsManager;
#endregion
#region Constructors and Destructors
public ServiceBusClient(
ISettingsManager settingsManager,
IMessageBodySerializer messageBodySerializer, s
tring queueName)
{
_settingsManager = settingsManager;
_messageBodySerializer = messageBodySerializer;
var connectionString = _settingsManager.GetSetting<string>(
SERVICE_BUS_CONNECTION_STRING_KEY);
NamespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
_messagingFactory =
MessagingFactory.CreateFromConnectionString(connectionString);
_queueName = queueName;
_queueClient = GetOrCreateQueue();
}
#endregion
#region Public Methods and Operators
public virtual void SendMessage(object bodyObject)
{
var brokeredMessage =
_messageBodySerializer.SerializeMessageBody(bodyObject);
_queueClient.Send(brokeredMessage);
}
public void Dispose()
{
_messagingFactory.Close();
}
#endregion
#region Methods
private QueueClient GetOrCreateQueue()
{
QueueDescription queue;
if (!NamespaceManager.QueueExists(_queueName))
{
var queueToCreate = new QueueDescription(_queueName)
{
LockDuration = TimeSpan.FromSeconds(
DEFAULT_LOCK_DURATION_IN_SECONDS)
};
queue = NamespaceManager.CreateQueue(queueToCreate);
}
else
{
queue = NamespaceManager.GetQueue(_queueName);
}
return _messagingFactory.CreateQueueClient(
queue.Path,
ReceiveMode.PeekLock);
}
#endregion
}
作为对此的扩展;如果上面的代码确实保持活动连接打开;使用单例模式为 API 的每个实例存储一个 ServiceBusClient 实例会很危险吗?还是 Azure SDK 在内部处理关闭的连接?