internal void run(string dispatcher_center_callback) { this.dispatcher_center_callback = dispatcher_center_callback; Loging.LogInformation <HpScheduleJob>("启动服务:" + this.getJobName()); this.listen(this.rabbitmq_url, this.jobKey); }
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()); } }
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 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); } }