我們在Windows Azure上以Web角色託管了一個API,其中有兩個實例接受請求,驗證它們並將它們添加到Azure服務總線隊列中。在Azure服務總線中重用連接
最近,我們已經開始負載測試,發現這個是我們目前的代碼引發以下異常:
不能添加命令,命令隊列 異常: Microsoft.ServiceBus.Messaging.QuotaExceededException: maximumallowed連接的數目已經達到或超過隊列 活動連接數:100,最大允許連接:100
將在下面客戶碼k假設我們維護一個類的單個實例,是否打開單個連接?我試圖確定問題是否出現在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在內部處理關閉的連接?
謝謝你的回覆阿布舍克;這正是我們需要知道的! –
我的問題與這個問題有關,如果你可以看看,不好意思:http://stackoverflow.com/questions/33512803/service-bus-singleton-connection-class @LukeMerrett – Ron
MessagingFactory在Microsoft中不可用。 Azure.EventHubs庫。您是否知道在創建eventhub客戶端時是否有其他方法可以嘗試重新使用基礎TCP連接,因爲它不可用? – Tany