/// <summary> /// /// </summary> /// <param name="evtCache"></param> private void flushEventCache(EventoHttpLivroOfertasAgregado evtCache) { string mensagem = null; try { mensagem = JsonConvert.SerializeObject(evtCache); mensagem = MDSUtils.montaMensagemHttp(ConstantesMDS.TIPO_REQUISICAO_LIVRO_OFERTAS_AGREGADO, 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); } }
public void SendEvent(EventoHttpLivroOfertasAgregado e) { try { //bool bsinaliza = queueHttpLOA.IsEmpty; queueHttpLOA.Enqueue(e); //if (bsinaliza) //{ // lock (syncQueueHttpLOA) // { // Monitor.Pulse(syncQueueHttpLOA); // } //} } catch (Exception ex) { logger.Error("SendEvent(EventoHttpLivroOfertasAgregado): " + ex.Message, ex); } }
private void despacharEventosLOA(object sender, HttpLivroOfertasAgregadoEventArgs args) { try { EventoHttpLivroOfertasAgregado httpLOA = args.Evento; if (dctSessions.ContainsKey(httpLOA.instrumento)) { bool sinaliza = queueEventosLOA.IsEmpty; queueEventosLOA.Enqueue(httpLOA); if (sinaliza) { lock (syncQueueEventosLOA) { Monitor.Pulse(syncQueueEventosLOA); } } } } catch (Exception ex) { logger.Error("despacharEventosLOA(): " + ex.Message, ex); } }
private void otimizadorEventos() { logger.Info("Iniciando otimizacao de eventos"); long lastLogTicks = 0; long lastOtimizationTicks = 0; string lastInstrumento = null; EventoHttpLivroOfertasAgregado evtCache = null; while (bKeepRunning) { try { EventoHttpLivroOfertasAgregado evento; if (queueEventosLOA.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 EventoHttpLivroOfertasAgregado(); 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: " + queueEventosLOA.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 (syncQueueEventosLOA) { Monitor.Wait(syncQueueEventosLOA, 50); } } catch (Exception ex) { logger.Error("otimizadorEventos(): " + ex.Message, ex); } } }