當前位置: 首頁>>代碼示例>>Java>>正文


Java DefaultPooledObject類代碼示例

本文整理匯總了Java中org.apache.commons.pool2.impl.DefaultPooledObject的典型用法代碼示例。如果您正苦於以下問題:Java DefaultPooledObject類的具體用法?Java DefaultPooledObject怎麽用?Java DefaultPooledObject使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DefaultPooledObject類屬於org.apache.commons.pool2.impl包,在下文中一共展示了DefaultPooledObject類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
public PooledObject<Socket> makeObject() throws Exception {
	// 簡單策略隨機取服務器,沒有考慮權重
	List<ServerAddress> temps = new ArrayList<ServerAddress>();
	temps.addAll(serverGroup.getServers());

	for (int i = temps.size() - 1; i >= 0; i--) {
		int index = new Random(System.currentTimeMillis()).nextInt(temps.size());
		ServerAddress server = temps.get(index);
		try {
			PooledObject<Socket> po = new DefaultPooledObject<Socket>(
					new Socket(server.getIp(), server.getPort()));
			if (logger.isDebugEnabled()) {
				logger.debug("tcpPool makeObject[" + server.getIp() + ":" + server.getPort() + "] success!");
			}
			return po;
		} catch (Exception e) {
			temps.remove(i);
			logger.error("tcpPool makeObject[" + server.getIp() + ":" + server.getPort() + "] failed! "
					+ e.getMessage());
		}
	}
	// 沒有可用服務器時,請求一下registry更新服務器信息
	this.updateServerGroup();
	throw new Exception("No server available!");
}
 
開發者ID:sylinklee,項目名稱:netto_rpc,代碼行數:26,代碼來源:TcpConnectPool.java

示例2: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public PooledObject<Jedis> makeObject() throws Exception {
  final HostAndPort hostAndPort = this.hostAndPort.get();
  final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout,
      soTimeout);

  jedis.connect();
  if (null != this.password) {
    jedis.auth(this.password);
  }
  if (database != 0) {
    jedis.select(database);
  }
  if (clientName != null) {
    jedis.clientSetname(clientName);
  }

  return new DefaultPooledObject<Jedis>(jedis);
}
 
開發者ID:x7-framework,項目名稱:x7,代碼行數:20,代碼來源:JedisFactory.java

示例3: setupObjectPool

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public void setupObjectPool() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(poolSize);

    objectPool = new GenericObjectPool<>(new BasePooledObjectFactory<TestObject>() {
        @Override
        public TestObject create() throws Exception {
            return new TestObject(true);
        }

        @Override
        public PooledObject<TestObject> wrap(TestObject testObject) {
            return new DefaultPooledObject<>(testObject);
        }
    }, config);
}
 
開發者ID:chrishantha,項目名稱:microbenchmarks,代碼行數:18,代碼來源:CommonsPool2GenericObjectPoolBenchmark.java

示例4: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public PooledObject<Jedis> makeObject() throws Exception {
  final HostAndPort hostAndPort = this.hostAndPort.get();
  final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout,
      soTimeout);

  try {
    jedis.connect();
    if (null != this.password) {
      jedis.auth(this.password);
    }
    if (database != 0) {
      jedis.select(database);
    }
    if (clientName != null) {
      jedis.clientSetname(clientName);
    }
  } catch (JedisException je) {
    jedis.close();
    throw je;
  }

  return new DefaultPooledObject<Jedis>(jedis);

}
 
開發者ID:sohutv,項目名稱:cachecloud,代碼行數:26,代碼來源:JedisFactory.java

示例5: main

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
public static void main(String[] args) {
	log("PCFMessageAgent Test");
	final Properties p = new Properties();
	p.setProperty(HOST_KEY, "localhost");
	p.setProperty(PORT_KEY, "1430");
	p.setProperty(CHANNEL_KEY, "JBOSS.SVRCONN");
	MQPCFPoolBuilder poolBuilder = new MQPCFPoolBuilder(p);
	log("Created");
	log("PCF_CTOR: [" + PCF_CTOR + "]");
	Object o = null;
	PooledObject<Object> pooledObject = null;
	try {
		o = poolBuilder.create();
		log("Created Object: [" + o + "]");
		pooledObject = new DefaultPooledObject<Object>(o);
		log("Validating Object....");
		poolBuilder.validateObject(pooledObject);
		//SystemClock.sleep(100000);
		
	} finally {
		if(pooledObject!=null) try { poolBuilder.destroyObject(pooledObject); } catch (Exception x) {}
	}
	
	
}
 
開發者ID:nickman,項目名稱:HeliosStreams,代碼行數:26,代碼來源:MQPCFPoolBuilder.java

示例6: getPool

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
private synchronized GenericKeyedObjectPool<String, Script> getPool() {
    if (pool == null) {
        GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
        poolConfig.setMaxTotalPerKey(-1);
        poolConfig.setMaxIdlePerKey(globalConfig.getGroovyEvaluationPoolMaxIdle());
        pool = new GenericKeyedObjectPool<>(
                new BaseKeyedPooledObjectFactory<String, Script>() {
                    @Override
                    public Script create(String key) throws Exception {
                        return createScript(key);
                    }

                    @Override
                    public PooledObject<Script> wrap(Script value) {
                        return new DefaultPooledObject<>(value);
                    }
                },
                poolConfig
        );
    }
    return pool;
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:23,代碼來源:AbstractScripting.java

示例7: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public PooledObject<Jedis> makeObject() throws Exception {
	final Jedis jedis = new Jedis(this.host, this.port, this.timeout);

	jedis.connect();
	if (null != this.password) {
		jedis.auth(this.password);
	}
	if (database != 0) {
		jedis.select(database);
	}
	if (clientName != null) {
		jedis.clientSetname(clientName);
	}

	return new DefaultPooledObject<Jedis>(jedis);
}
 
開發者ID:pengqiuyuan,項目名稱:g2,代碼行數:18,代碼來源:JedisFactory.java

示例8: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public PooledObject<Jedis> makeObject() throws Exception {
	final HostAndPort hostAndPort = this.hostAndPort.get();
	final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout);

	jedis.connect();
	if (null != this.password) {
		jedis.auth(this.password);
	}
	if (database != 0) {
		jedis.select(database);
	}
	if (clientName != null) {
		jedis.clientSetname(clientName);
	}

	return new DefaultPooledObject<Jedis>(jedis);
}
 
開發者ID:nince-wyj,項目名稱:jahhan,代碼行數:19,代碼來源:JedisFactory.java

示例9: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
   public PooledObject<Jedis> makeObject() throws Exception {
   final HostAndPort hostAndPort = this.hostAndPort.get();
final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), this.timeout);

jedis.connect();
if (null != this.password) {
    jedis.auth(this.password);
}
if (database != 0) {
    jedis.select(database);
}
if (clientName != null) {
    jedis.clientSetname(clientName);
}

return new DefaultPooledObject<Jedis>(jedis);
   }
 
開發者ID:EdwardLee03,項目名稱:jedis-sr,代碼行數:19,代碼來源:JedisFactory.java

示例10: invoke

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    if (calledMethods == null) {
        return null;
    }
    calledMethods.add(method.getName());
    if (boolean.class.equals(method.getReturnType())) {
        return Boolean.FALSE;
    } else if (int.class.equals(method.getReturnType())) {
        return Integer.valueOf(0);
    } else if (long.class.equals(method.getReturnType())) {
        return Long.valueOf(0);
    } else if (Object.class.equals(method.getReturnType())) {
        return new Object();
    } else if (PooledObject.class.equals(method.getReturnType())) {
        return new DefaultPooledObject<>(new Object());
    } else {
        return null;
    }
}
 
開發者ID:apache,項目名稱:commons-pool,代碼行數:21,代碼來源:TestPoolUtils.java

示例11: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
/**
 * My {@link KeyedPooledObjectFactory} method for creating
 * {@link PreparedStatement}s.
 * @param key the key for the {@link PreparedStatement} to be created
 */
@Override
public PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> makeObject(final PStmtKeyCPDS key) throws Exception {
    if (null == key) {
        throw new IllegalArgumentException();
    }
    // _openPstmts++;
    if (null == key.getResultSetType()
            && null == key.getResultSetConcurrency()) {
        if (null == key.getAutoGeneratedKeys()) {
            return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
                    connection.prepareStatement(key.getSql()),
                    key, pstmtPool, delegatingConnection));
        }
        return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
                        connection.prepareStatement(key.getSql(),
                                key.getAutoGeneratedKeys().intValue()),
                        key, pstmtPool, delegatingConnection));
    }
    return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
            connection.prepareStatement(key.getSql(),
                    key.getResultSetType().intValue(),
                    key.getResultSetConcurrency().intValue()),
                    key, pstmtPool, delegatingConnection));
}
 
開發者ID:apache,項目名稱:commons-dbcp,代碼行數:30,代碼來源:PooledConnectionImpl.java

示例12: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public synchronized PooledObject<PooledConnectionAndInfo> makeObject() {
    PooledConnectionAndInfo pci;
    try {
        PooledConnection pc = null;
        if (_username == null) {
            pc = _cpds.getPooledConnection();
        } else {
            pc = _cpds.getPooledConnection(_username, _password);
        }

        if (pc == null) {
            throw new IllegalStateException("Connection pool data source returned null from getPooledConnection");
        }

        // should we add this object as a listener or the pool.
        // consider the validateObject method in decision
        pc.addConnectionEventListener(this);
        pci = new PooledConnectionAndInfo(pc, _username, _password);
        pcMap.put(pc, pci);
    } catch (final SQLException e) {
        throw new RuntimeException(e.getMessage());
    }
    return new DefaultPooledObject<>(pci);
}
 
開發者ID:apache,項目名稱:commons-dbcp,代碼行數:26,代碼來源:CPDSConnectionFactory.java

示例13: testValidateObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Test
public void testValidateObject() throws Exception {
    when(dirContextValidatorMock
            .validateDirContext(DirContextType.READ_ONLY, dirContextMock))
            .thenReturn(true);

    final DirContextPooledObjectFactory objectFactory = new DirContextPooledObjectFactory();
    objectFactory.setDirContextValidator(dirContextValidatorMock);

    PooledObject pooledObject = new DefaultPooledObject(dirContextMock);
    final boolean valid = objectFactory.validateObject(DirContextType.READ_ONLY, pooledObject);
    assertThat(valid).isTrue();

    //Check exception in validator
    DirContextValidator secondDirContextValidatorMock = mock(DirContextValidator.class);

    when(secondDirContextValidatorMock.validateDirContext(DirContextType.READ_ONLY, dirContextMock))
            .thenThrow(new RuntimeException("Failed to validate"));
    objectFactory.setDirContextValidator(secondDirContextValidatorMock);

    final boolean valid2 = objectFactory.validateObject(DirContextType.READ_ONLY, pooledObject);
    assertThat(valid2).isFalse();
}
 
開發者ID:spring-projects,項目名稱:spring-ldap,代碼行數:24,代碼來源:DirContextPooledObjectFactoryTest.java

示例14: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public PooledObject<BinaryJedis> makeObject() throws Exception {
    final HostAndPort hostAndPort = this.hostAndPort.get();
    final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), this.timeout);

    jedis.connect();
    if (null != this.password) {
        jedis.auth(this.password);
    }
    if (database != 0) {
        jedis.select(database);
    }
    if (clientName != null) {
        jedis.clientSetname(clientName);
    }

    return new DefaultPooledObject<BinaryJedis>(jedis);
}
 
開發者ID:yamingd,項目名稱:argo,代碼行數:19,代碼來源:BinaryJedisFactory.java

示例15: makeObject

import org.apache.commons.pool2.impl.DefaultPooledObject; //導入依賴的package包/類
@Override
public PooledObject<Jedis> makeObject() throws Exception {
    final HostAndPort hostAndPort = this.hostAndPort.get();
    JaRedis.Builder builder = new JaRedis.Builder();
    builder
            .host(hostAndPort.getHost())
            .port(hostAndPort.getPort())
            .connectionTimeout(connectionTimeout)
            .soTimeout(soTimeout);
    Jedis jedis = builder.build();
    try {
        jedis.connect();
        if (null != this.password) {
            jedis.auth(this.password);
        }
        if (database != 0) {
            jedis.select(database);
        }
        if (clientName != null) {
            jedis.clientSetname(clientName);
        }
    } catch (JedisException je) {
        jedis.close();
        throw je;
    }
    return new DefaultPooledObject<>(jedis);
}
 
開發者ID:YanXs,項目名稱:nighthawk,代碼行數:28,代碼來源:JaRedisFactory.java


注:本文中的org.apache.commons.pool2.impl.DefaultPooledObject類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。