示例#1
0
        /// <summary>
        /// Генерация случайных книг без обработки исключений
        /// </summary>
        private void TryGenerationBook()
        {
            int MilisecondOfSecond = 1000;

            CancelTokenSource = new CancellationTokenSource();
            Token             = CancelTokenSource.Token;
            while (!Token.IsCancellationRequested)
            {
                NameBook = "Book" + Random.Next(MinNumberName, MaxNumberName).ToString();
                Pages    = Random.Next(MinNumberPages, MaxNumberPages);
                Language = (BookSDB.languageEnum)Random.Next(MinNumberLanguage, MaxNumberLanguage);
                Book     = new BookSDB(Language, Pages, NameBook);
                int timeSleep = Random.Next(MinInterval, MaxInterval) * MilisecondOfSecond;
                Thread.Sleep(timeSleep);
                string bookJSON = JsonSerializer.Serialize <BookSDB>(Book);
                WorkRabbit.PublishBookQueue(bookJSON);
            }
            CancelTokenSource = null;
        }
        /// <summary>
        /// Метод случайного добавления книг
        /// </summary>
        /// <param name="minInterval">Минимальное значение интервала добавления</param>
        /// <param name="maxInterval">Максимальное значение интервала добавления</param>
        /// <param name="token">Отменяющий токен</param>
        /// <param name="channel">Канал связи с RabbitMQ</param>
        /// <param name="exchangeName">Имя точки роутинга</param>
        /// <param name="routingKey1">Ключ привязки точки роутинга к очереди</param>
        private static void RandomAddingBooks(int minInterval, int maxInterval, CancellationToken token, IModel channel, string exchangeName, string routingKey1)
        {
            Random random = new Random();
            string name;
            int    pages;

            BookSDB.languageEnum language;
            BookSDB Book;

            while (!token.IsCancellationRequested)
            {
                name     = "Book" + random.Next(1, 100000).ToString();
                pages    = random.Next(5, 1500);
                language = (BookSDB.languageEnum)random.Next(1, 6);
                //содание объекта книги
                Book = new BookSDB(language, pages, name);
                int TimeSleep = random.Next(minInterval, maxInterval) * 1000;
                Thread.Sleep(TimeSleep);
                //сериализация объекта книги в JSON и отправка в очередь
                string BookJSON         = JsonSerializer.Serialize <BookSDB>(Book);
                byte[] messageBodyBytes = Encoding.UTF8.GetBytes(BookJSON);
                channel.BasicPublish(exchangeName, routingKey1, null, messageBodyBytes);
            }
        }