Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="evtCache"></param>
        private void flushEventCache(EventoHttpLivroOfertas evtCache)
        {
            string mensagem = null;

            try
            {
                mensagem = JsonConvert.SerializeObject(evtCache);
                mensagem = MDSUtils.montaMensagemHttp(ConstantesMDS.TIPO_REQUISICAO_LIVRO_OFERTAS, evtCache.instrumento, null, mensagem);

                bool bsinaliza = false;
                lock (objLockSnapshot)
                {
                    if (!String.IsNullOrEmpty(mensagem))
                    {
                        bsinaliza = queueToStreamer.IsEmpty;
                        queueToStreamer.Enqueue(mensagem);
                    }
                }

                if (bsinaliza)
                {
                    lock (syncQueueToStreamer)
                    {
                        Monitor.Pulse(syncQueueToStreamer);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("flushEventCache(): " + ex.Message, ex);
            }
        }
Example #2
0
 public void SendEvent(EventoHttpLivroOfertas e)
 {
     try
     {
         //bool bsinaliza = queueHttpLOF.IsEmpty;
         queueHttpLOF.Enqueue(e);
         //if (bsinaliza)
         //{
         //    lock (syncQueueHttpLOF)
         //    {
         //        Monitor.Pulse(syncQueueHttpLOF);
         //    }
         //}
     }
     catch (Exception ex)
     {
         logger.Error("SendEvent(EventoHttpLivroOfertas): " + ex.Message, ex);
     }
 }
Example #3
0
        private void despacharEventosLOF(object sender, HttpLivroOfertasEventArgs args)
        {
            try
            {
                EventoHttpLivroOfertas httpLOF = args.Evento;

                if (dctSessions.ContainsKey(httpLOF.instrumento))
                {
                    bool bsinaliza = queueEventosLOF.IsEmpty;
                    queueEventosLOF.Enqueue(httpLOF);
                    if (bsinaliza)
                    {
                        lock (syncQueueEventosLOF)
                        {
                            Monitor.Pulse(syncQueueEventosLOF);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("despacharEventosLOF(): " + ex.Message, ex);
            }
        }
Example #4
0
        private void otimizadorEventos()
        {
            logger.Info("Iniciando otimizacao de eventos");
            long   lastLogTicks         = 0;
            long   lastOtimizationTicks = 0;
            string lastInstrumento      = null;

            EventoHttpLivroOfertas evtCache = null;

            while (bKeepRunning)
            {
                try
                {
                    EventoHttpLivroOfertas evento;
                    if (queueEventosLOF.TryDequeue(out evento))
                    {
                        // Se houver cache, descarrega
                        if (!String.IsNullOrEmpty(lastInstrumento))
                        {
                            if (!lastInstrumento.Equals(evento.instrumento))
                            {
                                flushEventCache(evtCache);
                                lastInstrumento = null;
                                evtCache        = null;
                            }
                        }

                        // Se cache vazio, cria um novo cache
                        if (String.IsNullOrEmpty(lastInstrumento))
                        {
                            lastInstrumento      = evento.instrumento;
                            evtCache             = new EventoHttpLivroOfertas();
                            evtCache.cabecalho   = evento.cabecalho;
                            evtCache.instrumento = evento.instrumento;
                            evtCache.livroCompra = new List <Dictionary <string, string> >();
                            evtCache.livroVenda  = new List <Dictionary <string, string> >();
                        }

                        // Se for igual, acrescenta as operacoes de livro no final
                        if (lastInstrumento.Equals(evento.instrumento))
                        {
                            if (evento.livroCompra != null && evento.livroCompra.Count > 0)
                            {
                                evtCache.livroCompra.AddRange(evento.livroCompra);
                            }

                            if (evento.livroVenda != null && evento.livroVenda.Count > 0)
                            {
                                evtCache.livroVenda.AddRange(evento.livroVenda);
                            }
                        }

                        if (MDSUtils.shouldLog(lastLogTicks))
                        {
                            lastLogTicks = DateTime.UtcNow.Ticks;
                            logger.Info("Fila de eventos a serem otimizados: " + queueEventosLOF.Count);
                        }

                        continue;
                    }

                    // Se a fila estiver vazia e estourar o timeout, descarrega o cache
                    //if (!String.IsNullOrEmpty(lastInstrumento) && (DateTime.UtcNow.Ticks - lastOtimizationTicks) > TimeSpan.TicksPerMillisecond)
                    if (!String.IsNullOrEmpty(lastInstrumento))
                    {
                        flushEventCache(evtCache);
                        lastInstrumento = null;
                        evtCache        = null;

                        continue;
                    }

                    lock (syncQueueEventosLOF)
                    {
                        Monitor.Wait(syncQueueEventosLOF, 50);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("otimizadorEventos(): " + ex.Message, ex);
                }
            }
        }