Exemplo n.º 1
0
        public void Ready(PipeHandler worker)
        {
            Contract contract = new Contract {
                Command = MessageTypeEnum.Ready
            };

            worker.Send(contract);
        }
Exemplo n.º 2
0
        protected void SendFinish(PipeHandler worker)
        {
            Contract end = new Contract {
                Command = MessageTypeEnum.End
            };
            var json = JsonHelper.Serialize(end);

            worker.Send(json);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        public override void SendData(Contract data, object sender)
        {
            PipeHandler handler = sender as PipeHandler;

            handler.Send(data);
        }