예제 #1
0
        public static SortedDictionary <string, object> fromXML(string xml)
        {
            SortedDictionary <string, object> result = null;

            try
            {
                if (string.IsNullOrEmpty(xml))
                {
                    return(null);
                }

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);
                XmlNode     xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
                XmlNodeList nodes   = xmlNode.ChildNodes;

                result = new SortedDictionary <string, object>();
                foreach (XmlNode xn in nodes)
                {
                    XmlElement xe = (XmlElement)xn;
                    result[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中
                }
            }
            catch (Exception e)
            {
                result = null;
                Loging.LogError(e.ToString());
            }

            return(result);
        }
예제 #2
0
        private void listen(string rabbitmq_url, string jobKey)
        {
            try
            {
                Loging.LogInformation <HpScheduleJob>("建立连接: " + rabbitmq_url + ", jobKey: " + jobKey);

                var consumer = mFactory.CreateMqConsumer(jobKey);

                Loging.LogInformation <HpScheduleJob>("监听MQ消息: " + jobKey);

                consumer.ReceivedMessage((deliveryTag, message) =>
                {
                    //Log.ContextName = this.GetType().ToString();
                    //Log.ContextID = Guid.NewGuid().ToString("D");
                    Loging.LogInformation <HpScheduleJob>(string.Format("收到消息[{0}-{1}]: {2}", this.getJobName(), jobKey, message));
                    HandleMsg(consumer, message, deliveryTag);
                    // Log.ContextName = null;
                });

                Loging.LogInformation <HpScheduleJob>("服务启动成功:" + this.getJobName());
            }
            catch (Exception ex)
            {
                Loging.LogError <HpScheduleJob>(ex.ToString());
            }
        }
        public bool load()
        {
            try
            {
                if (!exists())
                {
                    if (this.dict == null)
                    {
                        Loging.LogError <JsonConfiguration>("加载配置失败,文件不存在:\n" + string.Join("\n", this.paths));
                    }
                    else
                    {
                        Loging.LogError <JsonConfiguration>("加载配置失败,文件不存在:\n" + string.Join("\n", this.paths));
                    }
                    return(false);
                }

                Dictionary <string, string> json = readJson();
                if (json == null)
                {
                    if (this.dict == null)
                    {
                        Loging.LogError <JsonConfiguration>("加载配置失败,文件格式不正确");
                    }
                    else
                    {
                        Loging.LogError <JsonConfiguration>("加载配置失败,文件格式不正确");
                    }
                    return(false);
                }

                this.dict = new Dictionary <string, string>();
                foreach (string key in json.Keys)
                {
                    this.dict[key.ToLower()] = json[key];
                }
            }
            catch (Exception e)
            {
                if (this.dict == null)
                {
                    Loging.LogError <JsonConfiguration>("加载配置失败", e);
                }
                else
                {
                    Loging.LogWarning <JsonConfiguration>("加载配置失败", e);
                }
                return(false);
            }
            return(true);
        }
        public static bool writeJson(Dictionary <string, string> json, string path)
        {
            try
            {
                var text = JsonUtil.SerializeObject(json);
                if (string.IsNullOrEmpty(text))
                {
                    return(false);
                }

                File.WriteAllText(path, text);

                return(true);
            }
            catch (Exception e)
            {
                Loging.LogError <JsonConfiguration>("写配置信息到本地出错:" + path, e);
                return(false);
            }
        }
예제 #5
0
        private void HandleMsg(IMQConsumer consumer, string message, ulong deliveryTag)
        {
            var context = new HpScheduleContext(mFactory);

            try
            {
                var entity = JsonConvert.DeserializeObject <DeliveredModel>(message);

                context.taskid     = entity.task_id;
                context.param      = entity.param;
                context.routingkey = this.dispatcher_center_callback;
                context.rabbimqUrl = this.rabbitmq_url;

                if (!context.Log("开始执行任务", 0))
                {
                    //应答,并使该消息重新从队列获取
                    consumer.NAck(deliveryTag);
                    return;
                }

                //应答
                consumer.Ack(deliveryTag);

                try
                {
                    //执行任务
                    Execute(context);
                }
                catch (Exception e)
                {
                    Loging.LogError <HpScheduleJob>("执行任务失败", e);
                    context.Log(e.ToString(), -1);
                }
            }
            catch (System.Exception e)
            {
                Loging.LogInformation <HpScheduleJob>(e.ToString());
                context.Log(e.ToString(), -1);
            }
        }
        private Dictionary <string, string> readJson(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    return(null);
                }

                string text = File.ReadAllText(path);
                if (string.IsNullOrEmpty(text))
                {
                    return(null);
                }

                return(JsonUtil.DeserializeJsonToObject <Dictionary <string, string> >(text));
            }
            catch (Exception e)
            {
                Loging.LogError <JsonConfiguration>("从本地读配置信息出错:" + path, e);
                return(null);
            }
        }
예제 #7
0
        public static string fromStream(System.IO.Stream stream)
        {
            StringBuilder builder = new StringBuilder("");

            try
            {
                System.IO.Stream s = stream;
                int    count       = 0;
                byte[] buffer      = new byte[1024];
                while ((count = s.Read(buffer, 0, 1024)) > 0)
                {
                    builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
                }
                s.Flush();

                s.Dispose();
            }
            catch (Exception e)
            {
                Loging.LogError(e.ToString());
                return(null);
            }
            return(builder.ToString());
        }
        private bool sendMsg(string message, int?progress)
        {
            IMQFactory factory = null;

            if (!mFactory.TryGetTarget(out factory))
            {
                return(false);
            }

            Loging.LogInformation <HpScheduleContext>(String.Format("Task({0}) ::: send message = {1}, progress = {2}", this.taskid, message, progress == null ? "null" : progress.ToString()));

            try
            {
                CallBackModel callback = new CallBackModel()
                {
                    task_id  = this.taskid,
                    message  = message,
                    progress = progress,
                    time     = DateTimeUtil.timestamp(DateTime.Now)// DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
                };

                var jsonstr = JsonConvert.SerializeObject(callback);

                using (var producer = factory.CreateMqProducer(routingkey))
                {
                    producer.sendMessage(jsonstr);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Loging.LogError <HpScheduleContext>(ex.ToString());
                return(false);
            }
        }