Java Code Examples for com.datastax.driver.core.Cluster.Builder#withPoolingOptions()

The following examples show how to use com.datastax.driver.core.Cluster.Builder#withPoolingOptions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraConfig.java    From realtime-analytics with GNU General Public License v2.0 6 votes vote down vote up
private void copyPoolingOptions(Builder builder) {
    PoolingOptions opts = new PoolingOptions();

    opts.setCoreConnectionsPerHost(HostDistance.REMOTE,
            remoteCoreConnectionsPerHost);
    opts.setCoreConnectionsPerHost(HostDistance.LOCAL,
            localCoreConnectionsPerHost);
    opts.setMaxConnectionsPerHost(HostDistance.REMOTE,
            remoteMaxConnectionsPerHost);
    opts.setMaxConnectionsPerHost(HostDistance.LOCAL,
            localMaxConnectionsPerHost);
    opts.setMaxSimultaneousRequestsPerConnectionThreshold(
            HostDistance.REMOTE,
            remoteMaxSimultaneousRequestsPerConnectionThreshold);
    opts.setMaxSimultaneousRequestsPerConnectionThreshold(
            HostDistance.LOCAL,
            localMaxSimultaneousRequestsPerConnectionThreshold);
    opts.setMinSimultaneousRequestsPerConnectionThreshold(
            HostDistance.REMOTE,
            remoteMinSimultaneousRequestsPerConnectionThreshold);
    opts.setMinSimultaneousRequestsPerConnectionThreshold(
            HostDistance.LOCAL,
            localMinSimultaneousRequestsPerConnectionThreshold);

    builder.withPoolingOptions(opts);
}
 
Example 2
Source File: CassandraConfig.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private Builder populatePoolingSettings(Map<String, String> properties, Builder builder) {
    String localCoreConnectionsPerHost = properties.get(DBConstants.Cassandra.LOCAL_CORE_CONNECTIONS_PER_HOST);
    String remoteCoreConnectionsPerHost = properties.get(DBConstants.Cassandra.REMOTE_CORE_CONNECTIONS_PER_HOST);
    String localMaxConnectionsPerHost = properties.get(DBConstants.Cassandra.LOCAL_MAX_CONNECTIONS_PER_HOST);
    String remoteMaxConnectionsPerHost = properties.get(DBConstants.Cassandra.REMOTE_MAX_CONNECTIONS_PER_HOST);
    String localNewConnectionThreshold = properties.get(DBConstants.Cassandra.LOCAL_NEW_CONNECTION_THRESHOLD);
    String remoteNewConnectionThreshold = properties.get(DBConstants.Cassandra.REMOTE_NEW_CONNECTION_THRESHOLD);
    String localMaxRequestsPerConnection = properties.get(DBConstants.Cassandra.LOCAL_MAX_REQUESTS_PER_CONNECTION);
    String remoteMaxRequestsPerConnection = properties.get(DBConstants.Cassandra.REMOTE_MAX_REQUESTS_PER_CONNECTION);
    PoolingOptions options = new PoolingOptions();
    if (localCoreConnectionsPerHost != null) {
        options.setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.parseInt(localCoreConnectionsPerHost));
    }
    if (remoteCoreConnectionsPerHost != null) {
        options.setCoreConnectionsPerHost(HostDistance.REMOTE, Integer.parseInt(remoteCoreConnectionsPerHost));
    }
    if (localMaxConnectionsPerHost != null) {
        options.setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.parseInt(localMaxConnectionsPerHost));
    }
    if (remoteMaxConnectionsPerHost != null) {
        options.setMaxConnectionsPerHost(HostDistance.REMOTE, Integer.parseInt(remoteMaxConnectionsPerHost));
    }
    if (localNewConnectionThreshold != null) {
        options.setNewConnectionThreshold(HostDistance.LOCAL, Integer.parseInt(localNewConnectionThreshold));
    }
    if (remoteNewConnectionThreshold != null) {
        options.setNewConnectionThreshold(HostDistance.REMOTE, Integer.parseInt(remoteNewConnectionThreshold));
    }
    if (localMaxRequestsPerConnection != null) {
        options.setMaxRequestsPerConnection(HostDistance.LOCAL, Integer.parseInt(localMaxRequestsPerConnection));
    }
    if (remoteMaxRequestsPerConnection != null) {
        options.setMaxRequestsPerConnection(HostDistance.REMOTE, Integer.parseInt(remoteMaxRequestsPerConnection));
    }
    builder = builder.withPoolingOptions(options);
    return builder;
}
 
Example 3
Source File: CassandraFactory.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
	 * 描述: 初始化配置
	 * 时间: 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);
		}
	}
 
Example 4
Source File: CassandraClusterCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
@Override
public Cluster create(CassandraServiceInfo serviceInfo,
		ServiceConnectorConfig serviceConnectorConfig) {

	Builder builder = Cluster.builder()
			.addContactPoints(serviceInfo.getContactPoints().toArray(new String[0]))
			.withPort(serviceInfo.getPort());

	if (StringUtils.hasText(serviceInfo.getUsername())) {
		builder.withCredentials(serviceInfo.getUsername(), serviceInfo.getPassword());
	}

	if (serviceConnectorConfig instanceof CassandraClusterConfig) {

		CassandraClusterConfig config = (CassandraClusterConfig) serviceConnectorConfig;

		if (config.getCompression() != null) {
			builder.withCompression(config.getCompression());
		}

		builder.withPoolingOptions(config.getPoolingOptions());
		builder.withSocketOptions(config.getSocketOptions());
		builder.withQueryOptions(config.getQueryOptions());
		builder.withNettyOptions(config.getNettyOptions());
		builder.withLoadBalancingPolicy(config.getLoadBalancingPolicy());
		builder.withReconnectionPolicy(config.getReconnectionPolicy());
		builder.withRetryPolicy(config.getRetryPolicy());

		if (config.getProtocolVersion() != null) {
			builder.withProtocolVersion(config.getProtocolVersion());
		}

		if (!config.isMetricsEnabled()) {
			builder.withoutMetrics();
		}

		if (!config.isJmxReportingEnabled()) {
			builder.withoutJMXReporting();
		}
	}

	return builder.build();
}