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

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

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

Cluster.connectAsync介绍

[英]Creates a new session on this cluster and initializes it asynchronously.

This will also initialize the Cluster if needed; note that cluster initialization happens synchronously on the thread that called this method. Therefore it is recommended to initialize the cluster at application startup, and not rely on this method to do it.
[中]在此群集上创建新会话并异步初始化它。
如果需要,这也将初始化集群;请注意,集群初始化在调用此方法的线程上同步进行。因此,建议在应用程序启动时初始化集群,而不要依赖此方法。

代码示例

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

/**
 * Creates a new session on this cluster and initializes it asynchronously.
 *
 * <p>This will also initialize the {@code Cluster} if needed; note that cluster initialization
 * happens synchronously on the thread that called this method. Therefore it is recommended to
 * initialize the cluster at application startup, and not rely on this method to do it.
 *
 * @return a future that will complete when the session is fully initialized.
 * @throws NoHostAvailableException if the Cluster has not been initialized yet ({@link #init} has
 *     not been called and this is the first connect call) and no host amongst the contact points
 *     can be reached.
 * @throws IllegalStateException if the Cluster was closed prior to calling this method. This can
 *     occur either directly (through {@link #close()} or {@link #closeAsync()}), or as a result
 *     of an error while initializing the Cluster.
 * @see #connect()
 */
public ListenableFuture<Session> connectAsync() {
 return connectAsync(null);
}

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

@Override
public ListenableFuture<Session> connectAsync() {
 return delegate().connectAsync();
}

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

@Override
public ListenableFuture<Session> connectAsync(String keyspace) {
 return delegate().connectAsync(keyspace);
}

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

/**
 * Creates a new session on this cluster, initialize it and sets the keyspace to the provided one.
 *
 * <p>Note that this method will initialize the newly created session, trying to connect to the
 * Cassandra nodes before returning. If you only want to create a Session object without
 * initializing it right away, see {@link #newSession}.
 *
 * @param keyspace The name of the keyspace to use for the created {@code Session}.
 * @return a new session on this cluster sets to keyspace {@code keyspaceName}.
 * @throws NoHostAvailableException if the Cluster has not been initialized yet ({@link #init} has
 *     not be called and this is the first connect call) and no host amongst the contact points
 *     can be reached, or if no host can be contacted to set the {@code keyspace}.
 * @throws AuthenticationException if an authentication error occurs while contacting the initial
 *     contact points.
 * @throws InvalidQueryException if the keyspace does not exist.
 * @throws IllegalStateException if the Cluster was closed prior to calling this method. This can
 *     occur either directly (through {@link #close()} or {@link #closeAsync()}), or as a result
 *     of an error while initializing the Cluster.
 */
public Session connect(String keyspace) {
 try {
  return Uninterruptibles.getUninterruptibly(connectAsync(keyspace));
 } catch (ExecutionException e) {
  throw DriverThrowables.propagateCause(e);
 }
}

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

/**
 * Creates a new session on this cluster and initialize it.
 *
 * <p>Note that this method will initialize the newly created session, trying to connect to the
 * Cassandra nodes before returning. If you only want to create a Session object without
 * initializing it right away, see {@link #newSession}.
 *
 * @return a new session on this cluster sets to no keyspace.
 * @throws NoHostAvailableException if the Cluster has not been initialized yet ({@link #init} has
 *     not be called and this is the first connect call) and no host amongst the contact points
 *     can be reached.
 * @throws AuthenticationException if an authentication error occurs while contacting the initial
 *     contact points.
 * @throws IllegalStateException if the Cluster was closed prior to calling this method. This can
 *     occur either directly (through {@link #close()} or {@link #closeAsync()}), or as a result
 *     of an error while initializing the Cluster.
 */
public Session connect() {
 try {
  return Uninterruptibles.getUninterruptibly(connectAsync());
 } catch (ExecutionException e) {
  throw DriverThrowables.propagateCause(e);
 }
}

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

/**
  * Ensures that if an attempt is made to create a {@link Session} via {@link Cluster#connectAsync}
  * with an invalid keyspace that the returned exception is decorated with an indication to check
  * that your keyspace name is valid and includes the original {@link SyntaxError}.
  */
 @Test(groups = "short")
 public void should_give_explicit_error_message_when_keyspace_name_invalid_async() {
  ListenableFuture<Session> sessionFuture = cluster().connectAsync("");
  try {
   sessionFuture.get();
  } catch (ExecutionException e) {
   assertThat(e.getCause()).isInstanceOf(SyntaxError.class);
   assertThat(e.getCause().getMessage())
     .contains("no viable alternative at input '<EOF>'")
     .contains("Check that your keyspace name is valid");
  } catch (Exception e) {
   fail("Did not expect Exception", e);
  }
 }
}

代码示例来源:origin: io.opentracing.contrib/opentracing-cassandra-driver

/**
 * {@inheritDoc}
 */
@Override
public ListenableFuture<Session> connectAsync() {
 // result shouldn't be wrapped into TracingSession because it internally calls connectAsync(String keyspace)
 return super.connectAsync();
}

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

private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) {
 ListenableFuture<Session> sessionFuture = cluster().connectAsync(keyspace);
 ListenableFuture<ResultSet> queryFuture =
   GuavaCompatibility.INSTANCE.transformAsync(
     sessionFuture,
     new AsyncFunction<Session, ResultSet>() {
      @Override
      public ListenableFuture<ResultSet> apply(Session session) throws Exception {
       return session.executeAsync("select v from foo where k = 1");
      }
     },
     executor);
 return GuavaCompatibility.INSTANCE.transform(
   queryFuture,
   new Function<ResultSet, Integer>() {
    @Override
    public Integer apply(ResultSet rs) {
     return rs.one().getInt(0);
    }
   },
   executor);
}

代码示例来源:origin: io.prestosql.cassandra/cassandra-driver

/**
 * Creates a new session on this cluster and initializes it asynchronously.
 * <p/>
 * This will also initialize the {@code Cluster} if needed; note that cluster
 * initialization happens synchronously on the thread that called this method.
 * Therefore it is recommended to initialize the cluster at application
 * startup, and not rely on this method to do it.
 *
 * @return a future that will complete when the session is fully initialized.
 * @throws NoHostAvailableException if the Cluster has not been initialized
 *                                  yet ({@link #init} has not been called and this is the first connect call)
 *                                  and no host amongst the contact points can be reached.
 * @throws IllegalStateException    if the Cluster was closed prior to calling
 *                                  this method. This can occur either directly (through {@link #close()} or
 *                                  {@link #closeAsync()}), or as a result of an error while initializing the
 *                                  Cluster.
 * @see #connect()
 */
public ListenableFuture<Session> connectAsync() {
  return connectAsync(null);
}

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

/**
 * Creates a new session on this cluster and initializes it asynchronously.
 * <p/>
 * This will also initialize the {@code Cluster} if needed; note that cluster
 * initialization happens synchronously on the thread that called this method.
 * Therefore it is recommended to initialize the cluster at application
 * startup, and not rely on this method to do it.
 *
 * @return a future that will complete when the session is fully initialized.
 * @throws NoHostAvailableException if the Cluster has not been initialized
 *                                  yet ({@link #init} has not been called and this is the first connect call)
 *                                  and no host amongst the contact points can be reached.
 * @throws IllegalStateException    if the Cluster was closed prior to calling
 *                                  this method. This can occur either directly (through {@link #close()} or
 *                                  {@link #closeAsync()}), or as a result of an error while initializing the
 *                                  Cluster.
 * @see #connect()
 */
public ListenableFuture<Session> connectAsync() {
  return connectAsync(null);
}

代码示例来源:origin: com.facebook.presto.cassandra/cassandra-driver

/**
 * Creates a new session on this cluster and initializes it asynchronously.
 * <p/>
 * This will also initialize the {@code Cluster} if needed; note that cluster
 * initialization happens synchronously on the thread that called this method.
 * Therefore it is recommended to initialize the cluster at application
 * startup, and not rely on this method to do it.
 *
 * @return a future that will complete when the session is fully initialized.
 * @throws NoHostAvailableException if the Cluster has not been initialized
 *                                  yet ({@link #init} has not been called and this is the first connect call)
 *                                  and no host amongst the contact points can be reached.
 * @throws IllegalStateException    if the Cluster was closed prior to calling
 *                                  this method. This can occur either directly (through {@link #close()} or
 *                                  {@link #closeAsync()}), or as a result of an error while initializing the
 *                                  Cluster.
 * @see #connect()
 */
public ListenableFuture<Session> connectAsync() {
  return connectAsync(null);
}

代码示例来源:origin: com.facebook.presto.cassandra/cassandra-driver

@Override
public ListenableFuture<Session> connectAsync() {
  return delegate().connectAsync();
}

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

@Override
public ListenableFuture<Session> connectAsync() {
  return delegate().connectAsync();
}

代码示例来源:origin: io.prestosql.cassandra/cassandra-driver

@Override
public ListenableFuture<Session> connectAsync() {
  return delegate().connectAsync();
}

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

@Override
public ListenableFuture<Session> connectAsync(String keyspace) {
  return delegate().connectAsync(keyspace);
}

代码示例来源:origin: com.facebook.presto.cassandra/cassandra-driver

@Override
public ListenableFuture<Session> connectAsync(String keyspace) {
  return delegate().connectAsync(keyspace);
}

代码示例来源:origin: io.prestosql.cassandra/cassandra-driver

@Override
public ListenableFuture<Session> connectAsync(String keyspace) {
  return delegate().connectAsync(keyspace);
}

代码示例来源:origin: io.opentracing.contrib/opentracing-cassandra-driver

/**
  * {@inheritDoc}
  */
 @Override
 public ListenableFuture<Session> connectAsync(String keyspace) {
  return GuavaCompatibility.INSTANCE
    .transform(super.connectAsync(keyspace), new Function<Session, Session>() {
     @Override
     public Session apply(Session session) {
      return new TracingSession(session, tracer, querySpanNameProvider, executorService);
     }
    });
 }
}

代码示例来源:origin: com.englishtown.vertx/vertx-cassandra

@Override
public void reconnectAsync(Handler<AsyncResult<Void>> callback) {
  logger.debug("Call to reconnect the session has been made");
  Session oldSession = session;
  FutureUtils.addCallback(cluster.connectAsync(), new FutureCallback<Session>() {
    @Override
    public void onSuccess(Session session) {
      DefaultCassandraSession.this.session = session;
      if (oldSession != null) {
        oldSession.closeAsync();
      }
      callback.handle(Future.succeededFuture());
      metrics.afterReconnect();
    }
    @Override
    public void onFailure(Throwable throwable) {
      callback.handle(Future.failedFuture(throwable));
    }
  }, vertx);
}

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

/**
  * Ensures that if an attempt is made to create a {@link Session} via {@link Cluster#connectAsync}
  * with an invalid keyspace that the returned exception is decorated with an indication to check
  * that your keyspace name is valid and includes the original {@link SyntaxError}.
  */
 @Test(groups = "short")
 public void should_give_explicit_error_message_when_keyspace_name_invalid_async() {
  ListenableFuture<Session> sessionFuture = cluster().connectAsync("");
  try {
   sessionFuture.get();
  } catch (ExecutionException e) {
   assertThat(e.getCause()).isInstanceOf(SyntaxError.class);
   assertThat(e.getCause().getMessage())
     .contains("no viable alternative at input '<EOF>'")
     .contains("Check that your keyspace name is valid");
  } catch (Exception e) {
   fail("Did not expect Exception", e);
  }
 }
}

相关文章