public void Start(QueueReaderSettings config, ActiveQueueContext context) { _client = Gerakul.SqlQueue.InMemory.QueueClient .Create(config.ConnectionString, config.QueueName); var subscriptionId = _client.FindSubscription(SqlQueueSubscription); if (subscriptionId == 0) { _client.CreateSubscription(SqlQueueSubscription); } _reader = _client.CreateAutoReader(SqlQueueSubscription); _reader.Start((data) => Task.Factory.StartNew(() => { try { var messages = data.Select(msg => Event.Parser.ParseFrom((byte[])msg.Body)); context.OnReceivedMessages(messages); } catch (Exception ex) { context.OnError($"MessagePump.OnMessage SqlQueue Parse Error: {ex.Message}"); } })).Wait(); }
public void Start(QueueReaderSettings config, ActiveQueueContext context) { var connectionString = config.ConnectionString.FetchConnectionSslOptions(out var configure); _client = RabbitHutch .CreateBus(x => { var connectionConfig = x.Resolve <IConnectionStringParser>().Parse(connectionString); return(configure(connectionConfig)); }, x => { }) .Advanced; var queue = _client.QueueDeclare(config.QueueName); _client.Consume(queue, (body, properties, info) => Task.Factory.StartNew(() => { try { var msg = Event.Parser.ParseFrom(body); context.OnReceivedMessage(msg); } catch (Exception ex) { context.OnError($"MessagePump.OnMessage RabbitMQ Parse Error: {ex.Message}"); } })); }
public void OnStart() { // Create context for ActiveQueues var context = new ActiveQueueContext { OnError = (errorMessage) => { _monik.ApplicationError(errorMessage); Thread.Sleep(DelayOnException); }, OnVerbose = (verboseMessage) => { _monik.ApplicationVerbose(verboseMessage); }, OnMeasure = (metricName, value) => { _monik.Measure(metricName, AggregationType.Gauge, value); _monik.Measure(metricName + "All", AggregationType.Accumulator, value); }, OnReceivedMessage = (msg) => { _msgBuffer.Enqueue(msg); _newMessageEvent.Set(); }, OnReceivedMessages = (messages) => { foreach (var msg in messages) { _msgBuffer.Enqueue(msg); } _newMessageEvent.Set(); } }; if (_queueReaderSettings != null) { foreach (var it in _queueReaderSettings) { try { var queue = CreateActiveQueueByType(it.Type); if (queue != null) { _queues.Add(queue); queue.Start(it, context); } else { _monik.ApplicationWarning( $"MessagePump.OnStart cannot initialize {it.Name}: unknown type {it.Type}"); } } catch (Exception ex) { _monik.ApplicationError($"MessagePump.OnStart failed initialization {it.Name}: {ex.Message}"); } } //configure all event sources } // Start message processing _pumpTask = Task.Run(OnProcessTask); _monik.ApplicationVerbose("MessagePump started"); }
public void Start(QueueReaderSettings config, ActiveQueueContext context) { _receiver = new MessageReceiver( new ServiceBusConnection(config.ConnectionString), config.QueueName, prefetchCount: PrefetchCount ); _receiverTokenSource = new CancellationTokenSource(); var completeTime = DateTime.UtcNow; _receiverTask = Task.Run(async() => { while (!_receiverTokenSource.IsCancellationRequested) { var receiveTime = DateTime.UtcNow; context.OnMeasure("AzureCTime", (receiveTime - completeTime).TotalMilliseconds); IList <Message> messages; try { messages = await _receiver.ReceiveAsync(MaxMessageCount); if (messages == null || messages.Count == 0) { continue; } } catch (Exception ex) { context.OnError($"AzureActiveQueue - exception received: {ex}"); try { await Task.Delay(TimeoutOnException, _receiverTokenSource.Token); } catch (OperationCanceledException) { // ignore } continue; } var processTime = DateTime.UtcNow; context.OnMeasure("AzureRTime", (processTime - receiveTime).TotalMilliseconds); context.OnMeasure("AzureBSize", messages.Count); var lockTokens = messages.Select(x => x.SystemProperties.LockToken); var completeMessagesTask = _receiver.CompleteAsync(lockTokens); foreach (var message in messages) { Event msg = null; try { msg = Event.Parser.ParseFrom(message.Body); } catch (Exception) { // ToDo: remove temporal fallback try { var buf = message.GetBody <byte[]>(); msg = Event.Parser.ParseFrom(buf); } catch (Exception ex) { context.OnError($"AzureActiveQueue - not able to handle message: {ex}"); } if (msg != null) { var curDate = DateTime.UtcNow; var name = $"{msg.Source}::{msg.Instance}"; if (!_fallbacks.TryGetValue(name, out var date) || (curDate - date).TotalMinutes > 30) { context.OnVerbose($"[AzureActiveQueue] {name} used obsolete xml serialized Event"); _fallbacks[name] = curDate; } } } if (msg != null) { context.OnReceivedMessage(msg); } } completeTime = DateTime.UtcNow; context.OnMeasure("AzurePTime", (completeTime - processTime).TotalMilliseconds); try { await completeMessagesTask; } catch (Exception ex) { context.OnError($"Failed to complete messages: {ex}"); } } }); }