public void Run() { while (allReceived.Read() < msgCount) { try { Msg msg = sub.NextMessage(500); while (msg != null) { received++; allReceived.Increment(); datas.Add(Encoding.UTF8.GetString(msg.Data)); msg.Ack(); msg = sub.NextMessage(500); } } catch (NATSTimeoutException) { // timeout is acceptable, means no messages available. } } }
private static void _subPullFetch(Context ctx, Stats stats, IJetStreamPullSubscription sub, int id, string counterKey) { int rcvd = 0; Msg lastUnAcked = null; int unAckedCount = 0; int unReported = 0; string label = ctx.GetLabel(id); InterlockedLong counter = ctx.GetSubscribeCounter(counterKey); while (counter.Read() < ctx.MessageCount) { stats.Start(); IList <Msg> list = sub.Fetch(ctx.BatchSize, 500); long hold = stats.Elapsed(); long received = DateTimeOffset.Now.ToUnixTimeMilliseconds(); int lc = list.Count; if (lc > 0) { foreach (Msg m in list) { stats.Count(m, received); counter.Increment(); if ((lastUnAcked = AckMaybe(ctx, stats, m, ++unAckedCount)) == null) { unAckedCount = 0; } } rcvd += lc; unReported = ReportMaybe(label, ctx, rcvd, unReported + lc, "Messages Read"); } AcceptHoldOnceStarted(label, stats, rcvd, hold); } if (lastUnAcked != null) { _ack(stats, lastUnAcked); } Report(label, rcvd, "Finished Reading Messages"); }
private static void SubPush(Context ctx, IConnection c, Stats stats, int id) { IJetStream js = c.CreateJetStreamContext(ctx.GetJetStreamOptions()); IJetStreamPushSyncSubscription sub; if (ctx.Action.IsQueue) { // if we don't do this, multiple threads will try to make the same consumer because // when they start, the consumer does not exist. So force them do it one at a time. lock (QueueLock) { sub = js.PushSubscribeSync(ctx.Subject, ctx.QueueName, ConsumerConfiguration.Builder() .WithAckPolicy(ctx.AckPolicy) .WithAckWait(Duration.OfSeconds(ctx.AckWaitSeconds)) .WithDeliverGroup(ctx.QueueName) .BuildPushSubscribeOptions()); } } else { sub = js.PushSubscribeSync(ctx.Subject, ConsumerConfiguration.Builder() .WithAckPolicy(ctx.AckPolicy) .WithAckWait(Duration.OfSeconds(ctx.AckWaitSeconds)) .BuildPushSubscribeOptions()); } int rcvd = 0; Msg lastUnAcked = null; int unAckedCount = 0; int unReported = 0; string label = ctx.GetLabel(id); InterlockedLong counter = ctx.GetSubscribeCounter(ctx.GetSubDurable(id)); while (counter.Read() < ctx.MessageCount) { try { stats.Start(); Msg m = sub.NextMessage(1000); long hold = stats.Elapsed(); long received = DateTimeOffset.Now.ToUnixTimeMilliseconds(); stats.AcceptHold(hold); stats.Count(m, received); counter.Increment(); if ((lastUnAcked = AckMaybe(ctx, stats, m, ++unAckedCount)) == null) { unAckedCount = 0; } unReported = ReportMaybe(label, ctx, ++rcvd, ++unReported, "Messages Read"); } catch (NATSTimeoutException) { // normal timeout long hold = stats.Elapsed(); AcceptHoldOnceStarted(label, stats, rcvd, hold); } } if (lastUnAcked != null) { _ack(stats, lastUnAcked); } Report(label, rcvd, "Finished Reading Messages"); }