none
How to Delete or Clear a Message Queue to Web Service RRS feed

  • Question

  • I have a problem it's about queue message, I'm consuming a WebService, and using a Insertion Method, each 5 minutes I use this method, so, the problem is when the Connection to Internet does not work correctly.

    When the connection is out, all the message are in stand by, so, when the connection works fine, the first message that had tried to send is sent, then that I want is when this happen, erase all those queue messages and send a new message

    Well, how it works, each 5 minutes I search some information, for example, I have 5 clients, then each customer has its owner information, so I search the first customer, and sent the information, this is for each one, the information of all customers are sent each 5 min, then all this messages are queued

        public static void OnTimeEvent(object source, ElapsedEventArgs e)
        {
            ConnectionResponse Connection = new ConnectionResponse();
            Service Web = new Service();
            HttpResponseMessage Response = new HttpResponseMessage();
    
            Console.WriteLine("Iniciando Proceso... ");
            showBalloon("Procesando", "Iniciando Proceso");
            Console.WriteLine("\n");
    
            object velO;
            object velOB;
    
            String JSon;
            String Id;
    
            String UrlPlaca;
            String UrlBoton;
            String UrlEvent;
    
            int i = 0;
            int x = 0;
    
            DateTime FechaIni;
            DateTime FechaFin;
            DateTime FechaIniE;
            DateTime FechaFinE;
            String Format = "yyyyMMddTHHmmss";
    
            try
            {
                Response = Connection.response(GloablVariable.User, GloablVariable.Pass, "http://api.site.com/api/v2/vehicles.json");
    
                if (Response.IsSuccessStatusCode)
                {
                    JSon = Response.Content.ReadAsStringAsync().Result;
                    var ArrayId = (JObject)JsonConvert.DeserializeObject(JSon);
    
                    var ResultId = ArrayId["Items"].Select(item => new
                    {
                        Id = item["Id"],
                    }).ToList();
    
                    foreach (var Identificator in ResultId)
                    {
                        Id = Identificator.Id.ToString();
                        UrlPlaca = "http://api.site.com/api/v2/vehicle/";
                        UrlPlaca += Id;
                        UrlPlaca += "/vehicle.json";
    
                        HttpResponseMessage ResponseP = new HttpResponseMessage();
                        ResponseP = Connection.response(GloablVariable.User, GloablVariable.Pass, UrlPlaca);
    
                        if (ResponseP.IsSuccessStatusCode)
                        {
                            String JSonP = ResponseP.Content.ReadAsStringAsync().Result;
                            var ArrayPlaca = (JObject)JsonConvert.DeserializeObject(JSonP);
                            GlobalVariable.Placa = ArrayPlaca["Registration"].ToString();
                        }
    
                        FechaFin = DateTime.Now;
                        FechaIni = DateTime.Now.AddMinutes(-5);
    
                        String FIni = FechaIni.ToString(Format);
                        String FFin = FechaFin.ToString(Format);
    
                        UrlBoton = "http://api.site.com/api/v2/vehicles/";
                        UrlBoton += Id;
                        UrlBoton += "/eventtypes/132/events/";
                        UrlBoton += FFin + "/";
                        UrlBoton += FIni + ".json";
    
                        HttpResponseMessage ResponseB = new HttpResponseMessage();
    
                        ResponseB = Connection.response(GloablVariable.User, GloablVariable.Pass, UrlBoton);
    
                        if (ResponseB.IsSuccessStatusCode)
                        {
                            String JSonB = ResponseB.Content.ReadAsStringAsync().Result;
                            var ArrayBoton = (JObject)JsonConvert.DeserializeObject(JSonB);
    
                            var ResultBoton = ArrayBoton["Items"].Select(evento => new
                            {
                                Id = evento["Id"],
                                UnitId = evento["UnitId"],
                                EventTypeId = evento["EventTypeId"],
                                EventTypeDescription = evento["EventTypeDescription"],
                                LocalTimestamp = evento["LocalTimestamp"],
                                Odometer = evento["Odometer"],
                                Position = evento["Position"][0],
                                Position1 = evento["Position"][1],
                                Speed = evento["Speed"],
                                UnitOfDistanceCode = evento["UnitOfDistanceCode"],
                            }).ToList();
    
                            i = 0;
                            foreach (var Botono in ResultBoton)
                                i = i + 1;
    
                            if (i > 0)
                            {
                                i = i - 1;
                                GlobalVariable.lonB = Convert.ToDouble(ResultBoton[i].Position.ToString());
                                GlobalVariable.fchEvntoB = Convert.ToDateTime(ResultBoton[i].LocalTimestamp.ToString());
                                GlobalVariable.even = ResultBoton[i].EventTypeId.ToString();
                                velOB = ResultBoton[i].Speed;
                                String velocidadBoton;
                                velocidadBoton = velOB.ToString();
                                GlobalVariable.fchEmB = DateTime.Now;
                                if (String.IsNullOrEmpty(velocidadBoton))
                                    GlobalVariable.velB = 0;
                                else
                                    GlobalVariable.velB = Convert.ToDouble(velOB.ToString());
                                i = i + 1;
                            }
                        }
    
                        HttpResponseMessage ResponseE = new HttpResponseMessage();
    
                        FechaFinE = DateTime.Now.AddDays(-7);
                        FechaIniE = DateTime.Now;
    
                        String FchIni = FechaIniE.ToString(Format);
                        String FchFin = FechaFinE.ToString(Format);
    
                        UrlEvent = "http://api.site.com/api/v2/vehicles/";
                        UrlEvent += Id;
                        UrlEvent += "/events/";
                        UrlEvent += FchFin + "/";
                        UrlEvent += FchIni + ".json";
    
                        ResponseE = Connection.response(GloablVariable.User, GloablVariable.Pass, UrlEvent);
    
                        if (ResponseE.IsSuccessStatusCode)
                        {
                            try
                            {
                                String JSonEvent = ResponseE.Content.ReadAsStringAsync().Result;
                                var ArrayEvent = (JObject)JsonConvert.DeserializeObject(JSonEvent);
    
                                var ResultEvent = ArrayEvent["Items"].Select(events => new
                                {
                                    Id = events["Id"],
                                    UnitId = events["UnitId"],
                                    EventTypeId = events["EventTypeId"],
                                    EventTypeDescription = events["EventTypeDescription"],
                                    LocalTimestamp = events["LocalTimestamp"],
                                    Odometer = events["Odometer"],
                                    Position = events["Position"][0],
                                    Position1 = events["Position"][1],
                                    Speed = events["Speed"],
                                    UnitOfDistanceCode = events["UnitOfDistanceCode"],
                                }).ToList();
    
                                x = 0;
                                foreach (var Events in ResultEvent)
                                    x = x + 1;
    
                                if (x > 0)
                                {
                                    x = x - 1;
    
                                    GlobalVariable.even1 = ResultEvent[x].Id.ToString() + "-" + DateTime.Now.ToString("HHmmss");
                                    GlobalVariable.lat = Convert.ToDouble(ResultEvent[x].Position1.ToString());
                                    GlobalVariable.lon = Convert.ToDouble(ResultEvent[x].Position.ToString());
                                    GlobalVariable.fchEvnto = Convert.ToDateTime(ResultEvent[x].LocalTimestamp.ToString());
                                    velO = ResultEvent[x].Speed;
                                    GlobalVariable.fchEm = DateTime.Now;
                                    String velocidadBoton;
                                    velocidadBoton = velO.ToString();
                                    if (String.IsNullOrEmpty(velocidadBoton) || velocidadBoton == "0")
                                        GlobalVariable.vel = 0;
                                    else
                                        GlobalVariable.vel = Convert.ToDouble(velO.ToString());
    
                                }
    
                            }
                            catch
                            {
                                Console.WriteLine("Se detectó un error, Favor de verificar el dispositivo: " + GlobalVariable.Placa);
                            }
                        }
    
                        if (i > 0)
                        {
                            Console.WriteLine("--------------------------------");
                            Console.WriteLine("Código: " + GlobalVariable.even);
                            Console.WriteLine("Placa: " + GlobalVariable.Placa);
                            Console.Write("Folio de Respuesta: ");
                            Console.WriteLine(Web.LoginYInsertarEvento("User", "Pass", GlobalVariable.Placa, "", GlobalVariable.even, GlobalVariable.latB, GlobalVariable.lonB, 0, GlobalVariable.velB, GlobalVariable.fchEvntoB, GlobalVariable.fchEmB));
                            Console.WriteLine("\n");
                        }
                        else
                        {
                            Console.WriteLine("--------------------------------");
                            Console.WriteLine("Código: " + GlobalVariable.even1);
                            Console.WriteLine("Placa: " + GlobalVariable.Placa);
                            Console.Write("Folio de Respuesta: ");
                            Console.WriteLine(Web.LoginYInsertarEvento("User", "Pass", GlobalVariable.Placa, "", GlobalVariable.even1, GlobalVariable.lat, GlobalVariable.lon, 0, GlobalVariable.vel, GlobalVariable.fchEvnto, GlobalVariable.fchEm));
                            Console.WriteLine("\n");
                        }
    
                    }
                }
                else
                {
                    Console.Write("Tu credencial ha caducado... ");
                    showBalloon("Credencial Caducada", "Tu credencial ha caducado");
                    System.Threading.Thread.Sleep(3000);
                    Environment.Exit(0);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err);
                showBalloon("Error", "Ha ocurrido un Error");
            }
    
        }

    The Web Service does not belong to me

    There is a way to clear that queue?

    I developed in C#

    in advance thank you very much, and have a great day

    Tuesday, August 4, 2015 3:17 PM