public void Send(Contract data) { ChainwayMessage message = new ChainwayMessage(data.Topic) { Body = data.Data }; message.setTags(data.Tags); message.setKeys(data.Keys); var result = _producer.send(message); }
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 void Start() { MQFactory.LoadConfig(); while (!IsStop) { try { var table = GetTopNData(MQFactory.Producerconfig[0].Top); if (table.Rows.Count > 0) { var list = table.ToList <T_Sync_Temp>(); list = (from l in list orderby l.Address select l).ToList(); string currentAddress = null; ChainwayProducer producer = null; foreach (var data in list) { if (string.IsNullOrEmpty(currentAddress)) { currentAddress = data.Address; } if (!currentAddress.Equals(data.Address)) { if (producer != null) { producer.shutdown(); } producer = new ChainwayProducer(data.Group); producer.setNamesrvAddr(data.Address); producer.start(); } else if (producer == null) { producer = new ChainwayProducer(data.Group); producer.setNamesrvAddr(data.Address); producer.start(); } var topics = data.Topic.Split(','); foreach (var t in topics) { ChainwayMessage msg = new ChainwayMessage(t); msg.Body = data.Data; msg.setKeys(data.TableName); msg.setTags(data.Tags); producer.Send(msg); } currentAddress = data.Address; } if (producer != null) { producer.shutdown(); } } else { Thread.Sleep(10 * 1000); } } catch (Exception ex) { _logger.WriteException(ex); Thread.Sleep(10 * 1000); } } }