本文整理汇总了Java中com.datastax.driver.core.PoolingOptions.setPoolTimeoutMillis方法的典型用法代码示例。如果您正苦于以下问题:Java PoolingOptions.setPoolTimeoutMillis方法的具体用法?Java PoolingOptions.setPoolTimeoutMillis怎么用?Java PoolingOptions.setPoolTimeoutMillis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.datastax.driver.core.PoolingOptions
的用法示例。
在下文中一共展示了PoolingOptions.setPoolTimeoutMillis方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: build
import com.datastax.driver.core.PoolingOptions; //导入方法依赖的package包/类
public PoolingOptions build() {
PoolingOptions poolingOptions = new PoolingOptions();
if (local != null) {
setPoolingOptions(poolingOptions, HostDistance.LOCAL, local);
}
if (remote != null) {
setPoolingOptions(poolingOptions, HostDistance.REMOTE, remote);
}
if (heartbeatInterval != null) {
poolingOptions.setHeartbeatIntervalSeconds((int) heartbeatInterval.toSeconds());
}
if (poolTimeout != null) {
poolingOptions.setPoolTimeoutMillis((int) poolTimeout.toMilliseconds());
}
if (idleTimeout != null) {
poolingOptions.setIdleTimeoutSeconds((int) idleTimeout.toSeconds());
}
return poolingOptions;
}
示例2: init
import com.datastax.driver.core.PoolingOptions; //导入方法依赖的package包/类
/**
* 描述: 初始化配置
* 时间: 2017年11月15日 上午11:25:07
* @author yi.zhang
* @param servers 服务地址
* @param keyspace 命名空间
* @param username 账号
* @param password 密码
*/
public void init(String servers,String keyspace,String username,String password) {
try {
// socket 链接配置
SocketOptions socket = new SocketOptions();
socket.setKeepAlive(true);
socket.setReceiveBufferSize(1024* 1024);
socket.setSendBufferSize(1024* 1024);
socket.setConnectTimeoutMillis(5 * 1000);
socket.setReadTimeoutMillis(1000);
//设置连接池
PoolingOptions pool = new PoolingOptions();
// pool.setMaxRequestsPerConnection(HostDistance.LOCAL, 32);
// pool.setMaxRequestsPerConnection(HostDistance.REMOTE, 32);
// pool.setCoreConnectionsPerHost(HostDistance.LOCAL, 2);
// pool.setCoreConnectionsPerHost(HostDistance.REMOTE, 2);
// pool.setMaxConnectionsPerHost(HostDistance.LOCAL, 4);
// pool.setMaxConnectionsPerHost(HostDistance.REMOTE, 4);
pool.setHeartbeatIntervalSeconds(60);
pool.setIdleTimeoutSeconds(120);
pool.setPoolTimeoutMillis(5 * 1000);
List<InetSocketAddress> saddress = new ArrayList<InetSocketAddress>();
if (servers != null && !"".equals(servers)) {
for (String server : servers.split(",")) {
String[] address = server.split(":");
String ip = address[0];
int port = 9042;
if (address != null && address.length > 1) {
port = Integer.valueOf(address[1]);
}
saddress.add(new InetSocketAddress(ip, port));
}
}
InetSocketAddress[] addresses = new InetSocketAddress[saddress.size()];
saddress.toArray(addresses);
Builder builder = Cluster.builder();
builder.withSocketOptions(socket);
// 设置压缩方式
builder.withCompression(ProtocolOptions.Compression.LZ4);
// 负载策略
// DCAwareRoundRobinPolicy loadBalance = DCAwareRoundRobinPolicy.builder().withLocalDc("localDc").withUsedHostsPerRemoteDc(2).allowRemoteDCsForLocalConsistencyLevel().build();
// builder.withLoadBalancingPolicy(loadBalance);
// 重试策略
builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE);
builder.withPoolingOptions(pool);
builder.addContactPointsWithPorts(addresses);
builder.withCredentials(username, password);
Cluster cluster = builder.build();
if (keyspace != null && !"".equals(keyspace)) {
session = cluster.connect(keyspace);
} else {
session = cluster.connect();
}
mapping = new MappingManager(session);
} catch (Exception e) {
logger.error("-----Cassandra Config init Error-----", e);
}
}
示例3: createStandaloneConnection
import com.datastax.driver.core.PoolingOptions; //导入方法依赖的package包/类
/**
* Method to create the standalone cassandra connection .
*
* @param ip
* @param port
* @param userName
* @param password
* @param keyspace
* @return
*/
private boolean createStandaloneConnection(String ip, String port, String userName,
String password, String keyspace) {
Session cassandraSession = null;
boolean connection = false;
Cluster cluster = null;
try {
if (null == cassandraSessionMap.get(keyspace)) {
PropertiesCache cache = PropertiesCache.getInstance();
PoolingOptions poolingOptions = new PoolingOptions();
poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL,
Integer.parseInt(cache.getProperty(Constants.CORE_CONNECTIONS_PER_HOST_FOR_LOCAL)));
poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL,
Integer.parseInt(cache.getProperty(Constants.MAX_CONNECTIONS_PER_HOST_FOR_LOCAl)));
poolingOptions.setCoreConnectionsPerHost(HostDistance.REMOTE,
Integer.parseInt(cache.getProperty(Constants.CORE_CONNECTIONS_PER_HOST_FOR_REMOTE)));
poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE,
Integer.parseInt(cache.getProperty(Constants.MAX_CONNECTIONS_PER_HOST_FOR_REMOTE)));
poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL,
Integer.parseInt(cache.getProperty(Constants.MAX_REQUEST_PER_CONNECTION)));
poolingOptions.setHeartbeatIntervalSeconds(
Integer.parseInt(cache.getProperty(Constants.HEARTBEAT_INTERVAL)));
poolingOptions
.setPoolTimeoutMillis(Integer.parseInt(cache.getProperty(Constants.POOL_TIMEOUT)));
if (!ProjectUtil.isStringNullOREmpty(userName)
&& !ProjectUtil.isStringNullOREmpty(password)) {
cluster = createCluster(ip, port, userName, password, poolingOptions);
} else {
cluster = createCluster(ip, port, poolingOptions);
}
QueryLogger queryLogger = QueryLogger.builder().withConstantThreshold(
Integer.parseInt(cache.getProperty(Constants.QUERY_LOGGER_THRESHOLD))).build();
cluster.register(queryLogger);
cassandraSession = cluster.connect(keyspace);
if (null != cassandraSession) {
connection = true;
cassandraSessionMap.put(keyspace, cassandraSession);
cassandraclusterMap.put(keyspace, cluster);
}
final Metadata metadata = cluster.getMetadata();
String msg = String.format("Connected to cluster: %s", metadata.getClusterName());
ProjectLogger.log(msg);
for (final Host host : metadata.getAllHosts()) {
msg = String.format("Datacenter: %s; Host: %s; Rack: %s", host.getDatacenter(),
host.getAddress(), host.getRack());
ProjectLogger.log(msg);
}
}
} catch (Exception e) {
ProjectLogger.log("Error occured while creating connection :", e);
throw new ProjectCommonException(ResponseCode.internalError.getErrorCode(), e.getMessage(),
ResponseCode.SERVER_ERROR.getResponseCode());
}
if (null != cassandraSessionMap.get(keyspace)) {
connection = true;
}
return connection;
}
示例4: start
import com.datastax.driver.core.PoolingOptions; //导入方法依赖的package包/类
@Override
public void start() {
try {
PoolingOptions poolingOptions = new PoolingOptions();
CassandraStoreConnectionPoolConfiguration poolConfig = configuration.connectionPool();
poolingOptions.setPoolTimeoutMillis(poolConfig.poolTimeoutMillis());
poolingOptions.setHeartbeatIntervalSeconds(poolConfig.heartbeatIntervalSeconds());
poolingOptions.setIdleTimeoutSeconds(poolConfig.idleTimeoutSeconds());
Cluster.Builder builder = Cluster.builder();
if (configuration.useSsl()) {
builder.withSSL();
}
if (!configuration.username().isEmpty()) {
builder.withCredentials(configuration.username(), configuration.password());
System.out.println("configuration.username() = " + configuration.username());
System.out.println("configuration.password() = " + configuration.password());
}
builder.withPoolingOptions(poolingOptions);
ArrayList<InetSocketAddress> servers = new ArrayList<>();
for (CassandraStoreServerConfiguration cassandraStoreServerConfiguration : configuration.servers()) {
servers.add(new InetSocketAddress(cassandraStoreServerConfiguration.host(), cassandraStoreServerConfiguration.port()));
}
builder.addContactPointsWithPorts(servers);
cluster = builder.build();
if (configuration.autoCreateKeyspace()) {
createKeySpace();
}
session = cluster.connect(configuration.keyspace());
entryTable = configuration.entryTable();
writeStatement = session.prepare("INSERT INTO " + entryTable + " (key, value, metadata) VALUES (?, ?, ?) USING TTL ?");
writeStatement.setConsistencyLevel(configuration.writeConsistencyLevel());
writeStatement.setSerialConsistencyLevel(configuration.writeSerialConsistencyLevel());
deleteStatement = session.prepare("DELETE FROM " + entryTable + " WHERE key=?");
deleteStatement.setConsistencyLevel(configuration.writeConsistencyLevel());
deleteStatement.setSerialConsistencyLevel(configuration.writeSerialConsistencyLevel());
selectStatement = session.prepare("SELECT value, metadata FROM " + entryTable + " WHERE key=?");
selectStatement.setConsistencyLevel(configuration.readConsistencyLevel());
selectStatement.setSerialConsistencyLevel(configuration.readSerialConsistencyLevel());
containsStatement = session.prepare("SELECT key FROM " + entryTable + " WHERE key=?");
selectAllStatement = session.prepare("SELECT key, value, metadata FROM " + entryTable);
selectAllStatement.setConsistencyLevel(configuration.readConsistencyLevel());
selectAllStatement.setSerialConsistencyLevel(configuration.readSerialConsistencyLevel());
sizeStatement = session.prepare("SELECT count(*) FROM " + entryTable);
clearStatement = session.prepare("TRUNCATE " + entryTable);
} catch (Exception e) {
throw log.errorCommunicating(e);
}
entryTable = configuration.entryTable();
log.debug("Cassandra cache store started.");
}