Example #1
0
        /// <summary>
        /// 消息发送(单个)
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="entity">消息数据</param>
        /// <returns>结果(0失败1成功)</returns>
        public int SendActiveMQMessage(MessageQueueName queueName, MessageEntity entity)
        {
            int    msgResult = 0;  //消息结果
            string msgEntity = ""; //文本消息

            //判断是否发送消息
            if (entity != null)
            {
                try
                {
                    //创建回话
                    using (ISession sesssion = connection.CreateSession())
                    {
                        //实体消息序列化文本消息
                        msgEntity = JsonConvertTool.SerializeObject(entity);

                        //创建生产者
                        IDestination     destination = new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(queueName.ToString());
                        IMessageProducer producer    = sesssion.CreateProducer(destination);
                        //生产消息
                        ITextMessage _message = producer.CreateTextMessage(msgEntity);

                        //发送消息(持久化)
                        producer.Send(_message, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue);
                    }
                    msgResult = 1;
                }
                catch (Exception ex)
                {
                    //  LogHelper.WriteLog(typeof(ActiveMq), "方法名:SendActiveMQMessage发送消息队列异常(单个):", Engineer.maq, entity, ex);
                }
            }
            return(msgResult);
        }
Example #2
0
        /// <summary>
        /// Put请求返回字符
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <returns>字符</returns>
        public static string PutResponse(string url, object obj)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            });
            HttpResponseMessage response = null;

            try
            {
                //using (HttpClient httpClient = new HttpClient())
                //{
                httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                httpClient.CancelPendingRequests();
                httpClient.DefaultRequestHeaders.Clear();
                string      postData    = JsonConvertTool.SerializeObject(obj);
                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                Task <HttpResponseMessage> taskResponse = httpClient.PostAsync(url, httpContent);
                taskResponse.Wait();
                response = taskResponse.Result;
                //using (HttpResponseMessage response = taskResponse.Result)
                //{
                //HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;
                if (response.IsSuccessStatusCode)
                {
                    //Task<string> taskStr = response.Content.ReadAsStringAsync();
                    //taskStr.Wait();
                    //string result = taskStr.Result;

                    Task <System.IO.Stream> taskStream = response.Content.ReadAsStreamAsync();
                    taskStream.Wait();
                    System.IO.Stream       dataStream = taskStream.Result;
                    System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                    string result = reader.ReadToEnd();
                    return(result);
                }
                //}
                return(null);
                //}
            }
            catch
            {
                return(null);
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Example #3
0
 /// <summary>
 ///     写入日志
 ///     sunj
 ///     2015年1月20日15:06:53
 /// </summary>
 /// <typeparam name="T">实体</typeparam>
 /// <param name="t">当前类</param>
 /// <param name="functionName">方法名称</param>
 /// <param name="author">作者</param>
 /// <param name="entity">参数实体</param>
 /// <param name="ex">错误信息</param>
 public static void WriteInfo <T>(Type t, string functionName, Engineer author, T entity)
 {
     try
     {
         string str = string.Empty;
         if (entity != null)
         {
             str = JsonConvertTool.SerializeObject(entity);
         }
         var log     = LogManager.GetLogger(t);
         var strInfo = "Error:方法名:" + functionName + "--作者:" + author.ToString() + "\r\n传入参数:\r\n" + str + "\r\n" + "客户端ip:" + ip + ",请求连接:" + rawUrl;
         log.Info(strInfo);
     }
     catch
     {
         //记录日志时有可能出现异常
     }
 }
Example #4
0
 /// <summary>
 ///     写入日志
 ///     baochen
 ///     2015年1月20日15:06:53
 /// </summary>
 /// <typeparam name="T">实体</typeparam>
 /// <param name="t">当前类</param>
 /// <param name="functionName">方法名称</param>
 /// <param name="author">作者</param>
 /// <param name="entity">参数实体</param>
 /// <param name="ex">错误信息</param>
 public static void WriteLog <T>(Type t, string functionName, Engineer author, T entity, Exception ex)
 {
     try
     {
         string str = string.Empty;
         if (entity != null)
         {
             str = JsonConvertTool.SerializeObject(entity);
         }
         var log     = LogManager.GetLogger(t);
         var strInfo = "Error:方法名:" + functionName + "--作者:" + author.ToString() + "\r\n传入参数:\r\n" + str + "\r\n错误信息:" +
                       ex.Message + "\r\n" + ex.StackTrace + "\r\n" + "客户端ip:" + ip + ",请求连接:" + rawUrl;
         log.Error(strInfo);
         //EmailDelegate dn = new EmailDelegate(new SendEmail().Send);
         //IAsyncResult ias = dn.BeginInvoke("屌丝你程序出错了", strInfo, author.ToString().Trim(), null, dn);
         //dn.EndInvoke(ias);
     }
     catch
     {
         //记录日志时有可能出现异常
     }
 }
Example #5
0
        /// <summary>
        /// 最终消息处理
        /// </summary>
        /// <param name="msg"></param>
        private void HandleMessage(T msg)
        {
            //处理消息
            ResultEntity result = _BaseBll.MainMethod(msg);

            //通过结果判断操作是否正确
            if (result != null && result.ResultCode == 1)
            {
            }
            else
            {
                //处理消息错误,再次处理判断
                if (ErrorRepeatHandle)
                {
                    //重新发送消息
                    SendMessage(msg);
                }

                //错误写日志
                LogHelper.WriteInfo(typeof(HandleActiveMq <T>), "HandleActiveMq处理消息错误!结果:" + JsonConvertTool.SerializeObject(result) + "\n内容:" + JsonConvertTool.SerializeObject(msg));

                //错误是否睡眠
                if (ErrorStopTime > 0)
                {
                    //错误睡眠时间
                    Thread.Sleep(ErrorStopTime);
                }
            }
        }