Esempio n. 1
0
        protected override async Task ExecuteCoreAsync(CancellationToken stoppingToken)
        {
            var random = new Random();

            while (!stoppingToken.IsCancellationRequested)
            {
                WriteLine($"{nameof(ConsumerService)}: Dequeue(before)");
                var bytes = await _queue.DequeueAsync(stoppingToken);

                WriteLine($"{nameof(ConsumerService)}: Dequeue(after) {HexHelper.ToString(bytes)}");

                // ランダムな時間待機する(何か処理する想定)
                var sec = random.Next(1, 3);
                WriteLine($"{nameof(ConsumerService)}: Wait {sec}s");
                await Task.Delay(TimeSpan.FromSeconds(sec), stoppingToken);
            }
        }
Esempio n. 2
0
        protected override async Task ExecuteCoreAsync(CancellationToken stoppingToken)
        {
            var random = new Random();

            while (!stoppingToken.IsCancellationRequested)
            {
                // ランダムな時間待機する(ランダムに要求が発生する想定)
                var sec = random.Next(1, 5);
                WriteLine($"{nameof(ProducerService)}: Wait {sec}s");
                await Task.Delay(TimeSpan.FromSeconds(sec), stoppingToken);

                // ランダムな値をキューに追加する
                var bytes = new byte[5];
                random.NextBytes(bytes);

                WriteLine($"{nameof(ProducerService)}: Enqueue(before) {HexHelper.ToString(bytes)}");
                _queue.Enqueue(bytes);
                WriteLine($"{nameof(ProducerService)}: Enqueue(after) {HexHelper.ToString(bytes)}");
            }
        }