Exemplo n.º 1
0
        public virtual bool onReceive(MessageEnvelope message, ITransport transport)
        {
            if (message.Body.isMessageUserBodySelected() && message.Body.MessageUserBody.QueuePath.ToUpper().Equals(this.QueuePath.ToUpper()))
            {
                lock (consumers)
                {
                    IConsumer <T> consumer = null;
                    if (consumers.ContainsKey(message.Body.MessageUserBody.ConsumerId))
                    {
                        consumer = consumers[message.Body.MessageUserBody.ConsumerId];
                    }

                    if (consumer != null)
                    {
                        Message <T> msg = new Message <T>();
                        try
                        {
                            msg.fillFromEnvelope(message);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }

                        T result = consumer.onMessage(msg);
                        if (msg.Mandatory)
                        {
                            MessageEnvelope deliveryReportMessage = new MessageEnvelope();
                            MessageBody     deliveryReportBody    = new MessageBody();
                            DeliveryReport  deliveryReportData    = new DeliveryReport();
                            deliveryReportMessage.Body = (deliveryReportBody);
                            deliveryReportMessage.Id   = ("/report-for/" + msg.Id);
                            deliveryReportBody.selectDeliveryReport(deliveryReportData);
                            deliveryReportData.ConsumerId = (consumer.Id);
                            deliveryReportData.MessageId  = (msg.Id);
                            deliveryReportData.QueuePath  = (this.queuePath);
                            DeliveredStatus status = new DeliveredStatus();
                            status.Value = DeliveredStatus.EnumType.delivered;
                            deliveryReportData.Status = status;

                            try
                            {
                                transport.sendAsync(deliveryReportMessage);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.ToString());
                            }
                        }

                        if (result != null)
                        {
                            Message <T> resultMsg = new Message <T>();
                            resultMsg.Id        = msg.Id;
                            resultMsg.Body      = result;
                            resultMsg.QueuePath = msg.QueuePath;
                            MessageEnvelope resultMsgEnv;
                            try
                            {
                                resultMsgEnv = resultMsg.createEnvelope();
                                resultMsgEnv.Body.MessageUserBody.ConsumerId = consumer.Id;
                                transport.sendAsync(resultMsgEnv);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.ToString());
                            }
                        }
                    }
                }
                return(true);
            }
            return(false);
        }