Пример #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);
        }
Пример #2
0
        /// <summary>
        /// Post请求返回实体
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <returns>实体</returns>
        public static T PostResponse <T>(string url, string postData)
        {
            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();
                HttpContent httpContent = new StringContent(postData);

                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                T result = default(T);
                Task <HttpResponseMessage> taskResponse = httpClient.PostAsync(url, httpContent);
                taskResponse.Wait();
                response = taskResponse.Result;
                //using (HttpResponseMessage response = taskResponse.Result)
                //{
                //HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                if (response.IsSuccessStatusCode)
                {
                    //response.Content.ReadAsAsync<IEnumerable<ProductInfo>>().Result
                    //Task<string> t = response.Content.ReadAsStringAsync();
                    //t.Wait();
                    //string s = t.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 s = reader.ReadToEnd();
                    result = JsonConvertTool.DeserializeObject <T>(s);
                }
                //}
                return(result);
                //}
            }
            catch
            {
                return(default(T));
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Post请求返回字符
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <returns>字符</returns>
        public static string PostResponse(string url, object obj)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            });
            HttpResponseMessage response = null;

            try
            {
                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;
                if (response.IsSuccessStatusCode)
                {
                    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);
                }
                else
                {
                    log.Error("结果:" + response.StatusCode + "内容:" + JsonConvertTool.SerializeObject(obj));
                }
                return(null);
            }
            catch (Exception exception)
            {
                log.Error("结果:异常 内容:" + JsonConvertTool.SerializeObject(obj) + "ex:" + exception.Message, exception);
                return(null);
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 根据Url地址Get请求返回实体
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <returns>实体</returns>
        public static T GetResponse <T>(string url)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            });
            HttpResponseMessage response = null;

            try
            {
                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();
                httpClient.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                Task <HttpResponseMessage> taskResponse = httpClient.GetAsync(url);
                taskResponse.Wait();
                T result = default(T);
                response = taskResponse.Result;
                if (response.IsSuccessStatusCode)
                {
                    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 s = reader.ReadToEnd();

                    result = JsonConvertTool.DeserializeObject <T>(s);
                }
                return(result);
            }
            catch (Exception e)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(e.ToString()),
                    ReasonPhrase = "error"
                };
                return(default(T));
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Пример #5
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
     {
         //记录日志时有可能出现异常
     }
 }
Пример #6
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
     {
         //记录日志时有可能出现异常
     }
 }
Пример #7
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);
                }
            }
        }