Example #1
0
        public void SetFactory(IPoolableObjectFactory <T> factory)
        {
            var oldFactory = this.factory;
            var idleObjs   = new List <T>();

            lock (this)
            {
                AssertOpen();
                if (Active > 0)
                {
                    throw new InvalidOperationException("Cannot change factory with active object in the pool.");
                }
                else
                {
                    idleObjs.AddRange(this.idlePool);
                    idlePool.Clear();
                }
                this.factory = factory;
            }

            foreach (var obj in idleObjs)
            {
                oldFactory.Destroy(obj);
            }
        }
Example #2
0
 /// <summary>
 /// Creates a new instance of the <see cref="Spring.Pool.Support.SimplePool"/>
 /// class.
 /// </summary>
 /// <param name="factory">
 /// The factory used to instantiate and manage the lifecycle of pooled objects.
 /// </param>
 /// <param name="size">The initial size of the pool.</param>
 /// <exception cref="System.ArgumentNullException">
 /// If the supplied <paramref name="factory"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="System.ArgumentException">
 /// If the supplied <paramref name="size"/> is less than or equal to zero.
 /// </exception>
 public SimplePool(IPoolableObjectFactory factory, int size)
 {
     AssertUtils.ArgumentNotNull(factory, "factory");
     this.available = new Semaphore(size);
     this.factory   = factory;
     InitItems(size);
 }
Example #3
0
 public AutoSizePool(IPoolableObjectFactory factory)
 {
     activeObjects         = new List <object>();
     passiveObjects        = new List <object>();
     sync                  = new object();
     PoolableObjectFactory = factory;
 }
Example #4
0
        public ObjectPool(IPoolableObjectFactory <T> factory, ObjectPool <T> .Configuration config)
        {
            this.isClosed       = false;
            this.factory        = factory;
            this.idlePool       = new Queue <T>();
            this.activePool     = new List <T>();
            this.autoResetEvent = new AutoResetEvent(false);
            this.minSize        = config.MinSize;
            this.maxSize        = config.MaxSize;
            this.timeout        = config.Timeout;

            if (minSize > maxSize)
            {
                minSize = maxSize;
            }

            this.available = maxSize;

            for (var i = 0; i < minSize; i++)
            {
                Add();
            }

            autoResetEvent.Set();
        }
Example #5
0
        public void SetUp()
        {
            factory = A.Fake <IPoolableObjectFactory>();
            A.CallTo(() => factory.MakeObject()).Returns(new object());

            pool = new SimplePool(factory, 1);
        }
 public AutoSizePool(IPoolableObjectFactory factory)
 {
     activeObjects = new List<object>();
     passiveObjects = new List<object>();
     sync = new object();
     PoolableObjectFactory = factory;
 }
Example #7
0
		/// <summary>
		/// Creates a new instance of the <see cref="Spring.Pool.Support.SimplePool"/>
		/// class.
		/// </summary>
		/// <param name="factory">
		/// The factory used to instantiate and manage the lifecycle of pooled objects.
		/// </param>
		/// <param name="size">The initial size of the pool.</param>
		/// <exception cref="System.ArgumentNullException">
		/// If the supplied <paramref name="factory"/> is <see langword="null"/>.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// If the supplied <paramref name="size"/> is less than or equal to zero.
		/// </exception>
		public SimplePool(IPoolableObjectFactory factory, int size)
		{
			AssertUtils.ArgumentNotNull(factory, "factory");
			this.available = new Semaphore(size);
			this.factory = factory;
			InitItems(size);
		}
Example #8
0
 public GameObjectPool(IPoolableObjectFactory factory, int maxNum)
 {
     this.factory     = factory;
     this.m_activeNum = 0;
     this.m_maxNum    = maxNum;
     this.m_pool      = new Stack <Object>();
 }
        public DefaultCommunicationService(ICommunicationServiceConfig config,
            IPoolableObjectFactory<IMessageSenderChannel> senderChannelFactory,
            IPoolableObjectFactory<IMessageReceiverChannel> receiverChannelFactory, 
            string name = "DefaultCommunicationService",
            Int32 instanceNum = 1) : base(name, instanceNum)
        {
            var senderChannelPoolConfig = new ObjectPoolConfig<IMessageSenderChannel>.Builder()
                {
                    SetValidateBeforeBorrow = true,
                    SetCapacity = config.TotalSenderChannels,
                    SetMaximumObjectsActiveOnStartup = config.TotalSenderChannels,
                    Factory = senderChannelFactory
                }.Build();
            _senderChannelPool = new DefaultMessageSenderChannelPool(senderChannelPoolConfig);

            var receiverChannelPoolConfig = new ObjectPoolConfig<IMessageReceiverChannel>.Builder()
            {
                SetCapacity = config.TotalReceiverChannels,
                SetMaximumObjectsActiveOnStartup = config.TotalReceiverChannels,
                SetValidateBeforeBorrow = true,
                Factory = receiverChannelFactory
            }.Build();
            _receiverChannelPool = new DefaultMessageReceiverChannelPool(
                receiverChannelPoolConfig, config.UniqueResponseTopic);
        }
        public void SetUp()
        {
            mocks   = new MockRepository();
            factory = (IPoolableObjectFactory)mocks.DynamicMock(typeof(IPoolableObjectFactory));
            Expect.Call(factory.MakeObject()).Return(new object()).Repeat.Any();

            mocks.ReplayAll();
            pool = new SimplePool(factory, 1);

            mocks.BackToRecordAll();
        }
Example #11
0
 public AbstractObjectPool(int minInstances, int maxInstances, int waitTime, IPoolableObjectFactory <T> poolableObjectFactory)
 {
     Console.WriteLine("=========== STARTING ============");
     this.MinInstances          = minInstances;
     this.MaxInstances          = maxInstances;
     this.WaitTime              = waitTime;
     this.PoolableObjectFactory = poolableObjectFactory;
     InitPool();
     Console.WriteLine("=========== FINISH ============");
     Console.WriteLine();
 }
Example #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="minSize"></param>
        /// <param name="maxSize"></param>
        public ObjectPool(
            IPoolableObjectFactory <T> factory,
            int minSize,
            int maxSize)
        {
            Assert.IsNotNull(factory);
            FoundationContract.Requires <ArgumentOutOfRangeException>(minSize >= 0);
            FoundationContract.Requires <ArgumentOutOfRangeException>(minSize <= maxSize);

            _factory = factory;
            MinSize  = minSize;
            MaxSize  = maxSize;
            _timer   = new Timer(TimerCallback, null, 30000, 30000);
        }
Example #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="minSize"></param>
        /// <param name="maxSize"></param>
        public ObjectPool(
            IPoolableObjectFactory <T> factory,
            int minSize,
            int maxSize)
        {
            Assert.IsNotNull(factory);
            Assert.IsInRange(minSize >= 0);
            Assert.IsInRange(minSize <= maxSize);

            _factory = factory;
            MinSize  = minSize;
            MaxSize  = maxSize;
            _timer   = new Timer(TimerCallback, null, 30000, 30000);
        }
        public ObjectPool(IPoolableObjectFactory <T> factory, int size)
        {
            if (null == factory)
            {
                if (null != _logger)
                {
                    _logger.Error("创建对象池时发生异常,对象池化工厂不能为空");
                }
                throw new ArgumentNullException("factory", "对象创建工厂不能为空!");
            }
            _factory = factory;
            InitItems(size);

            if (null != _logger)
            {
                _logger.InfoFormat("对象池已经创建,对象池长度为:{0}", size);
            }
        }
Example #15
0
        public void SetUp()
        {
            mocks = new MockRepository();
            factory = (IPoolableObjectFactory) mocks.DynamicMock(typeof(IPoolableObjectFactory));
            Expect.Call(factory.MakeObject()).Return(new object()).Repeat.Any();

            mocks.ReplayAll();
            pool = new SimplePool(factory, 1);

            mocks.BackToRecordAll();
        }
Example #16
0
 /// <summary>
 /// 获取对象池接口
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="factory"></param>
 /// <returns></returns>
 public static IObjectPool <T> GetObjectPool <T>(IPoolableObjectFactory <T> factory) where T : class
 {
     return(new DefaultPool <T>(factory));
 }
 public GenericObjectPool(IPoolableObjectFactory pof)
 {
     _factory = pof;
     _pool    = new LinkedList <object>();
 }
Example #18
0
 /// <summary>
 /// 构造函数,使用默认池大小
 /// </summary>
 /// <param name="factory"></param>
 public DefaultPool(IPoolableObjectFactory <T> factory) : this(factory, 0)
 {
 }
Example #19
0
 /// <summary>
 /// 构造函数,默认池大小为16
 /// </summary>
 /// <param name="factory">the factory used to instantiate and manage lifecycle of pooles object</param>
 /// <param name="size">对象池大小</param>
 public DefaultPool(IPoolableObjectFactory <T> factory, int size)
 {
     this.factory = factory;
     this.size    = size <= 0 ? 16 : size;
     Init(this.size);
 }
Example #20
0
 /// <summary>
 /// Creates a new instance of the <see cref="Spring.Pool.Support.SimplePool"/>
 /// class.
 /// </summary>
 /// <param name="factory">
 /// The factory used to instantiate and manage the lifecycle of pooled objects.
 /// </param>
 /// <param name="initialSize">The initial size of the pool.</param>
 /// <exception cref="System.ArgumentNullException">
 /// If the supplied <paramref name="factory"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="System.ArgumentException">
 /// If the supplied <paramref name="initialSize"/> is less than or equal to zero.
 /// </exception>
 public SimplePool(IPoolableObjectFactory factory, int initialSize)
     : this(factory, initialSize, initialSize)
 {
 }
 public StubAutoSizePool(IPoolableObjectFactory factory)
     : base(factory)
 {
 }
 public ExecutorThreadPool(int minInstances, int maxInstances, int waitTime, IPoolableObjectFactory <ExecutorTask> poolableObjectFactory)
     : base(minInstances, maxInstances, waitTime, poolableObjectFactory)
 {
 }
 public void Init()
 {
     repo = new MockRepository();
      factory = repo.StrictMock<IPoolableObjectFactory>();
      pool = new StubAutoSizePool(factory);
 }
Example #24
0
 public GenericObjectPoolFactory(IPoolableObjectFactory pof)
 {
     _factory = pof;
 }
 public IObjectPool CreatePool(IPoolableObjectFactory poolableObjectFactory)
 {
     return(new SimplePool(poolableObjectFactory, PoolSize));
 }
Example #26
0
		/// <summary>
		/// Creates a new instance of the <see cref="Spring.Pool.Support.SimplePool"/>
		/// class.
		/// </summary>
		/// <param name="factory">
		/// The factory used to instantiate and manage the lifecycle of pooled objects.
		/// </param>
		/// <param name="initialSize">The initial size of the pool.</param>
		/// <exception cref="System.ArgumentNullException">
		/// If the supplied <paramref name="factory"/> is <see langword="null"/>.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// If the supplied <paramref name="initialSize"/> is less than or equal to zero.
		/// </exception>
		public SimplePool(IPoolableObjectFactory factory, int initialSize)
            : this(factory, initialSize, initialSize)
		{
		}
 public IObjectPool CreatePool(IPoolableObjectFactory poolableObjectFactory)
 {
     return new AutoSizePool(poolableObjectFactory);
 }
 public IObjectPool CreatePool(IPoolableObjectFactory poolableObjectFactory)
 {
     return new SimplePool(poolableObjectFactory, PoolSize);
 }
Example #29
0
 /// <summary>
 /// Method SetFactory
 /// </summary>
 /// <param name="factory">An IPoolableObjectFactory<type></param>
 public void SetFactory(IPoolableObjectFactory <type> factory)
 {
     _factory = factory;
 }
Example #30
0
 public void Init()
 {
     repo    = new MockRepository();
     factory = repo.StrictMock <IPoolableObjectFactory>();
     pool    = new StubAutoSizePool(factory);
 }
 public IObjectPool CreatePool(IPoolableObjectFactory poolableObjectFactory)
 {
     return(new AutoSizePool(poolableObjectFactory));
 }
Example #32
0
 public IObjectPool CreatePool(IPoolableObjectFactory factory, int maxNum)
 {
     return(new GameObjectPool(factory, maxNum));
 }
Example #33
0
 public StubAutoSizePool(IPoolableObjectFactory factory)
     : base(factory)
 {
 }