Ejemplo n.º 1
0
 ResultSet IOneCWebService2.ExecuteMethod(
     string _connectionName,
     string _poolUserName,
     string _poolPassword,
     string _methodName,
     Parameters _parameters
     )
 {
     try
     {
         if (ConnectionPool.Pools.ContainsKey(_connectionName))
         {
             AbstractAdapter adapter = ConnectionPool.Pools[_connectionName].GetConnection(_poolUserName, _poolPassword);
             try
             {
                 return(adapter.ExecuteMethod(_methodName, _parameters.Params));
             }
             finally
             {
                 ConnectionPool.Pools[_connectionName].ReleaseConnection(adapter);
             }
         }
         else
         {
             throw new Exception("Connection with name '" + _connectionName + "' not found");
         }
     }
     catch (Exception _e)
     {
         SimpleLogger.DefaultLogger.Severe("Exception in ExecuteRequest: " + _e.ToString());
         ResultSet resultSet = new ResultSet();
         resultSet.Error = _e.ToString();
         return(resultSet);
     }
 }
Ejemplo n.º 2
0
 public void ReleaseConnection(AbstractAdapter _adapter)
 {
     lock (connections)
     {
         if (connections.ContainsKey(_adapter.Guid))
         {
             PoolableConnection pc = connections[_adapter.Guid];
             pc.Release();
         }
         else
         {
             throw new Exception("Unknown adapter for this pool: GUID=" + _adapter.Guid.ToString());
         }
     }
 }
Ejemplo n.º 3
0
        public void TestLoop()
        {
            ConnectionPool pool = new ConnectionPool();

            pool.Logger = new ConsoleLogger();

            pool.Parameters.Add(ConnectionPool.AdapterTypeParam, "OneCService2.V81Adapter");
            pool.Parameters.Add(ConnectionPool.NameParam, "TestName");
            pool.Parameters.Add(ConnectionPool.PoolSizeParam, "1");
            pool.Parameters.Add(ConnectionPool.PoolUserNameParam, "PoolUserName");
            pool.Parameters.Add(ConnectionPool.PoolPasswordParam, "PoolPassword");

            pool.Parameters.Add(V81Adapter.ModeParam, "File");
            pool.Parameters.Add(V81Adapter.FileParam, @"C:\Work\1C\Test");
            pool.Parameters.Add(V81Adapter.UserNameParam, "");
            pool.Parameters.Add(V81Adapter.PasswordParam, "");

            try
            {
                pool.Init();

                int loopCount = 1000000;

                for (int i = 0; i < loopCount; i++)
                {
                    AbstractAdapter adapter = pool.GetConnection("PoolUserName", "PoolPassword");

                    try
                    {
                        ResultSet r = adapter.ExecuteScript(
                            "с = Новый Структура(); с.Вставить(\"A\", Справочники.Номенклатура.НайтиПоКоду(1)); с.Вставить(\"B\", 2); результат=с;"
                            );
                        Console.WriteLine("Loop: " + i);
                        Thread.Sleep(50);
                    }
                    finally
                    {
                        pool.ReleaseConnection(adapter);
                    }
                }
            }
            finally
            {
                pool.Done();
            }
        }
Ejemplo n.º 4
0
 /*Обрабатываем очерель ожидания*/
 private void ProcessWaitQueue()
 {
     try
     {
         while (!waitQueueProcessorStopFlag)
         {
             lock (waitQueue)
             {
                 if (waitQueue.Count > 0)
                 {
                     AbstractAdapter adapter = null;
                     lock (connections)
                     {
                         adapter = GetFreeConnection(false);
                         if (adapter != null)
                         {
                             waitQueue.Dequeue().Set();
                         }
                     }
                 }
                 else
                 {
                     Thread.Sleep(waitQueueSleepDelay);
                 }
             }
         }
     }
     catch (Exception _e)
     {
         logger.Severe("Exception in ProcessWaitList: " + _e.ToString());
     }
     finally
     {
         /*Выведем из состояния ожидания всех тех кто там все еще находится*/
         lock (waitQueue)
         {
             while (waitQueue.Count > 0)
             {
                 waitQueue.Dequeue().Set();
             }
         }
     }
 }
Ejemplo n.º 5
0
        public AbstractAdapter GetConnection(string _userName, string _password)
        {
            if ((!_userName.Equals(PoolUserName)) || (!_password.Equals(PoolPassword)))
            {
                throw new Exception("Wron user name or passowrd for access to connection pool");
            }

            AbstractAdapter adapter = null;

            lock (connections)
            {
                adapter = GetFreeConnection(true);
                if (adapter != null)
                {
                    return(adapter);
                }
            }
            /*Текущий поток попадает в очередь ожидания*/
            EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);

            lock (waitQueue)
            {
                waitQueue.Enqueue(waitHandle);
            }
            /*И приостанавливается*/
            Mutex.WaitAny(new WaitHandle[] { waitHandle }, 30000);
            /*До тех пор, пока его не пробудит обработчик ожидания для повторной попытки*/
            lock (connections)
            {
                adapter = GetFreeConnection(true);
                if (adapter != null)
                {
                    return(adapter);
                }
            }
            /*Если же потоку так и не удалось получить соединение*/
            throw new Exception("Sorry, but current thread could not get free connection from pool. May be increase pool size?");
        }
Ejemplo n.º 6
0
        public void Init()
        {
            if (!Parameters.ContainsKey(PoolSizeParam))
            {
                throw new Exception("" + PoolSizeParam + " parameter not found");
            }
            else
            {
                poolSize = Convert.ToInt32(Parameters[PoolSizeParam]);
            }
            if (!Parameters.ContainsKey(NameParam))
            {
                throw new Exception("" + NameParam + " parameter not found");
            }
            else
            {
                name = Parameters[NameParam];
            }
            if (!Parameters.ContainsKey(NameParam))
            {
                throw new Exception("" + AdapterTypeParam + " parameter not found");
            }
            else
            {
                adapterType = Type.GetType(Parameters[AdapterTypeParam]);
            }

            if (!Parameters.ContainsKey(PoolUserNameParam))
            {
                throw new Exception("" + PoolUserNameParam + " parameter not found");
            }
            else
            {
                poolUserName = Parameters[PoolUserNameParam];
            }

            if (!Parameters.ContainsKey(PoolPasswordParam))
            {
                throw new Exception("" + PoolPasswordParam + " parameter not found");
            }
            else
            {
                poolPassword = Parameters[PoolPasswordParam];
            }

            /*Поднимем соедиения для пула*/
            for (int i = 0; i < poolSize; i++)
            {
                AbstractAdapter adapter = (AbstractAdapter)Activator.CreateInstance(adapterType);
                adapter.Parameters = Parameters;
                PoolableConnection pc = new PoolableConnection();
                pc.Adapter = adapter;

                connections.Add(adapter.Guid, pc);

                adapter.Init();
            }

            /*Запустим обработчик очереди ожидания*/
            waitQueueProcessor          = new Thread(new ThreadStart(this.ProcessWaitQueue));
            waitQueueProcessor.Priority = ThreadPriority.AboveNormal;
            waitQueueProcessor.Name     = "WaitQueueProcessor";
            waitQueueProcessor.Start();
        }
Ejemplo n.º 7
0
        public void TestGetAndReleaseConnections()
        {
            ConnectionPool pool = new ConnectionPool();

            pool.Logger = new ConsoleLogger();

            pool.Parameters.Add(ConnectionPool.AdapterTypeParam, "OneCService2.V81Adapter");
            pool.Parameters.Add(ConnectionPool.NameParam, "TestName");
            pool.Parameters.Add(ConnectionPool.PoolSizeParam, "3");
            pool.Parameters.Add(ConnectionPool.PoolUserNameParam, "PoolUserName");
            pool.Parameters.Add(ConnectionPool.PoolPasswordParam, "PoolPassword");

            pool.Parameters.Add(V81Adapter.ModeParam, "File");
            pool.Parameters.Add(V81Adapter.FileParam, @"C:\Work\1C\Test");
            pool.Parameters.Add(V81Adapter.UserNameParam, "");
            pool.Parameters.Add(V81Adapter.PasswordParam, "");

            try
            {
                pool.Init();

                /*Неправильная аутентификация*/
                try
                {
                    AbstractAdapter adapter = pool.GetConnection("AAA", "BBB");
                    Assert.IsTrue(false);
                }
                catch (Exception _e)
                {
                    Console.WriteLine(_e.ToString());
                }

                AbstractAdapter adapter1 = pool.GetConnection("PoolUserName", "PoolPassword");
                AbstractAdapter adapter2 = pool.GetConnection("PoolUserName", "PoolPassword");
                AbstractAdapter adapter3 = pool.GetConnection("PoolUserName", "PoolPassword");

                try
                {
                    /*Должно отвалится по таймауту*/
                    pool.GetConnection("PoolUserName", "PoolPassword");
                    Assert.IsTrue(false);
                }
                catch (Exception _e)
                {
                    Console.WriteLine(_e.ToString());
                }

                pool.ReleaseConnection(adapter1);
                pool.ReleaseConnection(adapter2);
                pool.ReleaseConnection(adapter3);

                /*Берем и не отдаем назад соединение - должно все равно закрыться*/
                adapter1 = pool.GetConnection("PoolUserName", "PoolPassword");

                Assert.IsTrue(true);
            }
            finally
            {
                pool.Done();
            }

            Assert.IsTrue(true);
        }