如何每20秒检查一次到达服务总线的消息
本文关键字:服务 总线 消息 一次 20秒 何每 检查 | 更新日期: 2023-09-27 18:15:10
我使用的是azure的服务总线,它将保存我的消息列表,这意味着消息可以随时进入服务总线。
所以我想在我的服务总线上保持监视,以检查是否有消息在服务中。这就像我只想在我的服务总线上监视消息每隔0f 20秒到达我的服务总线。
在每20秒我想检查消息到达我的服务总线,这我想在后台异步执行。
我想在后台每隔20秒调用下面的方法:
private static void ReceiveMessages()
{
// For PeekLock mode (default) where applications require "at least once" delivery of messages
SubscriptionClient agentSubscriptionClient = SubscriptionClient.Create(TopicName, "AgentSubscription");
BrokeredMessage message = null;
while (true)
{
try
{
//receive messages from Agent Subscription
message = agentSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
Console.WriteLine("'nReceiving message from AgentSubscription...");
Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
// Further custom message processing could go here...
message.Complete();
}
else
{
//no more messages in the subscription
break;
}
}
catch (MessagingException e)
{
if (!e.IsTransient)
{
Console.WriteLine(e.Message);
throw;
}
else
{
HandleTransientErrors(e);
}
}
}
// For ReceiveAndDelete mode, where applications require "best effort" delivery of messages
SubscriptionClient auditSubscriptionClient = SubscriptionClient.Create(TopicName, "AuditSubscription", ReceiveMode.ReceiveAndDelete);
while (true)
{
try
{
message = auditSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
Console.WriteLine("'nReceiving message from AuditSubscription...");
Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
// Further custom message processing could go here...
}
else
{
Console.WriteLine("'nno more messages in the subscription");
//no more messages in the subscription
break;
}
}
catch (MessagingException e)
{
if (!e.IsTransient)
{
Console.WriteLine(e.Message);
throw;
}
}
}
agentSubscriptionClient.Close();
auditSubscriptionClient.Close();
}
有谁能告诉我如何每隔20秒调用上面的方法吗?
最适合初学者的解决方案是:
从工具箱中拖出一个计时器,给它一个名字,设置你想要的间隔,并将"启用"设置为True。然后双击Timer, Visual Studio(或者其他你正在使用的工具)会给你写下面的代码:
private void wait_Tick(object sender, EventArgs e)
{
refreshText(); //add the method you want to call here.
}
或
private Timer timer1;
public void InitTimer()
{
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 2000; // in miliseconds
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
isonline()
}