Ejemplo n.º 1
0
        /// <summary>
        ///     创建一个新的事务
        /// </summary>
        /// <param name="role">针对的服务角色</param>
        /// <returns>返回新的事务</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public BusinessMessageTransaction CreateTransaction(string role)
        {
            if (string.IsNullOrEmpty(role))
            {
                throw new ArgumentNullException("role");
            }
            string errMsg;
            IServerConnectionAgent <BaseMessage> agent = _clsuter.GetChannel(role, _protocolStacks[role], out errMsg);

            return(agent == null
                       ? new FailMobiSageMessageTransaction(errMsg)
                       : _transactionManager.Create(IdentityHelper.Create((IPEndPoint)agent.GetChannel().LocalEndPoint), agent.GetChannel()));
        }
 /// <summary>
 ///     创建一个新的事务
 /// </summary>
 /// <returns>返回新创建的事务</returns>
 /// <exception cref="ArgumentNullException">参数错误</exception>
 public MessageTransaction <BaseMessage> CreateTransaction()
 {
     return(_transactionManager.Create(IdentityHelper.Create(_channel.LocalEndPoint, _channel.ChannelType), _channel));
 }
        /// <summary>
        ///     获取某个字段的值
        /// </summary>
        /// <typeparam name="T">字段类型</typeparam>
        /// <param name="database">数据库名</param>
        /// <param name="table">表名</param>
        /// <param name="service">服务名称</param>
        /// <param name="field">字段名</param>
        /// <returns>返回相应字段的值</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public string GetField(string database, string table, string service, string field)
        {
            string result = null;

            #region Get cache first.

            Dictionary <string, string> items;
            //has cache.
            if (_cacheKeys.TryGetValue(service, out items))
            {
                return(items.TryGetValue(field, out result) ? result : null);
            }

            #endregion
            PrepareConnection();
            #region Get remote config.

            Exception exception = null;
            CSNGetKeyDataRequestMessage requestMessage = new CSNGetKeyDataRequestMessage();
            requestMessage.DatabaseName = database;
            requestMessage.TableName    = table;
            requestMessage.ServiceName  = service;

            BusinessMessageTransaction transaction = _transactionManager.Create(IdentityHelper.Create((IPEndPoint)_channel.LocalEndPoint), _channel);
            if (transaction == null)
            {
                throw new Exception("Cannot create a CSN message transaction!");
            }
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <BaseMessage> e)
            {
                CSNGetKeyDataResponseMessage rspMessage = (CSNGetKeyDataResponseMessage)e.Target;
                #region Add Cache.

                Dictionary <string, string> values = new Dictionary <string, string>();
                if (rspMessage.Items != null)
                {
                    foreach (KeyValueItem item in rspMessage.Items)
                    {
                        values.Add(item.Key, item.Value);
                    }
                    _cacheKeys.Add(service, values);
                }
                //get result.
                result = values.ContainsKey(field) ? values[field] : null;

                #endregion
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                exception = new Exception("Failed! Cannot get key data config from CSN service, #key: " + field);
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                exception = new Exception("Timeout! Cannot get key data config from CSN service, #key: " + field);
                autoResetEvent.Set();
            };
            transaction.SendRequest(requestMessage);
            //wait 35s.
            autoResetEvent.WaitOne(35000);
            autoResetEvent.Dispose();
            if (exception != null)
            {
                throw exception;
            }

            #endregion

            return(result);
        }