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