I am new to Rabbitmq and just doing producer-consumer implementation below is my producer which fetches data from API and push to queue, so problem is that for some time everything works fine but suddenly it throws exception (after 20-24 hours) and everything stops working. So I want is to work this producer forever it should not stop producing how we can achieve this.
Exception I get is :
System.IO.EndOfStreamException ("SharedQueue closed")
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);
}
}
}
}