Exemplo n.º 1
0
        /// <summary>
        /// The create emitter.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The <see cref="Action"/>.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// </exception>
        private static Action CreateEmitter(int id, CancellationToken cancellationToken)
        {
            return () =>
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        long messageNumber = Interlocked.Increment(ref MessageNumber);
                        try
                        {
                            switch (Params.Type)
                            {
                                case ProducerParams.OneWay:

                                    string label = Params.Type;
                                    Message message = new OneWay(messageNumber);

                                    if (Params.Misbehave)
                                    {
                                        if (Random.Next(10) == 0)
                                        {
                                            label = "bad.msg";
                                        }

                                        if (Random.Next(10) == 0)
                                        {
                                            message = new BadOneWay(messageNumber);
                                        }
                                    }

                                    Console.WriteLine("#{0}: Emitting {1} with label {2}...", id, messageNumber, label);
                                    Bus.Emit(label, message).
                                        ContinueWith(r => Console.WriteLine("#{0}: Received {1} confirmation for {2}.", id, !r.IsFaulted ? "successful" : "failure", messageNumber), cancellationToken);
                                    break;
                                case ProducerParams.Request:

                                    label = Params.Type;
                                    message = new Request(messageNumber);

                                    if (Params.Misbehave)
                                    {
                                        if (Random.Next(10) == 0)
                                        {
                                            label = "bad.msg";
                                        }

                                        if (Random.Next(10) == 0)
                                        {
                                            message = new BadOneWay(messageNumber);
                                        }
                                    }

                                    Console.WriteLine("#{0}: Requesting {1} using label {2}...", id, messageNumber, label);
                                    Bus.RequestAsync<Message, Response>(label, message, new RequestOptions { Timeout = TimeSpan.FromSeconds(5) }).
                                        ContinueWith(
                                            r =>
                                                {
                                                    if (r.IsFaulted)
                                                    {
                                                        Console.WriteLine("#{0}: Timeout while waiting for {1}.", id, messageNumber);
                                                    }
                                                    else if (r.IsCompleted)
                                                    {
                                                        Console.WriteLine("#{0}: Received response {1}.", id, r.Result.Number);
                                                    }
                                                },
                                            cancellationToken);
                                    break;
                                default:
                                    throw new NotSupportedException("Unknown producer type: " + Params.Type);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("#{0}: Caught exception: {1}.", id, ex);
                            Console.ForegroundColor = ConsoleColor.White;
                        }

                        cancellationToken.WaitHandle.WaitOne(Params.Delay);
                    }
                };
        }
Exemplo n.º 2
0
        /// <summary>
        /// The create emitter.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The <see cref="Action"/>.
        /// </returns>
        /// <exception cref="NotSupportedException">
        /// </exception>
        private static Action CreateEmitter(int id, CancellationToken cancellationToken)
        {
            return(() =>
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    long messageNumber = Interlocked.Increment(ref MessageNumber);
                    try
                    {
                        switch (Params.Type)
                        {
                        case ProducerParams.OneWay:

                            string label = Params.Type;
                            Message message = new OneWay(messageNumber);

                            if (Params.Misbehave)
                            {
                                if (Random.Next(10) == 0)
                                {
                                    label = "bad.msg";
                                }

                                if (Random.Next(10) == 0)
                                {
                                    message = new BadOneWay(messageNumber);
                                }
                            }

                            Console.WriteLine("#{0}: Emitting {1} with label {2}...", id, messageNumber, label);
                            Bus.Emit(label, message).
                            ContinueWith(r => Console.WriteLine("#{0}: Received {1} confirmation for {2}.", id, !r.IsFaulted ? "successful" : "failure", messageNumber), cancellationToken);
                            break;

                        case ProducerParams.Request:

                            label = Params.Type;
                            message = new Request(messageNumber);

                            if (Params.Misbehave)
                            {
                                if (Random.Next(10) == 0)
                                {
                                    label = "bad.msg";
                                }

                                if (Random.Next(10) == 0)
                                {
                                    message = new BadOneWay(messageNumber);
                                }
                            }

                            Console.WriteLine("#{0}: Requesting {1} using label {2}...", id, messageNumber, label);
                            Bus.RequestAsync <Message, Response>(label, message, new RequestOptions {
                                Timeout = TimeSpan.FromSeconds(5)
                            }).
                            ContinueWith(
                                r =>
                            {
                                if (r.IsFaulted)
                                {
                                    Console.WriteLine("#{0}: Timeout while waiting for {1}.", id, messageNumber);
                                }
                                else if (r.IsCompleted)
                                {
                                    Console.WriteLine("#{0}: Received response {1}.", id, r.Result.Number);
                                }
                            },
                                cancellationToken);
                            break;

                        default:
                            throw new NotSupportedException("Unknown producer type: " + Params.Type);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("#{0}: Caught exception: {1}.", id, ex);
                        Console.ForegroundColor = ConsoleColor.White;
                    }

                    cancellationToken.WaitHandle.WaitOne(Params.Delay);
                }
            });
        }