예제 #1
0
        public bool Subscribe(string channel)
        {
            ValidateInitialized();

            IMQCreateChannelParams paramsCreateChannelId = _mq.CreateCreateChannelParams();

            paramsCreateChannelId.ChannelName = channel;

            var resChannelId = _mq.CreateChannel(paramsCreateChannelId);

            if (resChannelId.Success)
            {
                long channelId = resChannelId.ChannelId;
                lock (_lockChannels)
                {
                    if (!_channels.ContainsKey(channel))
                    {
                        _channels.Add(channel, resChannelId.ChannelId);
                    }
                }

                IMQSubscribeParams paramsSubs = _mq.CreateSubscribeParams();
                paramsSubs.ChannelId    = channelId;
                paramsSubs.SubscriberId = _subscriberId;

                var resSubs = _mq.Subscribe(paramsSubs);

                return(resSubs.Success);
            }
            else
            {
                return(resChannelId.Success);
            }
        }
        public IMQCreateChannelResult CreateChannel(IMQCreateChannelParams paramsCreateChannel)
        {
            var result = new DbMQCreateChannelResult();

            string        spName = "[SP_Create_Channel]";
            SqlConnection conn   = OpenConnection("ConnectionString");

            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = schema + "." + spName;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection  = conn;

            try
            {
                SqlParameter paramChannelName = new SqlParameter("@Channel_Name",
                                                                 SqlDbType.NVarChar, 255,
                                                                 ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Current,
                                                                 paramsCreateChannel.ChannelName);

                SqlParameter paramMessageTimeout = new SqlParameter("@Message_Timeout",
                                                                    SqlDbType.Time, 7,
                                                                    ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Current,
                                                                    paramsCreateChannel.MessageTimeout);

                SqlParameter paramOutChannelId = new SqlParameter("@Channel_Id",
                                                                  SqlDbType.BigInt, 0,
                                                                  ParameterDirection.Output, false, 0, 0, "", DataRowVersion.Current,
                                                                  result.ChannelId);

                cmd.Parameters.Add(paramChannelName);
                cmd.Parameters.Add(paramMessageTimeout);
                cmd.Parameters.Add(paramOutChannelId);

                cmd.ExecuteNonQuery();

                result.ChannelId = (long)paramOutChannelId.Value;
                result.Success   = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Errors.Add(new Interfaces.Error()
                {
                    Message = ex.Message,
                    Code    = Interfaces.EErrorCodes.MQDbError,
                    Type    = Interfaces.EErrorType.Error
                });
            }

            conn.Close();

            return(result);
        }
예제 #3
0
        public void CreateChannel_AlreadyExists()
        {
            RunInitSql("011.CreateChannel_AlreadyExists", _conn);

            IMessageQueue mq = CreateMQ();

            IMQCreateChannelParams paramsCreateChannel = mq.CreateCreateChannelParams();

            paramsCreateChannel.ChannelName    = ConfigurationManager.AppSettings["ChannelName"];
            paramsCreateChannel.MessageTimeout = TimeSpan.FromMinutes(30);

            IMQCreateChannelResult result = mq.CreateChannel(paramsCreateChannel);

            RunFinalizeSql("011.CreateChannel_AlreadyExists", _conn);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Success);
            Assert.Greater(result.ChannelId, 0);
        }