Example #1
0
        public static void Send(string exchangeName, byte[] messageBody, bool durable = false, string routeKey = "")
        {
            var _channel = _mqpool.Value.GetMQChannel("DotPay.Tools.DistributedMessageSender");

            var build = new BytesMessageBuilder(_channel);
            build.WriteBytes(messageBody);

            //是否持久化消息
            if (durable) ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

            try
            {
                _channel.BasicPublish(exchangeName, routeKey, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
            }
            catch (RabbitMQ.Client.Exceptions.AlreadyClosedException ex)
            {
                Log.Warn("messagesender发送消息时,发现消息队列服务器已关闭",ex);

                try
                {
                    _channel = _mqpool.Value.GetMQChannel("DotPay.Tools.DistributedMessageSender");
                    _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                }
                catch (Exception eex)
                {
                    Log.Error("分布式消息发送器链接消息队列出错", eex);
                }
            }
            catch (System.IO.EndOfStreamException ex)
            {
                Log.Warn("messagesender发送消息时,发现消息队列服务器已关闭",ex);

                try
                {
                    _channel = _mqpool.Value.GetMQChannel("DotPay.Tools.DistributedMessageSender");
                    _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                }
                catch (Exception eex)
                {
                    Log.Error("分布式消息发送器链接消息队列出错", eex);
                }
            }
        }
Example #2
0
        private static void SendCreatePaymentAddressCommand(CurrencyType currency, int userID, string address)
        {
            var mqname = "NXTSendCreatePaymentAddressCommand";
            var channel = _mqpool.GetMQChannel("NXTSendCreatePaymentAddressCommand");
            var exchangeAndQueueName = Utilities.GenerateExchangeAndQueueNameOfCreatePaymentAddress(currency);
            var cmd = new CreatePaymentAddress(userID, address, currency);
            var build = new BytesMessageBuilder(channel);
            build.WriteBytes(Encoding.UTF8.GetBytes(IoC.Resolve<IJsonSerializer>().Serialize(cmd)));
            ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

            try
            {
                channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
            }
            catch (EndOfStreamException ex)
            {
                if (ex.Message.Equals("SharedQueue closed", StringComparison.OrdinalIgnoreCase))
                {
                    channel = _mqpool.GetMQChannel(mqname);
                    channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                }
            }
        }
        private void PublishEventMessage(string eventId, string eventJsonString)
        {
            var useChannel = this.GetChannelByEventId(eventId);
            var routeKey = (eventId.GetHashCode() % dispatcherSetting.QueuePoolSize).ToString();
            var eventMsg = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new EventMessage { EventId = eventId, Message = eventJsonString }));
            var build = new BytesMessageBuilder(useChannel);
            build.WriteBytes(eventMsg);
            ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

            useChannel.BasicPublish(MQ_EXCHANAGE_NAME, routeKey, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
        }
        private static void ProcessTx(int userID, GetTransactionResponse tx, UInt64 nxtAccountID, CurrencyType currency)
        {
            var cacheKey = currency.ToString() + tx.SenderRS + tx.Transaction + "create";

            if (tx.Type != TransactionType.Ordinary || tx.Recipient != nxtAccountID)
            {
                //如果交易类型不是普通交易或者收款账户非当前账户(非收款),忽略,并加入忽略名单,防止多次请求交易明细,浪费资源
                Cache.Add(cacheKey, tx.Confirmations);
                return;
            }
            object obj = null;
            if (Cache.TryGet(cacheKey, out obj)) return;

            Log.Info("发现新的交易....");

            var actualAmount = tx.AmountNQT / 100000000;

            #region 发送交易到处理消息队列
            var mqname = "NXTReceiveTransactionListener";
            _channel = _mqpool.GetMQChannel(mqname);
            var exchangeName = Utilities.GenerateVirtualCoinReceivePaymentExchangeAndQueueName(currency).Item1;
            var build = new BytesMessageBuilder(_channel);

            var cmd = new CreateReceivePaymentTransaction(tx.Transaction, tx.RecipientRS, actualAmount, currency);
            build.WriteBytes(Encoding.UTF8.GetBytes(IoC.Resolve<IJsonSerializer>().Serialize(cmd)));
            ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

            try
            {
                Log.Info("交易将被发送到Exchange->{0}", exchangeName);

                _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());

                Log.Info("交易成功发送到Exchange->{0}", exchangeName);

                Cache.Add(cacheKey, tx.Confirmations);
            }
            catch (RabbitMQ.Client.Exceptions.AlreadyClosedException ex)
            {
                Log.Error("发送{0}新交易创建指令时发现消息队列服务器已关闭".FormatWith(Config.CoinCode), ex);

                try
                {
                    _channel = _mqpool.GetMQChannel(mqname);
                    _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                    Log.Info("交易成功发送到Exchange->{0}", exchangeName);
                }
                catch (Exception eex)
                {
                    Log.Fatal("重新链接消息队列发送NXT新交易仍然出错了".FormatWith(Config.CoinCode), eex);
                }
            }
            catch (Exception ex)
            {
                Log.Error("发送{0}新交易创建指令时出现错误".FormatWith(Config.CoinCode), ex);
            }
            #endregion

            #region 提取币到总账户中
            try
            {
                var nxtclient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase + userID);
                var result = nxtclient.SendMoneyAsnc(Config.NxtSumAccount, actualAmount - 1).Result;
                Log.Info("用户充值的币发送到总帐户成功");
            }
            catch (Exception ex)
            {
                Log.Fatal("用户ID={0}充值的{1}NXT发送到总帐户失败了,请及时处理".FormatWith(userID, actualAmount), ex);
            }
            #endregion
        }
Example #5
0
        private static void SendBalanceWarnMessage(string message)
        {
            var mqname = "SendBalanceWarnMessage";
            var channel = _mqpool.GetMQChannel(mqname);
            var exchangeAndQueueName = Utilities.GenerateBanlanceWarnExchangeAndQueueName();
            var build = new BytesMessageBuilder(channel);
            build.WriteBytes(Encoding.UTF8.GetBytes(message));
            ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

            try
            {
                channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
            }
            catch (EndOfStreamException ex)
            {
                if (ex.Message.Equals("SharedQueue closed", StringComparison.OrdinalIgnoreCase))
                {
                    channel = _mqpool.GetMQChannel(mqname);
                    channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                }
            }
        }
        private static void ProcessTx(GetTransactionResponse tx, CurrencyType currency)
        {
            if (!ExistConfirmTx(tx, currency))
            {
                Log.Info("txid={0}的交易确认已达到{1}个,开始发送确认消息..", tx.Transaction, tx.Confirmations);
                var mqname = "NXTTransactionConfirmationValidator";
                _channel = _mqpool.GetMQChannel(mqname);
                var build = new BytesMessageBuilder(_channel);
                var exchangeName = Utilities.GenerateVirtualCoinReceivePaymentExchangeAndQueueName(currency).Item1;

                try
                {
                    var amount = tx.AmountNQT / 100000000;
                    var cmd = new ConfirmReceivePaymentTransaction(tx.Transaction, tx.RecipientRS, tx.Confirmations, amount, currency);
                    build.WriteBytes(Encoding.UTF8.GetBytes(IoC.Resolve<IJsonSerializer>().Serialize(cmd)));
                    ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;

                    _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());

                    var cacheKey = currency.ToString() + tx.Transaction + "_" + amount + "confirm";

                    Cache.Add(cacheKey, tx.Confirmations);

                    Log.Info("txid={0}的交易确认消息发送完毕", tx.Transaction, tx.Confirmations);
                }
                catch (RabbitMQ.Client.Exceptions.AlreadyClosedException ex)
                {
                    Log.Error("发送{0}新交易创建指令时发现消息队列服务器已关闭".FormatWith(Config.CoinCode), ex);

                    try
                    {
                        _channel = _mqpool.GetMQChannel(mqname);
                        _channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());

                    }
                    catch (Exception eex)
                    {
                        Log.Fatal("NXTTransactionConfirmationValidator消息队列服务器连接重试后仍无法连接成功,可能消息队列服务器已经挂掉,请尽快处理!", eex);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("发送{0}新交易创建指令时出现错误".FormatWith(Config.CoinCode), ex);
                }

            }
        }
        private static void SendCompleteCommand(CurrencyType currency, string withdrawUniqueID, string txid, decimal txfee)
        {
            try
            {
                var mqname = "NXTSendCompleteCommand";
                var channel = _mqpool.GetMQChannel(mqname);

                var exchangeAndQueueName = Utilities.GenerateVirtualCoinCompletePaymentExchangeAndQueueName();

                var cmd = new CompleteVirtualCoinWithdraw(withdrawUniqueID, txid, txfee, currency);

                var build = new BytesMessageBuilder(channel);
                build.WriteBytes(Encoding.UTF8.GetBytes(IoC.Resolve<IJsonSerializer>().Serialize(cmd)));
                ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;
                try
                {
                    channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                }
                catch (EndOfStreamException ex)
                {
                    if (ex.Message.Equals("SharedQueue closed", StringComparison.OrdinalIgnoreCase))
                    {
                        channel = _mqpool.GetMQChannel(mqname);
                        channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("提现处理完毕后,发送完成消息时出现错误:withdrawID={0},currency={1},txid={3}".FormatWith(withdrawUniqueID, currency, txid), ex);
                }
            }
            catch (Exception ex)
            {
                Log.Error("提现处理完毕后,发送完成消息时出现错误:withdrawID={0},currency={1},txid={3}".FormatWith(withdrawUniqueID, currency, txid), ex);
            }
        }
        private static void SendWithdrawFailOrAddressInvalidCommand(CurrencyType currency, string withdrawUniqueID, VirtualCoinWithdrawFailProcessType processType)
        {
            var cmdMessage = string.Empty;
            try
            {
                var mqname = currency.ToString() + "SendWithdrawFailOrAddressInvalidCommand";
                var channel = _mqpool.GetMQChannel(mqname);
                var exchangeAndQueueName = Utilities.GenerateVirtualCoinWithdrawTranferFailOrAddressInvalidExchangeAndQueueName();
                var nullUserID = 0;

                if (processType == VirtualCoinWithdrawFailProcessType.Cancel)
                {
                    Log.Info("{0}提现{1}的发送到p2p失败,发送处理失败指令", currency, withdrawUniqueID);
                    var cmd = new CancelVirtualCoinWithdraw(withdrawUniqueID, nullUserID, string.Empty, currency);
                    cmdMessage = IoC.Resolve<IJsonSerializer>().Serialize(cmd);
                }
                else
                {
                    Log.Info("{0}提现{1}的提现地址不合法,发送撤销提现指令", currency, withdrawUniqueID);
                    var cmd = new VirtualCoinWithdrawFail(withdrawUniqueID, nullUserID, string.Empty, currency);
                    cmdMessage = IoC.Resolve<IJsonSerializer>().Serialize(cmd);

                }

                var build = new BytesMessageBuilder(channel);
                build.WriteBytes(Encoding.UTF8.GetBytes(cmdMessage));
                ((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;
                try
                {
                    channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                }
                catch (EndOfStreamException ex)
                {
                    if (ex.Message.Equals("SharedQueue closed", StringComparison.OrdinalIgnoreCase))
                    {
                        channel = _mqpool.GetMQChannel(mqname);
                        channel.BasicPublish(exchangeAndQueueName.Item1, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("发送提现失败处理消息时--失败了:" + cmdMessage, ex);
                }

            }
            catch (Exception ex)
            {
                Log.Error("发送提现失败处理消息时--失败了:" + cmdMessage, ex);
            }
        }