Java Code Examples for io.vertx.core.impl.ContextInternal#promise()

The following examples show how to use io.vertx.core.impl.ContextInternal#promise() . 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: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<List<ConsumerGroupListing>> listConsumerGroups() {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<List<ConsumerGroupListing>> promise = ctx.promise();

  ListConsumerGroupsResult listConsumerGroupsResult = this.adminClient.listConsumerGroups();
  listConsumerGroupsResult.all().whenComplete((groupIds, ex) -> {

    if (ex == null) {
      promise.complete(Helper.fromConsumerGroupListings(groupIds));
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 2
Source File: AsyncMapImpl.java    From vertx-ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param ttl Time to live in ms.
 */
private <T> Future<T> executeWithTtl(Function<IgniteCache<K, V>, IgniteFuture<T>> cacheOp, long ttl) {
  ContextInternal ctx = vertx.getOrCreateContext();
  Promise<T> promise = ctx.promise();
  IgniteCache<K, V> cache0 = ttl > 0 ?
    cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl))) : cache;

  IgniteFuture<T> future = cacheOp.apply(cache0);
  future.listen(fut -> {
    try {
      promise.complete(unmarshal(future.get()));
    } catch (IgniteException e) {
      promise.fail(new VertxException(e));
    }
  });
  return promise.future();
}
 
Example 3
Source File: KafkaWriteStreamImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<List<PartitionInfo>> partitionsFor(String topic) {
  ContextInternal ctx = (ContextInternal) context.owner().getOrCreateContext();
  Promise<List<PartitionInfo>> trampolineProm = ctx.promise();

  // TODO: should be this timeout related to the Kafka producer property "metadata.fetch.timeout.ms" ?
  this.context.owner().setTimer(2000, id -> {
    trampolineProm.tryFail("Kafka connect timeout");
  });

  this.context.<List<PartitionInfo>>executeBlocking(prom -> {
    prom.complete(
      this.producer.partitionsFor(topic)
    );
  }).onComplete(trampolineProm);

  return trampolineProm.future(); // Trampoline on caller context
}
 
Example 4
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
public Future<Map<TopicPartition, OffsetAndMetadata>> listConsumerGroupOffsets(String groupId, ListConsumerGroupOffsetsOptions options) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Map<TopicPartition, OffsetAndMetadata>> promise = ctx.promise();

  ListConsumerGroupOffsetsResult listConsumerGroupOffsetsResult = this.adminClient.listConsumerGroupOffsets(groupId, Helper.to(options));
  listConsumerGroupOffsetsResult.partitionsToOffsetAndMetadata().whenComplete((cgo, ex) -> {

    if (ex == null) {
      Map<TopicPartition, OffsetAndMetadata> consumerGroupOffsets = new HashMap<>();

      for (Map.Entry<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata> cgoOffset : cgo.entrySet()) {
        consumerGroupOffsets.put(Helper.from(cgoOffset.getKey()), Helper.from(cgoOffset.getValue()));
      }
      promise.complete(consumerGroupOffsets);
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 5
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> alterConfigs(Map<ConfigResource, Config> configs) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  AlterConfigsResult alterConfigsResult = this.adminClient.alterConfigs(Helper.toConfigMaps(configs));
  alterConfigsResult.all().whenComplete((v, ex) -> {

    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 6
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> deleteTopics(List<String> topicNames) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  DeleteTopicsResult deleteTopicsResult = this.adminClient.deleteTopics(topicNames);
  deleteTopicsResult.all().whenComplete((v, ex) -> {

    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 7
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> createTopics(List<NewTopic> topics) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  CreateTopicsResult createTopicsResult = this.adminClient.createTopics(Helper.toNewTopicList(topics));
  createTopicsResult.all().whenComplete((v, ex) -> {

    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 8
Source File: PoolBase.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<SqlConnection> getConnection() {
  ContextInternal current = vertx.getOrCreateContext();
  Object metric;
  if (metrics != null) {
    metric = metrics.enqueueRequest();
  } else {
    metric = null;
  }
  Promise<Connection> promise = current.promise();
  acquire(promise);
  if (metrics != null) {
    promise.future().onComplete(ar -> {
      metrics.dequeueRequest(metric);
    });
  }
  return promise.future().map(conn -> {
    SqlConnectionImpl wrapper = wrap(current, conn);
    conn.init(wrapper);
    return wrapper;
  });
}
 
Example 9
Source File: FutureMappedBatchLoaderImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<Map<K, V>> load(Set<K> keys, BatchLoaderEnvironment env) {
  ContextInternal context = (ContextInternal) contextProvider.apply(env);
  Promise<Map<K, V>> promise;
  if (context == null) {
    promise = Promise.promise();
    invokeBatchLoader(keys, env, promise);
  } else {
    promise = context.promise();
    context.runOnContext(v -> invokeBatchLoader(keys, env, promise));
  }
  return promise.future().toCompletionStage();
}
 
Example 10
Source File: FutureBatchLoaderImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment env) {
  ContextInternal context = (ContextInternal) contextProvider.apply(env);
  Promise<List<V>> promise;
  if (context == null) {
    promise = Promise.promise();
    invokeBatchLoader(keys, env, promise);
  } else {
    promise = context.promise();
    context.runOnContext(v -> invokeBatchLoader(keys, env, promise));
  }
  return promise.future().toCompletionStage();
}
 
Example 11
Source File: CallbackDataFetcherImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<T> get(DataFetchingEnvironment env) {
  ContextInternal context = (ContextInternal) contextProvider.apply(env);
  Promise<T> promise;
  if (context == null) {
    promise = Promise.promise();
    invokeDataFetcher(env, promise);
  } else {
    promise = context.promise();
    context.runOnContext(v -> invokeDataFetcher(env, promise));
  }
  return promise.future().toCompletionStage();
}
 
Example 12
Source File: DB2ConnectionImpl.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static Future<DB2Connection> connect(Vertx vertx, DB2ConnectOptions options) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  DB2ConnectionFactory client;
  try {
    client = new DB2ConnectionFactory(ctx, options);
  } catch (Exception e) {
    return ctx.failedFuture(e);
  }
  QueryTracer tracer = ctx.tracer() == null ? null : new QueryTracer(ctx.tracer(), options);
  Promise<DB2Connection> promise = ctx.promise();
  ctx.dispatch(null, v -> connect(client, ctx, tracer, promise));
  return promise.future();
}
 
Example 13
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 5 votes vote down vote up
@Override
public Future<Void> deleteConsumerGroups(List<String> groupIds) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  DeleteConsumerGroupsResult deleteConsumerGroupsResult = this.adminClient.deleteConsumerGroups(groupIds);
  deleteConsumerGroupsResult.all().whenComplete((v, ex) -> {
    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 14
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 5 votes vote down vote up
@Override
public Future<Void> deleteConsumerGroupOffsets(String groupId, Set<TopicPartition> partitions) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  DeleteConsumerGroupOffsetsResult deleteConsumerGroupOffsetsResult = this.adminClient.deleteConsumerGroupOffsets(groupId, Helper.toTopicPartitionSet(partitions));
  deleteConsumerGroupOffsetsResult.all().whenComplete((v, ex) -> {
    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 15
Source File: CallbackBatchLoaderImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<List<V>> load(List<K> keys, BatchLoaderEnvironment env) {
  ContextInternal context = (ContextInternal) contextProvider.apply(env);
  Promise<List<V>> promise;
  if (context == null) {
    promise = Promise.promise();
    invokeBatchLoader(keys, env, promise);
  } else {
    promise = context.promise();
    context.runOnContext(v -> invokeBatchLoader(keys, env, promise));
  }
  return promise.future().toCompletionStage();
}
 
Example 16
Source File: KafkaWriteStreamImpl.java    From vertx-kafka-client with Apache License 2.0 5 votes vote down vote up
@Override
public Future<Void> flush() {
  ContextInternal ctx = (ContextInternal) context.owner().getOrCreateContext();
  Promise<Void> trampolineProm = ctx.promise();
  this.context.<Void>executeBlocking(prom -> {
    this.producer.flush();
    prom.complete();
  }).onComplete(trampolineProm);
  return trampolineProm.future(); // Trampoline on caller context
}
 
Example 17
Source File: CallbackMappedBatchLoaderImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<Map<K, V>> load(Set<K> keys, BatchLoaderEnvironment env) {
  ContextInternal context = (ContextInternal) contextProvider.apply(env);
  Promise<Map<K, V>> promise;
  if (context == null) {
    promise = Promise.promise();
    invokeBatchLoader(keys, env, promise);
  } else {
    promise = context.promise();
    context.runOnContext(v -> invokeBatchLoader(keys, env, promise));
  }
  return promise.future().toCompletionStage();
}
 
Example 18
Source File: MSSQLConnectionImpl.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static Future<MSSQLConnection> connect(Vertx vertx, MSSQLConnectOptions options) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  QueryTracer tracer = ctx.tracer() == null ? null : new QueryTracer(ctx.tracer(), options);
  PromiseInternal<MSSQLConnection> promise = ctx.promise();
  MSSQLConnectionFactory client = new MSSQLConnectionFactory(ctx, options);
  ctx.dispatch(null, v -> {
    client.connect()
      .<MSSQLConnection>map(conn -> {
        MSSQLConnectionImpl msConn = new MSSQLConnectionImpl(client, ctx, conn, tracer, null);
        conn.init(msConn);
        return msConn;
      }).onComplete(promise);
  });
  return promise.future();
}
 
Example 19
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Map<String, ConsumerGroupDescription>> describeConsumerGroups(List<String> groupIds) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Map<String, ConsumerGroupDescription>> promise = ctx.promise();

  DescribeConsumerGroupsResult describeConsumerGroupsResult = this.adminClient.describeConsumerGroups(groupIds);
  describeConsumerGroupsResult.all().whenComplete((cg, ex) -> {
    if (ex == null) {
      Map<String, ConsumerGroupDescription> consumerGroups = new HashMap<>();

      for (Map.Entry<String, org.apache.kafka.clients.admin.ConsumerGroupDescription> cgDescriptionEntry: cg.entrySet()) {
        List<MemberDescription> members = new ArrayList<>();

        for (org.apache.kafka.clients.admin.MemberDescription memberDescription : cgDescriptionEntry.getValue().members()) {
          MemberDescription m = new MemberDescription();
          m.setConsumerId(memberDescription.consumerId())
            .setClientId(memberDescription.clientId())
            .setAssignment(Helper.from(memberDescription.assignment()))
            .setHost(memberDescription.host());

          members.add(m);
        }

        ConsumerGroupDescription consumerGroupDescription = new ConsumerGroupDescription();

        consumerGroupDescription.setGroupId(cgDescriptionEntry.getValue().groupId())
          .setCoordinator(Helper.from(cgDescriptionEntry.getValue().coordinator()))
          .setMembers(members)
          .setPartitionAssignor(cgDescriptionEntry.getValue().partitionAssignor())
          .setSimpleConsumerGroup(cgDescriptionEntry.getValue().isSimpleConsumerGroup())
          .setState(cgDescriptionEntry.getValue().state());

        consumerGroups.put(cgDescriptionEntry.getKey(), consumerGroupDescription);
      }
      promise.complete(consumerGroups);
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example 20
Source File: TransactionImpl.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
TransactionImpl(ContextInternal context, Connection connection) {
  this.context = context;
  this.connection = connection;
  this.completion = context.promise();
}