protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Console.WriteLine("Customer Approval Service Started");


            var consumerHelper  = new ConsumerHandler(_consumerConfig, "customers_not_validated");
            var customerMessage = consumerHelper.ReadMessage(stoppingToken);


            //Deserilaize
            Customer customer = JsonConvert.DeserializeObject <Customer>(customerMessage.Value);

            //TODO:: Validate Customer Approval
            Console.WriteLine($"Info: CustomerHandler => Verifying customer {customer.Name}");
            if (customer.Id == 10)
            {
                throw new Exception("Teste");
            }
            // Customer Verified
            customer.Status = Status.APPROVED;

            //Write to ReadyToShip Queue

            var producerHandler = new ProducerHandler(_producerConfig, "customers");
            await producerHandler.WriteMessage(JsonConvert.SerializeObject(customer));
        }
Example #2
0
 public void RabbitBuild()
 {
     if (_consumerExecutorDescriptor.Count == 0)
     {
         return;
     }
     //注册Rabbit
     Task.Factory.StartNew(() =>
     {
         _consumer = new ConsumerHandler(_consumerExecutorDescriptor);
         _consumer.Start();
     });
 }
        /// <summary>
        /// Method for adding a function to receive messages from the given topic
        /// </summary>
        /// <typeparam name="T">The type of the message, inherited from <see cref="Avro.Specific.ISpecificRecord"/></typeparam>
        /// <param name="handler">Delegate function to be called once a message is received</param>
        /// <param name="topic">The name of the topic to listen to</param>
        public void AddCallback <T>(ConsumerHandler <T> handler, string topic)
            where T : Avro.Specific.ISpecificRecord
        {
            // Make sure we are not requested to listen to core topics via this method
            if (Configuration.CoreTopics.ContainsKey(topic))
            {
                throw new CommunicationException($"you are not able to listen to ({topic}), since it is part of the core test-bed topics");
            }

            if (_consumers.ContainsKey(topic))
            {
                // Check if the types are matching and add a new consumer if they are
                if (_consumers[topic][0].MessageType != typeof(T))
                {
                    throw new CommunicationException($"could not create consumer type {typeof(T)}, since it is not conform the initial consumer message type {_consumers[topic][0].MessageType}");
                }
            }
            else
            {
                foreach (KeyValuePair <Type, string> kvp in Configuration.StandardTopics)
                {
                    if (kvp.Value == topic && kvp.Key != typeof(T))
                    {
                        throw new CommunicationException($"could not create consumer type {typeof(T)} for stadard topic ({topic}), since it is not conform the initial standard message type {kvp.Key}");
                    }
                }
            }

            // Create a new consumer listening to the given topic
            AbstractConsumer <T> newConsumer = new AbstractConsumer <T>(_configuration, handler, topic, _cancellationTokenSource);

            newConsumer.OnError += Adapter_Error;
            newConsumer.OnLog   += Adapter_Log;
            if (_consumers.ContainsKey(topic))
            {
                _consumers[topic].Add(newConsumer);
            }
            else
            {
                _consumers.Add(topic, new List <IAbstractConsumer>()
                {
                    newConsumer
                });
            }
        }
    static void Main(string[] args)
    {
        //no external refs
        {
            foo f = new foo();
            f = null;
            Check0();
        }

        //one external ref
        {
            foo f = new foo();

            System.IntPtr p = ConsumerHandler.Instance.AddReference(f);

            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p));
            f = null;
            Check0();
        }

        //one external ref
        {
            foo f = new foo();

            System.IntPtr p = ConsumerHandler.Instance.AddReference(f);
            f = null;
            Check1();
            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p));
            Check0();
        }

        //one external ref
        {
            foo f = new foo();

            System.IntPtr p = ConsumerHandler.Instance.AddReference(f);
            Safir.Dob.Internal.ConsumerBase cb = ConsumerHandler.ToConsumer(p);
            cb = ConsumerHandler.ToConsumer(p);
            cb = ConsumerHandler.ToConsumer(p);
            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p));
            f = null;
            Check1();
            cb = null;
            Check0();
        }

        //two external ref
        {
            foo f = new foo();

            System.IntPtr p1 = ConsumerHandler.Instance.AddReference(f);
            System.IntPtr p2 = ConsumerHandler.Instance.AddReference(f);
            f = null;

            if (p1 != p2)
            {
                Console.WriteLine("Consumer address should be the same!");
            }

            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p1));
            Check1();
            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p2));
            Check0();
        }

        //two objects
        {
            foo f1 = new foo();
            foo f2 = new foo();

            System.IntPtr p1 = ConsumerHandler.Instance.AddReference(f1);
            System.IntPtr p2 = ConsumerHandler.Instance.AddReference(f2);
            f1 = null;
            f2 = null;

            if (p1 == p2)
            {
                Console.WriteLine("Consumer address should be different!");
            }

            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p1));
            ConsumerHandler.Instance.DropReference(ConsumerHandler.ToConsumer(p2));
            Check0();
        }

        //one external ref

        /*
         * {
         *  foo f = new foo();
         *
         *  System.IntPtr p1 = ConsumerHandler.Instance.AddReference(f);
         *  System.IntPtr p2 = ConsumerHandler.Instance.AddReference(f);
         *
         *  System.Console.WriteLine("p1 = " + p1);
         *  System.Console.WriteLine("p2 = " + p2);
         *
         *  ConsumerHandler.Instance.DropReference(p1);
         *  ConsumerHandler.Instance.DropReference(p2);
         *  f = null;
         *  Check0();
         *
         * }*/
    }