简体   繁体   English

发生错误后如何保持频道正常运行?

[英]How can I keep a Channel alive after an error?

I want to delete several queues on our RabbitMQ server and have some code that looks like this: 我想删除RabbitMQ服务器上的几个队列,并提供一些类似于以下代码的代码:

string[] queuesToDelete = new[] {
  "QueueThatExists1",
  "QueueThatDoesn'tExist", // this queue causes an error - which I expect
  "QueueThatExists2" };    // this queue also errors - which I don't expect

IConnectionFactory factory = ...
using (IModel = factory.CreateModel()) {
  foreach (string queue in queuesToDelete) {
    try {
      model.QueueDelete(queue);
      Console.WriteLine("Queue {0} deleted");
    } catch (Exception e) {
      Console.WriteLine("Queue {0} could not be deleted because {1}", queue, e);
    }
  }
}

However I get this as output: 但是我得到这个作为输出:

Queue QueueThatExists1 deleted 队列QueueThatExists1已删除
Queue QueueThatDoesn'tExist could not be deleted because Queue Not Found 由于未找到队列,无法删除队列QueueThatDoesn'tExist
Queue QueueThatExists2 could not be deleted because Already Closed 无法删除队列QueueThatExists2,因为已经关闭

I have changed the code to look more like this (which works as I expect): 我已经更改了代码,使其看起来更像这样(按我的预期工作):

string[] queuesToDelete = new[] {
  "QueueThatExists1",
  "QueueThatDoesn'tExist", // this queue causes an error - which I expect
  "QueueThatExists2" };    // this queue also errors - which I don't expect

IConnectionFactory factory = ...
IModel model;
try {
  model = factory.CreateModel();
  foreach (string queue in queuesToDelete) {
    try {
      model.QueueDelete(queue);
      Console.WriteLine("Queue {0} deleted");
    } catch (Exception e) {
      Console.WriteLine("Queue {0} could not be deleted because {1}", queue, e);
      // reset the connection
      model.Dispose();
      model = factory.CreateModel();
    }
  } finally {
    if (model != null)
      model.Dispose();
  }
}

However this looks bad. 但是,这看起来很糟糕。 I have removed a using statement and hand rolled the same thing with a try - finally block. 我已经删除了一条using语句,并try - finally了相同的try - finally阻止。 It feels like I am fighting the API. 感觉就像我在打API。 Question: Is there a more elegant way of achieving the same result? 问题:是否有更优雅的方法来达到相同的结果?

I notice that RabbitMQ for java has lyra and autorecovery , but cannot find anything similar for C#. 我注意到Java的RabbitMQ具有lyraautorecovery ,但是找不到与C#类似的东西。

Check out project EasyNetQ . 出项目EasyNetQ

EasyNetQ implement subscriber reconnection ( EasyNetQ doc ). EasyNetQ实现订户重新连接( EasyNetQ doc )。

Instead of a try/catch I recommend checking if that you're interested in exists. 我建议您检查一下是否存在,而不是尝试/捕获。 You can do this with the API. 您可以使用API​​进行此操作。 Here is our method to do that: 这是我们的方法:

  private bool DoesSomethingExist(string something, string queueOrExchange)
    {
        var connectionInfo = GetRabbitConnectionInfo();
        var url = string.Format("{0}/{1}/{2}/{3}", connectionInfo.APIUrl, queueOrExchange, connectionInfo.VirtualHostName, something);
        using (var client = new HttpClient())
        {
            var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", connectionInfo.UserName, connectionInfo.Password));
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            var response = client.GetAsync(url).Result;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                return true;
            }
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return false;
            }

            var content = response.Content;
            throw new Exception(string.Format("Unhandled API response code of {0}, content: {1}", response.StatusCode, content));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关闭连接后,我可以保持SqlDataReader“活着”吗? - Can I keep a SqlDataReader “alive” after closing connection? 如何确定Connection:keep-alive是否正常工作? - How can I tell if Connection: keep-alive is working? 我如何使用httpclient也可以创建一个功能,即使关闭应用程序,该功能也可以使用户登录保持活动状态? - how can i use httpclient too create a function that will keep the user login alive even after the app shut done? 异常后如何使ReactiveCommand保持活动状态? - How to keep ReactiveCommand alive after exception? 如何使用计时器使程序保持活动状态? - How do I keep a program alive with timers? 订阅Redis频道无法继续进行 - subscription to redis channel does not keep alive 在调用它的方法退出后,如何使任务保持活动状态? - How do I keep a task alive after the method that called it has exited? 如何在ASP.NET Web服务客户端请求上禁用keep-alive? - How can I disable keep-alive on ASP.NET Web Service client requests? 从虹膜设备读取数据时如何保持我的状态活跃? - How can I keep my state alive while reading from an iris device? 如何使用无限循环或控制台读取键的替代方法使应用程序保持活动状态? (如果可能的话) - How can I keep an application alive with alternative to infinite loop or console read key? (if possible)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM