@PostConstruct public void initOpts() { opts = new SocketOptions(); opts.setConnectTimeoutMillis(connectTimeoutMillis); opts.setReadTimeoutMillis(readTimeoutMillis); if (keepAlive != null) { opts.setKeepAlive(keepAlive); } if (reuseAddress != null) { opts.setReuseAddress(reuseAddress); } if (soLinger != null) { opts.setSoLinger(soLinger); } if (tcpNoDelay != null) { opts.setTcpNoDelay(tcpNoDelay); } if (receiveBufferSize != null) { opts.setReceiveBufferSize(receiveBufferSize); } if (sendBufferSize != null) { opts.setSendBufferSize(sendBufferSize); } }
public CassandraConfigDb(List<String> contactPoints, int port) { this.contactPoints = new ArrayList<InetAddress> (contactPoints.size()); for (String contactPoint : contactPoints) { try { this.contactPoints.add(InetAddress.getByName(contactPoint)); } catch (UnknownHostException e) { throw new IllegalArgumentException(e.getMessage()); } } this.port = port; cluster = (new Cluster.Builder()).withPort (this.port) .addContactPoints(this.contactPoints) .withSocketOptions(new SocketOptions().setReadTimeoutMillis(60000).setKeepAlive(true).setReuseAddress(true)) .withLoadBalancingPolicy(new RoundRobinPolicy()) .withReconnectionPolicy(new ConstantReconnectionPolicy(500L)) .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE)) .build (); session = cluster.newSession(); preparedStatements = new ConcurrentHashMap<StatementName, PreparedStatement> (); prepareStatementCreateLock = new Object(); }
static Session createSession() throws Exception { Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1") // long read timeout is sometimes needed on slow travis ci machines .withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000)) .withQueryOptions(getQueryOptions()) .build(); Session session = cluster.connect(); session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION =" + " { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); session.execute("CREATE TABLE IF NOT EXISTS test.users" + " (id int PRIMARY KEY, fname text, lname text)"); try { session.execute("TRUNCATE test.users"); } catch (NoHostAvailableException e) { // sometimes slow, so give it a second chance session.execute("TRUNCATE test.users"); } for (int i = 0; i < 10; i++) { session.execute("INSERT INTO test.users (id, fname, lname) VALUES (" + i + ", 'f" + i + "', 'l" + i + "')"); } return session; }
private static Cluster getCluster(){ if(cluster==null){ synchronized (SessionManager.class) { if(cluster==null){ PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions .setMaxRequestsPerConnection(HostDistance.REMOTE, max) .setMaxRequestsPerConnection(HostDistance.LOCAL,max) .setMaxQueueSize(max*10) .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) .setMaxConnectionsPerHost( HostDistance.LOCAL, 2) .setCoreConnectionsPerHost(HostDistance.REMOTE, 1) .setMaxConnectionsPerHost( HostDistance.REMOTE, 2); SocketOptions socketOptions = new SocketOptions(); socketOptions.setConnectTimeoutMillis(60000); socketOptions.setReadTimeoutMillis(60000); cluster = Cluster.builder().addContactPoint(url).withPoolingOptions(poolingOptions).withSocketOptions(socketOptions).build(); Metadata metadata = cluster.getMetadata(); Set<Host> allHosts = metadata.getAllHosts(); for(Host host:allHosts){ System.out.println("host:"+host.getAddress()); } } } } return cluster; }
@BeforeClass public static void startCassandra() throws Exception { //Start the Embedded Cassandra Service cassandra.start(); final SocketOptions socketOptions = new SocketOptions(); // Setting this to 0 disables read timeouts. socketOptions.setReadTimeoutMillis(0); // This defaults to 5 s. Increase to a minute. socketOptions.setConnectTimeoutMillis(60 * 1000); cluster = Cluster.builder().addContactPoint(CASSANDRA_HOST).withClusterName("beam") .withSocketOptions(socketOptions).build(); session = cluster.connect(); createCassandraData(); }
public static Cluster getInputCluster(String[] hosts, Configuration conf) { int port = getInputNativePort(conf); Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); Optional<Integer> protocolVersion = getProtocolVersion(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, hosts); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); if (protocolVersion.isPresent()) { builder.withProtocolVersion(protocolVersion.get()); } builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
private static SocketOptions getReadSocketOptions(Configuration conf) { SocketOptions socketOptions = new SocketOptions(); Optional<Integer> connectTimeoutMillis = getInputNativeConnectionTimeout(conf); Optional<Integer> readTimeoutMillis = getInputNativeReadConnectionTimeout(conf); Optional<Integer> receiveBufferSize = getInputNativeReceiveBufferSize(conf); Optional<Integer> sendBufferSize = getInputNativeSendBufferSize(conf); Optional<Integer> soLinger = getInputNativeSolinger(conf); Optional<Boolean> tcpNoDelay = getInputNativeTcpNodelay(conf); Optional<Boolean> reuseAddress = getInputNativeReuseAddress(conf); Optional<Boolean> keepAlive = getInputNativeKeepAlive(conf); if (connectTimeoutMillis.isPresent()) socketOptions.setConnectTimeoutMillis(connectTimeoutMillis.get()); if (readTimeoutMillis.isPresent()) socketOptions.setReadTimeoutMillis(readTimeoutMillis.get()); if (receiveBufferSize.isPresent()) socketOptions.setReceiveBufferSize(receiveBufferSize.get()); if (sendBufferSize.isPresent()) socketOptions.setSendBufferSize(sendBufferSize.get()); if (soLinger.isPresent()) socketOptions.setSoLinger(soLinger.get()); if (tcpNoDelay.isPresent()) socketOptions.setTcpNoDelay(tcpNoDelay.get()); if (reuseAddress.isPresent()) socketOptions.setReuseAddress(reuseAddress.get()); if (keepAlive.isPresent()) socketOptions.setKeepAlive(keepAlive.get()); return socketOptions; }
public CassandraConn(String node, String keyspace, String username, String password) { PoolingOptions pools = new PoolingOptions(); pools.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, maxRequestPerConnection); pools.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, minRequestPerConnection); pools.setCoreConnectionsPerHost(HostDistance.LOCAL, coreConnectionLocalPerHost); pools.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionLocalPerHost); pools.setCoreConnectionsPerHost(HostDistance.REMOTE, coreConnectionRemotePerHost); pools.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionRemotePerHost); cluster = Cluster.builder() .addContactPoint(node) .withPoolingOptions(pools) .withCredentials(username, password) .withSocketOptions(new SocketOptions().setTcpNoDelay(true)) .build(); Metadata metadata = cluster.getMetadata(); System.out.printf("Connected to cluster: %s\n", metadata.getClusterName()); for ( Host host : metadata.getAllHosts() ) { System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()); } session = cluster.connect(keyspace); }
public AsyncFuture<Connection> construct() { AsyncFuture<Session> session = async.call(() -> { // @formatter:off final PoolingOptions pooling = new PoolingOptions(); final QueryOptions queryOptions = new QueryOptions() .setFetchSize(fetchSize) .setConsistencyLevel(consistencyLevel); final SocketOptions socketOptions = new SocketOptions() .setReadTimeoutMillis((int) readTimeout.toMilliseconds()); final Cluster.Builder cluster = Cluster.builder() .addContactPointsWithPorts(seeds) .withRetryPolicy(retryPolicy) .withPoolingOptions(pooling) .withQueryOptions(queryOptions) .withSocketOptions(socketOptions) .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())); // @formatter:on authentication.accept(cluster); return cluster.build().connect(); }); if (configure) { session = session.lazyTransform(s -> { return schema.configure(s).directTransform(i -> s); }); } return session.lazyTransform(s -> { return schema.instance(s).directTransform(schema -> { return new Connection(s, schema); }); }); }
private void configureSocketOpts() { final String readTimeoutConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_READ_TIMEOUT); final String connectTimeoutConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_CONNECT_TIMEOUT); final SocketOptions socketOptions = builder.getConfiguration().getSocketOptions(); if (StringUtils.isNotEmpty(readTimeoutConfiguration)) { socketOptions.setReadTimeoutMillis(Integer.parseInt(readTimeoutConfiguration)); } if (StringUtils.isNotEmpty(connectTimeoutConfiguration)) { socketOptions.setConnectTimeoutMillis(Integer.parseInt(connectTimeoutConfiguration)); } builder = builder.withSocketOptions(socketOptions); }
private Cluster cluster() { return Cluster.builder() .addContactPoints(contactPoint).withPort(port) .withCredentials(username, password) .withSocketOptions(new SocketOptions().setKeepAlive(true) .setReadTimeoutMillis(readTimeoutMillis)) .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder() .withLocalDc(localDatacenter) .build()) .build(); }
/** * Sets socket options to use. * * @param options Socket options. */ @SuppressWarnings("UnusedDeclaration") public void setSocketOptions(SocketOptions options) { sockOptions = options; invalidate(); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { fetchSize = (Integer)in.readObject(); readConsistency = (ConsistencyLevel)in.readObject(); writeConsistency = (ConsistencyLevel)in.readObject(); user = U.readString(in); pwd = U.readString(in); port = (Integer)in.readObject(); contactPoints = (List<InetAddress>)in.readObject(); contactPointsWithPorts = (List<InetSocketAddress>)in.readObject(); maxSchemaAgreementWaitSeconds = (Integer)in.readObject(); protoVer = (Integer)in.readObject(); compression = U.readString(in); useSSL = (Boolean)in.readObject(); collectMetrix = (Boolean)in.readObject(); jmxReporting = (Boolean)in.readObject(); creds = (Credentials)in.readObject(); loadBalancingPlc = (LoadBalancingPolicy)readObject(in); reconnectionPlc = (ReconnectionPolicy)readObject(in); addrTranslator = (AddressTranslator)readObject(in); speculativeExecutionPlc = (SpeculativeExecutionPolicy)readObject(in); authProvider = (AuthProvider)readObject(in); sslOptions = (SSLOptions)readObject(in); poolingOptions = (PoolingOptions)readObject(in); sockOptions = (SocketOptions)readObject(in); nettyOptions = (NettyOptions)readObject(in); }
public static Cluster getCluster(String[] hosts, Configuration conf, int port) { Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); Optional<Integer> protocolVersion = getProtocolVersion(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(hosts); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); if (protocolVersion.isPresent()) { builder.withProtocolVersion(ProtocolVersion.fromInt(protocolVersion.get())); } builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
public static Cluster getInputCluster(String[] hosts, Configuration conf) { int port = getInputNativePort(conf); Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, hosts); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
static Cluster newCluster() { return Cluster.builder().addContactPoint("127.0.0.1") // long read timeout is sometimes needed on slow travis ci machines .withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000)) // let driver know that only idempotent queries are used so it will retry on timeout .withQueryOptions(new QueryOptions().setDefaultIdempotence(true)) .build(); }
public CqlSession(final String nodes, final int port, final String keyspace, final SocketOptions socketOptions, final RetryPolicy retryPolicy, final QueryOptions queryOptions, final LoadBalancingPolicy loadBalancingPolicy, final int maxConnectionsPerHost, final MetricFactory metricFactory) { // this is temp. to reuse current hosts properties: final Iterable<String> nodesIter = Splitter.on(",").split(nodes); final String[] nodesArr = Iterables.toArray( StreamSupport.stream(nodesIter.spliterator(), false).map(input -> { if (input == null) return null; final int idx = input.lastIndexOf(":"); return input.substring(0, idx); }).collect(Collectors.toList()), String.class); /*PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionsPerHost); poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionsPerHost);*/ final Cluster cluster = Cluster.builder(). withPort(port). withSocketOptions(socketOptions). withQueryOptions(queryOptions). withLoadBalancingPolicy(loadBalancingPolicy). // withPoolingOptions(poolingOptions). addContactPoints(nodesArr).build(); //cluster.init(); this.session = cluster.connect(keyspace); this.retryPolicy = Preconditions.checkNotNull(retryPolicy); this.metricFactory = Preconditions.checkNotNull(metricFactory); }
public static Cluster getInputCluster(String host, Configuration conf) { int port = getInputNativePort(conf); Optional<AuthProvider> authProvider = getAuthProvider(conf); Optional<SSLOptions> sslOptions = getSSLOptions(conf); LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, host); SocketOptions socketOptions = getReadSocketOptions(conf); QueryOptions queryOptions = getReadQueryOptions(conf); PoolingOptions poolingOptions = getReadPoolingOptions(conf); Cluster.Builder builder = Cluster.builder() .addContactPoint(host) .withPort(port) .withCompression(ProtocolOptions.Compression.NONE); if (authProvider.isPresent()) builder.withAuthProvider(authProvider.get()); if (sslOptions.isPresent()) builder.withSSL(sslOptions.get()); builder.withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions) .withQueryOptions(queryOptions) .withPoolingOptions(poolingOptions); return builder.build(); }
private Cluster.Builder populateSocketOptions(Properties properties, Cluster.Builder builder) { String connectionTimeoutMillisProp = properties.getProperty(CassandraStoreParameters.CONNECTION_TIMEOUT_MILLIS); String keepAliveProp = properties.getProperty(CassandraStoreParameters.KEEP_ALIVE); String readTimeoutMillisProp = properties.getProperty(CassandraStoreParameters.READ_TIMEOUT_MILLIS); String receiveBufferSizeProp = properties.getProperty(CassandraStoreParameters.RECEIVER_BUFFER_SIZE); String reuseAddress = properties.getProperty(CassandraStoreParameters.REUSE_ADDRESS); String sendBufferSize = properties.getProperty(CassandraStoreParameters.SEND_BUFFER_SIZE); String soLinger = properties.getProperty(CassandraStoreParameters.SO_LINGER); String tcpNoDelay = properties.getProperty(CassandraStoreParameters.TCP_NODELAY); SocketOptions options = new SocketOptions(); if (connectionTimeoutMillisProp != null) { options.setConnectTimeoutMillis(Integer.parseInt(connectionTimeoutMillisProp)); } if (keepAliveProp != null) { options.setKeepAlive(Boolean.parseBoolean(keepAliveProp)); } if (readTimeoutMillisProp != null) { options.setReadTimeoutMillis(Integer.parseInt(readTimeoutMillisProp)); } if (receiveBufferSizeProp != null) { options.setReceiveBufferSize(Integer.parseInt(receiveBufferSizeProp)); } if (reuseAddress != null) { options.setReuseAddress(Boolean.parseBoolean(reuseAddress)); } if (sendBufferSize != null) { options.setSendBufferSize(Integer.parseInt(sendBufferSize)); } if (soLinger != null) { options.setSoLinger(Integer.parseInt(soLinger)); } if (tcpNoDelay != null) { options.setTcpNoDelay(Boolean.parseBoolean(tcpNoDelay)); } return builder.withSocketOptions(options); }
private Builder populateSocketOptions(Map<String, String> properties, Builder builder) throws DataServiceFault { String connectionTimeoutMillisProp = properties.get(DBConstants.Cassandra.CONNECTION_TIMEOUT_MILLIS); String keepAliveProp = properties.get(DBConstants.Cassandra.KEEP_ALIVE); String readTimeoutMillisProp = properties.get(DBConstants.Cassandra.READ_TIMEOUT_MILLIS); String receiveBufferSizeProp = properties.get(DBConstants.Cassandra.RECEIVER_BUFFER_SIZE); String reuseAddress = properties.get(DBConstants.Cassandra.REUSE_ADDRESS); String sendBufferSize = properties.get(DBConstants.Cassandra.SEND_BUFFER_SIZE); String soLinger = properties.get(DBConstants.Cassandra.SO_LINGER); String tcpNoDelay = properties.get(DBConstants.Cassandra.TCP_NODELAY); SocketOptions options = new SocketOptions(); if (connectionTimeoutMillisProp != null) { options.setConnectTimeoutMillis(Integer.parseInt(connectionTimeoutMillisProp)); } if (keepAliveProp != null) { options.setKeepAlive(Boolean.parseBoolean(keepAliveProp)); } if (readTimeoutMillisProp != null) { options.setReadTimeoutMillis(Integer.parseInt(readTimeoutMillisProp)); } if (receiveBufferSizeProp != null) { options.setReceiveBufferSize(Integer.parseInt(receiveBufferSizeProp)); } if (reuseAddress != null) { options.setReuseAddress(Boolean.parseBoolean(reuseAddress)); } if (sendBufferSize != null) { options.setSendBufferSize(Integer.parseInt(sendBufferSize)); } if (soLinger != null) { options.setSoLinger(Integer.parseInt(soLinger)); } if (tcpNoDelay != null) { options.setTcpNoDelay(Boolean.parseBoolean(tcpNoDelay)); } return builder.withSocketOptions(options); }
private Cluster buildClusterSpecs() { Cluster.Builder builder = Cluster.builder(); // dbhost String dbhost = getParamString("dbhost"); String[] nodeAddresses = dbhost.split(","); for (String address : nodeAddresses) { builder.addContactPoint(address); } // dbport builder.withPort(getParamInt("dbport", 9042)); // db_timeout_millis and db_connect_retry_wait_millis SocketOptions socketOpts = new SocketOptions(); socketOpts.setReadTimeoutMillis(getParamInt("db_timeout_millis", 10000)); socketOpts.setConnectTimeoutMillis(getParamInt("db_connect_retry_wait_millis", 5000)); builder.withSocketOptions(socketOpts); // dbuser/dbpassword String dbuser = getParamString("dbuser"); if (!Utils.isEmpty(dbuser)) { builder.withCredentials(dbuser, getParamString("dbpassword")); } // compression builder.withCompression(Compression.SNAPPY); // TLS/SSL if (getParamBoolean("dbtls")) { builder.withSSL(getSSLOptions()); } return builder.build(); }
@Test public void shouldCreateClusterWithConfig() throws Exception { CassandraServiceInfo info = new CassandraServiceInfo("local", Collections.singletonList("127.0.0.1"), 9142); CassandraClusterConfig config = new CassandraClusterConfig(); config.setCompression(ProtocolOptions.Compression.NONE); config.setPoolingOptions(new PoolingOptions().setPoolTimeoutMillis(1234)); config.setQueryOptions(new QueryOptions()); config.setProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED); config.setLoadBalancingPolicy(new RoundRobinPolicy()); config.setReconnectionPolicy(new ConstantReconnectionPolicy(1)); config.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE); config.setSocketOptions(new SocketOptions()); Cluster cluster = creator.create(info, config); Configuration configuration = cluster.getConfiguration(); assertThat(configuration.getProtocolOptions().getCompression(), is(config.getCompression())); assertThat(configuration.getQueryOptions(), is(config.getQueryOptions())); assertThat(configuration.getSocketOptions(), is(config.getSocketOptions())); Policies policies = configuration.getPolicies(); assertThat(policies.getLoadBalancingPolicy(), is(config.getLoadBalancingPolicy())); assertThat(policies.getReconnectionPolicy(), is(config.getReconnectionPolicy())); assertThat(policies.getRetryPolicy(), is(config.getRetryPolicy())); }
@Bean(name = "my-service") public Cluster testClusterFactoryWithConfig() { CassandraClusterConfig config = new CassandraClusterConfig(); SocketOptions socketOptions = new SocketOptions(); socketOptions.setSendBufferSize(12345); config.setSocketOptions(socketOptions); return connectionFactory().cluster("my-service", config); }
private SocketOptions getSocketOptions(CassandraProperties properties) { SocketOptions options = new SocketOptions(); if (nonNull(properties.getConnectTimeout())) { options.setConnectTimeoutMillis((int) properties.getConnectTimeout().toMillis()); } if (nonNull(properties.getConnectTimeout())) { options.setReadTimeoutMillis((int) properties.getReadTimeout().toMillis()); } return options; }
private SocketOptions getSocketOptions(CassandraProperties properties) { SocketOptions options = new SocketOptions(); options.setConnectTimeoutMillis(properties.getConnectTimeoutMillis()); options.setReadTimeoutMillis(properties.getReadTimeoutMillis()); return options; }
private static SocketOptions getSocketOptions(CassandraProperties properties) { SocketOptions options = new SocketOptions(); options.setConnectTimeoutMillis(properties.getConnectTimeoutMillis()); options.setReadTimeoutMillis(properties.getReadTimeoutMillis()); return options; }
private SocketOptions getSocketOptions() { SocketOptions options = new SocketOptions(); options.setConnectTimeoutMillis(this.eventStoreConfig.getConnectTimeoutMillis()); options.setReadTimeoutMillis(this.eventStoreConfig.getReadTimeoutMillis()); return options; }
/** * 描述: 初始化配置 * 时间: 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); } }
/** * Sets socket options to use. * * @param options Socket options. */ @SuppressWarnings("UnusedDeclaration") public void setSocketOptions(SocketOptions options) { this.sockOptions = options; invalidate(); }
private SocketOptions getSocketOptions() { SocketOptions options = new SocketOptions(); options.setConnectTimeoutMillis(this.properties.getConnectTimeoutMillis()); options.setReadTimeoutMillis(this.properties.getReadTimeoutMillis()); return options; }