我是Rabbitmq的新手,下面只是做生产者 - 消费者实现的是我的生产者,它从API获取数据并推送到队列,所以问题是一段时间一切正常但突然它抛出异常(20-24小时后) )一切都停止了。所以我想要永远地为这个制作人工作它不应该停止产生我们如何实现这一点。
我得到的例外是:
System.IO.EndOfStreamException(" SharedQueue已关闭")
public class Sender
{
static ConnectionFactory factory = new ConnectionFactory();
static Dictionary<string, IModel> _channelMap = new Dictionary<string, IModel>();
static Sender()
{
factory.HostName = "localhost";
}
public static void createChannel(string queue)
{
IConnection connection = factory.CreateConnection();
IModel model = connection.CreateModel();
model.QueueDeclare(queue, true, false, false, null);
_channelMap.Add(queue, model);
}
/// <summary>
/// This method will be called by API it will fetch data from DB and will push it to queue.
/// </summary>
/// <returns></returns>
public void pushToProducer(string queue)
{
IModel channel = null;
string allTIN = string.Empty;
if (_channelMap.ContainsKey(queue))
{
//get model
_channelMap.TryGetValue(queue, out channel);
}
else
{
createChannel(queue);
//get model
_channelMap.TryGetValue(queue, out channel);
}
string url = ConfigurationManager.AppSettings["apiURL"];
string requestFormatStyle = ConfigurationManager.AppSettings["requestFormat"];
RestClient restClientObject = Utility.GetRestClient(url);
RestRequest restRequest = new RestRequest(string.Format(requestFormatStyle), Method.GET);
restRequest.RequestFormat = DataFormat.Json;
while(true)
{
System.Threading.Thread.Sleep(1000);
try
{
string message = string.Empty;
IRestResponse responseOfAPI = restClientObject.Execute(restRequest);
if (responseOfAPI != null)
{
message = (responseOfAPI.Content == null) ? string.Empty : responseOfAPI.Content;
if (!string.IsNullOrWhiteSpace(message))
{
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
channel.BasicPublish("", queue, properties, body);
}
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception Sender " + ex.StackTrace);
}
}
}
}