com.datastax.driver.core.Cluster.init()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(142)

本文整理了Java中com.datastax.driver.core.Cluster.init()方法的一些代码示例,展示了Cluster.init()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cluster.init()方法的具体详情如下:
包路径:com.datastax.driver.core.Cluster
类名称:Cluster
方法名:init

Cluster.init介绍

[英]Initialize this Cluster instance.

This method creates an initial connection to one of the contact points used to construct the Cluster instance. That connection is then used to populate the cluster Metadata.

Calling this method is optional in the sense that any call to one of the connect methods of this object will automatically trigger a call to this method beforehand. It is thus only useful to call this method if for some reason you want to populate the metadata (or test that at least one contact point can be reached) without creating a first Session.

Please note that this method only creates one control connection for gathering cluster metadata. In particular, it doesn't create any connection pools. Those are created when a new Session is created through connect.

This method has no effect if the cluster is already initialized.
[中]初始化此群集实例。
此方法创建到用于构造集群实例的一个接触点的初始连接。然后使用该连接填充集群元数据。
调用此方法是可选的,因为对该对象的某个connect方法的任何调用都将自动触发对该方法的调用。因此,只有在出于某种原因希望在不创建第一个会话的情况下填充元数据(或测试至少可以到达一个接触点)时,调用此方法才有用。
请注意,此方法仅创建一个用于收集群集元数据的控件连接。特别是,它不会创建任何连接池。通过connect创建新会话时会创建这些会话。
如果集群已初始化,则此方法无效。

代码示例

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Override
 public void run() {
  cluster.init();
 }
}.start();

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Override
public Cluster init() {
 return delegate().init();
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

private UnsupportedProtocolVersionException initWithUnsupportedVersion(Cluster cluster) {
 Throwable t = null;
 try {
  cluster.init();
 } catch (Throwable t2) {
  t = t2;
 }
 if (t instanceof UnsupportedProtocolVersionException) {
  return (UnsupportedProtocolVersionException) t;
 } else {
  throw new AssertionError("Expected UnsupportedProtocolVersionException, got " + t);
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Override
 public void run() {
  try {
   startLatch.await();
   cluster.init();
  } catch (InterruptedException e) {
   fail("unexpected interruption", e);
  }
 }
});

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

cluster().init();

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Validates that when a Cluster is initialized that {@link
 * SpeculativeExecutionPolicy#init(Cluster)} is called and that when a Cluster is closed {@link
 * SpeculativeExecutionPolicy#close()} is called.
 *
 * @test_category queries:speculative_execution
 * @expected_result init and close are called on cluster init and close.
 * @jira_ticket JAVA-796
 * @since 2.0.11, 2.1.7, 2.2.1
 */
@Test(groups = "short")
public void should_init_and_close_policy_on_cluster() {
 SpeculativeExecutionPolicy mockPolicy = mock(SpeculativeExecutionPolicy.class);
 Cluster cluster =
   Cluster.builder()
     .addContactPoints(scassandras.address(2).getAddress())
     .withPort(scassandras.getBinaryPort())
     .withSpeculativeExecutionPolicy(mockPolicy)
     .build();
 verify(mockPolicy, times(0)).init(cluster);
 verify(mockPolicy, times(0)).close();
 try {
  cluster.init();
  verify(mockPolicy, times(1)).init(cluster);
 } finally {
  cluster.close();
  verify(mockPolicy, times(1)).close();
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

protected void initTestCluster(Object testInstance) throws Exception {
 if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster()) {
  Cluster.Builder builder = ccmTestConfig.clusterProvider(testInstance);
  // add contact points only if the provided builder didn't do so
  if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints());
  builder.withPort(ccm.getBinaryPort());
  cluster = register(builder.build());
  cluster.init();
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@BeforeClass(groups = "short")
public void setUp() throws Exception {
 scassandra = ScassandraCluster.builder().withNodes(2).build();
 scassandra.init();
 cluster =
   builder()
     .addContactPoints(scassandra.address(1).getAddress())
     .withPort(scassandra.getBinaryPort())
     .build();
 cluster.init();
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void should_throw_ufe_when_protocol_version_lesser_than_4() throws Exception {
 try {
  Cluster v3cluster =
    register(
        Cluster.builder()
          .addContactPoints(getContactPoints())
          .withPort(ccm().getBinaryPort())
          .withProtocolVersion(V3)
          .build())
      .init();
  Session v3session = v3cluster.connect();
  Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1);
  statement.setOutgoingPayload(payload1);
  v3session.execute(statement);
  fail("Should not send custom payloads with protocol V3");
 } catch (UnsupportedFeatureException e) {
  assertThat(e.getMessage())
    .isEqualTo(
      "Unsupported feature with the native protocol V3 (which is currently in use): Custom payloads are only supported since native protocol V4");
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@BeforeMethod(groups = "short")
public void setup() {
 QueryOptions queryOptions = new QueryOptions();
 queryOptions.setRefreshNodeListIntervalMillis(DEBOUNCE_TIME);
 queryOptions.setMaxPendingRefreshNodeListRequests(5);
 queryOptions.setRefreshSchemaIntervalMillis(0);
 // Create a separate cluster that will receive the schema events on its control connection.
 cluster2 =
   register(
     Cluster.builder()
       .addContactPoints(getContactPoints())
       .withPort(ccm().getBinaryPort())
       .withQueryOptions(queryOptions)
       .build());
 cluster2.init();
 // Create a spy of the Cluster's control connection and replace it with the spy.
 controlConnection = spy(cluster2.manager.controlConnection);
 cluster2.manager.controlConnection = controlConnection;
 reset(controlConnection);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

private Cluster connectWithoutVersion() {
 Cluster cluster =
   register(
     Cluster.builder()
       .addContactPoints(getContactPoints())
       .withPort(ccm().getBinaryPort())
       .build());
 cluster.init();
 return cluster;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

protected void resetTestSession() throws Exception {
 session.close();
 Cluster.Builder builder = ccmTestConfig.clusterProvider(this);
 // add contact points only if the provided builder didn't do so
 if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints());
 builder.withPort(ccm.getBinaryPort());
 cluster = register(builder.build());
 cluster.init();
 session.close();
 session = register(cluster.connect());
 useKeyspace(session, keyspace);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
  * Validates that when metrics are enabled but JMX reporting is disabled via {@link
  * Cluster.Builder#withoutJMXReporting()} that {@link Cluster#getMetrics()} is not null and
  * 'clusterName-metrics:name=connected-to' MBean is present.
  *
  * @test_category metrics
  */
 @Test(groups = "short", expectedExceptions = InstanceNotFoundException.class)
 public void should_be_no_jmx_mbean_when_jmx_is_disabled() throws Exception {
  Cluster cluster =
    register(
      Cluster.builder()
        .addContactPoints(getContactPoints())
        .withPort(ccm().getBinaryPort())
        .withoutJMXReporting()
        .build());
  try {
   cluster.init();
   assertThat(cluster.getMetrics()).isNotNull();
   assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue();
   assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isFalse();
   ObjectName clusterMetricsON =
     ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to");
   server.getMBeanInfo(clusterMetricsON);
  } finally {
   cluster.close();
  }
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

private Cluster connectWithVersion(ProtocolVersion version) {
 Cluster cluster =
   register(
     Cluster.builder()
       .addContactPoints(getContactPoints())
       .withPort(ccm().getBinaryPort())
       .withProtocolVersion(version)
       .build());
 cluster.init();
 return cluster;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@Test(groups = "short")
public void should_not_leak_session_when_wrong_keyspace() throws Exception {
 // Checking for JAVA-806
 channelMonitor = new SocketChannelMonitor();
 channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS);
 Cluster cluster =
   register(
     Cluster.builder()
       .addContactPoints(getContactPoints().get(0))
       .withPort(ccm().getBinaryPort())
       .withNettyOptions(channelMonitor.nettyOptions())
       .build());
 cluster.init();
 assertThat(cluster.manager.sessions.size()).isEqualTo(0);
 try {
  // Should be 1 control connection after initialization.
  assertOpenConnections(1, cluster);
  cluster.connect("wrong_keyspace");
  fail("Should not have connected to a wrong keyspace");
 } catch (InvalidQueryException e) {
  // ok
 }
 assertThat(cluster.manager.sessions.size()).isEqualTo(0);
 cluster.close();
 // Ensure no channels remain open.
 channelMonitor.stop();
 channelMonitor.report();
 assertThat(channelMonitor.openChannels(ccm().addressOfNode(1), ccm().addressOfNode(2)).size())
   .isEqualTo(0);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@BeforeClass(groups = "short")
public void setup() throws InterruptedException {
 Cluster.Builder builder =
   Cluster.builder()
     .addContactPoints(getContactPoints())
     .withPort(ccm().getBinaryPort())
     .withQueryOptions(nonDebouncingQueryOptions());
 cluster1 = builder.build();
 cluster2 = builder.build();
 schemaDisabledCluster =
   spy(
     Cluster.builder()
       .addContactPoints(getContactPoints())
       .withPort(ccm().getBinaryPort())
       .withClusterName("schema-disabled")
       .withQueryOptions(nonDebouncingQueryOptions().setMetadataEnabled(false))
       .build());
 schemaDisabledSession = schemaDisabledCluster.connect();
 schemaDisabledControlConnection = spy(schemaDisabledCluster.manager.controlConnection);
 schemaDisabledCluster.manager.controlConnection = schemaDisabledControlConnection;
 session1 = cluster1.connect();
 cluster2.init();
 cluster1.register(listener1 = mock(SchemaChangeListener.class));
 cluster2.register(listener2 = mock(SchemaChangeListener.class));
 listeners = Lists.newArrayList(listener1, listener2);
 schemaDisabledCluster.register(schemaDisabledListener = mock(SchemaChangeListener.class));
 verify(schemaDisabledListener, times(1)).onRegister(schemaDisabledCluster);
 execute(CREATE_KEYSPACE, "lowercase");
 execute(CREATE_KEYSPACE, "\"CaseSensitive\"");
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Validates that when metrics are disabled using {@link Cluster.Builder#withoutMetrics()} that
 * {@link Cluster#getMetrics()} returns null and 'clusterName-metrics:name=connected-to' MBean is
 * not present.
 *
 * @test_category metrics
 */
@Test(groups = "short", expectedExceptions = InstanceNotFoundException.class)
public void metrics_should_be_null_when_metrics_disabled() throws Exception {
 Cluster cluster =
   register(
     Cluster.builder()
       .addContactPoints(getContactPoints())
       .withPort(ccm().getBinaryPort())
       .withoutMetrics()
       .build());
 try {
  cluster.init();
  assertThat(cluster.getMetrics()).isNull();
  assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isFalse();
  ObjectName clusterMetricsON =
    ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to");
  server.getMBeanInfo(clusterMetricsON);
 } finally {
  cluster.close();
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Ensures that calling {@link Metadata#newTokenRange(Token, Token)} on a Cluster that has schema
 * metadata disabled will throw a {@link IllegalStateException}.
 *
 * @jira_ticket JAVA-858
 * @since 2.0.11
 */
@Test(groups = "short", expectedExceptions = IllegalStateException.class)
public void should_throw_illegal_state_exception_on_newTokenRange_with_metadata_disabled() {
 Cluster cluster =
   Cluster.builder()
     .addContactPoints(getContactPoints())
     .withPort(ccm().getBinaryPort())
     .withQueryOptions(nonDebouncingQueryOptions().setMetadataEnabled(false))
     .build();
 try {
  cluster.init();
  Token.Factory factory = Token.getFactory("Murmur3Partitioner");
  Token token = factory.fromString(Long.toString(1));
  cluster.getMetadata().newTokenRange(token, token);
 } finally {
  cluster.close();
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Ensures that calling {@link Metadata#newToken(String)} on a Cluster that has schema metadata
 * disabled will throw a {@link IllegalStateException}.
 *
 * @jira_ticket JAVA-858
 * @since 2.0.11
 */
@Test(groups = "short", expectedExceptions = IllegalStateException.class)
public void should_throw_illegal_state_exception_on_newToken_with_metadata_disabled() {
 Cluster cluster =
   Cluster.builder()
     .addContactPoints(getContactPoints())
     .withPort(ccm().getBinaryPort())
     .withQueryOptions(nonDebouncingQueryOptions().setMetadataEnabled(false))
     .build();
 try {
  cluster.init();
  cluster.getMetadata().newToken("0x00");
 } finally {
  cluster.close();
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Ensures that when a cluster is initialized that {@link LoadBalancingPolicy#init(Cluster,
 * Collection)} is called with each reachable contact point.
 *
 * @test_category load_balancing:notification
 * @expected_result init() is called for each of two contact points.
 */
@Test(groups = "short")
public void should_init_policy_with_up_contact_points() throws Exception {
 HistoryPolicy policy = new HistoryPolicy(new RoundRobinPolicy());
 Cluster cluster =
   register(
     Cluster.builder()
       .addContactPoints(getContactPoints())
       .withPort(ccm().getBinaryPort())
       .withLoadBalancingPolicy(policy)
       .build());
 try {
  cluster.init();
  assertThat(policy.history)
    .containsOnly(
      entry(INIT, TestUtils.findHost(cluster, 1)),
      entry(INIT, TestUtils.findHost(cluster, 2)));
 } finally {
  cluster.close();
 }
}

相关文章