public BaseResponse Publish(InserLogRequest request)
        {
            MQConstants.MQ_QUEUE_NAME = request.queuName;
            BaseResponse     baseResponse     = new BaseResponse();
            PublisherRequest publisherRequest = new PublisherRequest {
                //find project id
            };

            try
            {
                using (var connection = _rabbitMQService.GetRabbitMQConnection())
                {
                    using (var chanel = connection.CreateModel())
                    {
                        chanel.QueueDeclare(queue: request.queuName,
                                            durable: MQConstants.MQ_QUEUE_DURABLE,
                                            exclusive: false,
                                            autoDelete: false,
                                            arguments: null);

                        var properties = chanel.CreateBasicProperties();
                        properties.Persistent = MQConstants.MQ_PROPERTIES_PERSISTENT;

                        var message = JsonConvert.SerializeObject(request);

                        var body = Encoding.UTF8.GetBytes(message);

                        chanel.BasicPublish(exchange: "",
                                            routingKey: request.queuName,
                                            basicProperties: properties,
                                            mandatory: true,
                                            body: body);
                    }
                }
                baseResponse.SetStatus(ResponseCode.SUCCESS);
                return(baseResponse);
            }
            catch (Exception ex)
            {
                baseResponse.SetStatus(ResponseCode.SYSTEM_ERROR, ex.Message);
                return(baseResponse);
            }
        }
        public ConsumerResponse Consume()
        {
            ConsumerResponse consumerResponse = new ConsumerResponse();

            try
            {
                using (IConnection connection = _rabbitMQService.GetRabbitMQConnection())
                {
                    using (IModel channel = connection.CreateModel())
                    {
                        channel.QueueDeclare(queue: MQConstants.MQ_QUEUE_NAME,
                                             durable: MQConstants.MQ_QUEUE_DURABLE,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);

                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var             body           = ea.Body;
                            var             message        = Encoding.UTF8.GetString(body);
                            InserLogRequest consumerResult = JsonConvert.DeserializeObject <InserLogRequest>(message);
                            consumerResponse.publisherRequests.Add(consumerResult);
                        };
                        channel.BasicConsume(queue: MQConstants.MQ_QUEUE_NAME,
                                             autoAck: true,
                                             consumer: consumer);
                    }
                }
                consumerResponse.SetStatus(ResponseCode.SUCCESS);
                return(consumerResponse);
            }
            catch (Exception ex)
            {
                consumerResponse.SetStatus(ResponseCode.SYSTEM_ERROR);
                return(consumerResponse);
            }
        }
Example #3
0
        public ActionResult Publish(InserLogRequest request)
        {
            BaseResponse baseResponse = null;
            //autontication
            var autRequest = new AuthenticationRequest
            {
                ProjectCode = request.ProjectCode,
                Password    = request.Password
            };

            baseResponse = _dbHelper.Authentication(autRequest);
            if (baseResponse.Code != (int)ResponseCode.SUCCESS)
            {
                return(BadRequest(baseResponse.Message));
            }


            baseResponse = _publisher.Publish(request);
            if (baseResponse.Code != (int)ResponseCode.SUCCESS)
            {
                return(BadRequest(baseResponse));
            }
            return(Ok(baseResponse));
        }