Apache Commons Pool2 Source Analysis | Apache Commons Pool2 Source Code Analysis

Source: Internet
Author: User

Apache Commons PoolImplements 对象池 the function. Defines the operations of object generation, destruction, activation, passivation, and its state transitions, and provides several default object pool implementations.
Before telling the principle of implementation, let's mention a few important objects:

    • Pooledobject (Pool object).
    • Pooledobjectfactory (Pool object factory).
    • Object pool.

The following sections explain their implementation in detail.

pooledobject (Pool object)used to encapsulate objects such as threads, database connections, TCP connections, and wrap them into objects that can be managed by the pool. Two default pool object implementations are provided:
    • Defaultpoolobject. A generic object used for non-soft references.
    • Pooledsoftreference. The object used for soft references.
when developing components such as connection pooling, thread pooling, and so on, you need to overload 5 methods according to the actual situation: Startevictiontest, endevictiontest, allocate, deallocate, and invalidate, Used to modify the internal state of the wrapped object under different scenarios.

There are a variety 状态 of pooledobject that change in different stages or after treatment.

status span> description
IDLE In queue, not using
allocated in use
eviction in queue, currently under test May be recycled
eviction_return_to_head is not in the queue, it is currently being tested, and may be recycled. You need to remove an object from the pool and test it from the queue
VALIDATION is in the queue and is currently verifying
VALIDATION _preallocation is not in the queue and is currently being validated. When an object is loaned out of the pool, and when Testonborrow is configured, it is validated when it is removed from the queue and pre-allocated
validation_return_to_head is not in the queue , validation is in progress. When an object is loaned from a pool, it is tested first when the object is removed from the queue. You should do a full validation when returning to the head of the queue
INVALID Recycle or verify failure, will be destroyed
abandoned about to be invalid
return returned to the pool
depending on the default implementation of Apache Commons Pool2, its status changes as shown:pooledobjectfactory (Pool object factory) PooledObject Some methods that define the life cycle of an operation instance PooledObjectFactory must be thread-safe. There are already two abstract factories:
    • Basepooledobjectfactory.
    • Basekeyedpooledobjectfactory.
directly inherit them to implement their own pool object factory.
Method Describe
Makeobject Used to generate a new Objectpool instance
Activateobject Each Objectpool instance of a passivation (passivated) is called before it is Lent (borrowed) from the pool
Validateobject When you may be used to borrow an object from a pool, test the Objectpool instance in the active (activated) state to make sure it is valid. It is also possible to test for the validity of a call before passivation in the Objectpool instance return pool. It is called only for instances that are in the active state
Passivateobject Called when the Objectpool instance is returned to the pool
Destroyobject Called when the Objectpool instance is being cleaned out of the pool and discarded (whether or not the test results from Validateobject are determined by the specific implementation)
Object Pool Object PoolResponsible for managing Pooledobject, such as: Lending objects, returning objects, validating objects, how many active objects, and how many idle objects there are. There are three default implementation classes:
    • Genericobjectpool.
    • Prosiedobjectpool.
    • Softreferenceobjectpool.
Method Describe
Borrowobject Borrows an object from the pool. Either call the Pooledobjectfactory.makeobject method to create it, or activate it with Pooledobjectfactory.activeobject for a free object, and then use the POOLEDOBJECTFACTORY.VALIDATEOB Ject method is validated before returning
Returnobject Returns an object to the pool. By convention: An object must be borrowed from a pool using the Borrowobject method
Invalidateobject Discards an object. By convention: The object must be borrowed from the pool using the Borrowobject method. This method is typically used to discard an object when an exception or other problem occurs
AddObject Use the factory to create an object, passivation and put it into the free object pool
Getnumberidle Returns the number of idle objects in the pool. It is possible that the approximate value of the object in the pool is available for lending. If this information is invalid, a negative number is returned
Getnumactive Returns the number of objects from the loan. If this information is not available, a negative number is returned
Clear Clears all idle objects in the pool, optionally releasing their associated resources. Clearing idle objects must use the Pooledobjectfactory.destroyobject method
Close Close the pool and release the associated resources
Borrowobject (Loan object)The following is a logical implementation of the Borrowobject method in Genericobjectpool, with both blocking and non-blocking modes of acquiring objects.Returnobject (return object)The following is the logical implementation of the Returnobject method in Genericobjectpool, which implements FIFO (first-in, in-and-out) and LIFO (LIFO).References
    • Apache Commons Pool Overview
    • Apache Commons Pool API

http://aofengblog.blog.163.com/blog/static/6317021201463075826473/

http://commons.apache.org/proper/commons-pool/

http://commons.apache.org/proper/commons-pool/download_pool.cgi

Apache Commons Pool2 Source Analysis | Apache Commons Pool2 Source Code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.