Example usage for org.apache.commons.pool2 PooledObject getCreateTime

List of usage examples for org.apache.commons.pool2 PooledObject getCreateTime

Introduction

In this page you can find the example usage for org.apache.commons.pool2 PooledObject getCreateTime.

Prototype

long getCreateTime();

Source Link

Document

Obtain the time (using the same basis as System#currentTimeMillis() ) that this object was created.

Usage

From source file:JDBCPool.dbcp.demo.sourcecode.PoolableConnectionFactory.java

/**
 * ?Connection//from ww  w . ja va2  s.  c o m
 * @param p
 * @throws Exception
 */
private void validateLifetime(PooledObject<PoolableConnection> p) throws Exception {
    if (maxConnLifetimeMillis > 0) {
        long lifetime = System.currentTimeMillis() - p.getCreateTime();
        if (lifetime > maxConnLifetimeMillis) {
            throw new LifetimeExceededException(Utils.getMessage("connectionFactory.lifetimeExceeded",
                    Long.valueOf(lifetime), Long.valueOf(maxConnLifetimeMillis)));
        }
    }
}

From source file:net.sf.jasperreports.phantomjs.ProcessFactory.java

@Override
public void activateObject(PooledObject<PhantomJSProcess> pooledObject) throws Exception {
    super.activateObject(pooledObject);

    PhantomJSProcess process = pooledObject.getObject();
    if (process.hasEnded()) {
        if (log.isDebugEnabled()) {
            log.debug(process.getId() + " has ended");
        }//  w ww  . j  a  v a2s .c om

        throw new JRRuntimeException("Process " + process.getId() + " has ended");
    }

    long borrowedCount = ((DefaultPooledObject<PhantomJSProcess>) pooledObject).getBorrowedCount();
    if (borrowedCount >= expirationCount) {
        if (log.isDebugEnabled()) {
            log.debug(process.getId() + " borrow count " + borrowedCount + " exceeded expiration count "
                    + expirationCount);
        }

        throw new JRRuntimeException("Process " + process.getId() + " borrow count exceeded");
    }

    long now = System.currentTimeMillis();
    if (now >= pooledObject.getCreateTime() + expirationTime) {
        if (log.isDebugEnabled()) {
            log.debug(process.getId() + " expiration time " + expirationTime + " from "
                    + pooledObject.getCreateTime() + " exceeded");
        }

        throw new JRRuntimeException("Process " + process.getId() + " expiration time exceeded");
    }
}