protected int ReceiveMsg(PipeHandler worker, ChainwayMQConfig config, bool stop = false) { int count = 0; while (true) { if (stop) { break; } string json = worker.Receive(120); if (string.IsNullOrEmpty(json)) { throw new TimeoutException("接收主机信息超时"); } Contract data = JsonHelper.Deserialize <Contract>(json); switch (data.Command) { case MessageTypeEnum.Work: Contract result = new Contract(); try { ChainwayProducer producer = GetProducer(config.Group, config.Address); foreach (var t in config.Topic) { ChainwayMessage msg = new ChainwayMessage(t); msg.setKeys(data.Keys); msg.setTags(data.Tags); msg.Body = data.Data; producer.send(msg); count++; _logger.Debug(string.Format("表{0}成功推送1条数据到消息队列,json:{1}", data.Keys, data.Data)); } result.Command = MessageTypeEnum.OK; } catch (Exception ex) { result.Command = MessageTypeEnum.Error; result.Message = ex.Message; _logger.WriteException(ex); } worker.Send(result); break; case MessageTypeEnum.End: return(count); } } return(count); }
public override void HandleData(DataTable table, object sender) { if (table.Rows.Count > 0) { string tableName = _tableconfig.FormatedTableName; foreach (DataRow row in table.Rows) { if (Stop) { return; } var entity = row.ToDictionary(); entity["__Status"] = _tableconfig.Action; string json = JsonHelper.Serialize(entity); Contract data = new Contract { Data = json, Command = MessageTypeEnum.Work, Keys = tableName, }; SendData(data, sender); var response = _handler.Receive <Contract>(30); if (response == null) { throw new TimeoutException("接收代理回执超时"); } if (response.Command == MessageTypeEnum.Error) { throw new Exception(response.Message); } else if (response.Command == MessageTypeEnum.Ready) { throw new ReadyException("proxy says ready, so let's get ready"); } if (_tableconfig.DeleteData) { DeleteSendData(entity); } _logger.Write(string.Format("表{1}成功推送数据到消息队列,json:{0}", json, _tableconfig.IDOrTableName)); RecordCondition(table, row); } _logger.Write(string.Format("表{1}成功推送{0}条数据到消息队列", table.Rows.Count, _tableconfig.IDOrTableName)); } }
protected Contract SendMessage(string group, Contract contract, PipeWorker worker) { string json = JsonHelper.Serialize(contract); var config = MQFactory.Consumerconfig.Find(t => t.Group.Equals(group)); PipeHandler handler = new PipeHandler(worker.Pipe); handler.Send(json); var responseString = handler.Receive(30); if (string.IsNullOrEmpty(responseString)) { throw new TimeoutException("接收消费回执超时"); } var response = JsonHelper.Deserialize <Contract>(responseString); return(response); }