List of usage examples for java.util.concurrent CompletableFuture thenRun
public CompletableFuture<Void> thenRun(Runnable action)
From source file:com.yahoo.pulsar.broker.service.BrokerService.java
private CompletableFuture<Topic> createPersistentTopic(final String topic) throws RuntimeException { checkTopicNsOwnership(topic);/*from w w w.j av a 2s . c o m*/ final long topicCreateTimeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); DestinationName destinationName = DestinationName.get(topic); if (!pulsar.getNamespaceService().isServiceUnitActive(destinationName)) { // namespace is being unloaded String msg = String.format("Namespace is being unloaded, cannot add topic %s", topic); log.warn(msg); throw new RuntimeException(new ServiceUnitNotReadyException(msg)); } final CompletableFuture<Topic> topicFuture = new CompletableFuture<>(); getManagedLedgerConfig(destinationName).thenAccept(config -> { // Once we have the configuration, we can proceed with the async open operation managedLedgerFactory.asyncOpen(destinationName.getPersistenceNamingEncoding(), config, new OpenLedgerCallback() { @Override public void openLedgerComplete(ManagedLedger ledger, Object ctx) { PersistentTopic persistentTopic = new PersistentTopic(topic, ledger, BrokerService.this); CompletableFuture<Void> replicationFuture = persistentTopic.checkReplication(); replicationFuture.thenRun(() -> { log.info("Created topic {}", topic); long topicLoadLatencyMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - topicCreateTimeMs; pulsarStats.recordTopicLoadTimeValue(topic, topicLoadLatencyMs); addTopicToStatsMaps(destinationName, persistentTopic); topicFuture.complete(persistentTopic); }); replicationFuture.exceptionally((ex) -> { log.warn("Replication check failed. Removing topic from topics list {}, {}", topic, ex); persistentTopic.stopReplProducers().whenComplete((v, exception) -> { topics.remove(topic, topicFuture); topicFuture.completeExceptionally(ex); }); return null; }); } @Override public void openLedgerFailed(ManagedLedgerException exception, Object ctx) { log.warn("Failed to create topic {}", topic, exception); topics.remove(topic, topicFuture); topicFuture.completeExceptionally(new PersistenceException(exception)); } }, null); }).exceptionally((exception) -> { log.warn("[{}] Failed to get topic configuration: {}", topic, exception.getMessage(), exception); topics.remove(topic, topicFuture); topicFuture.completeExceptionally(exception); return null; }); return topicFuture; }
From source file:org.apache.pulsar.broker.service.BrokerService.java
private void createPersistentTopic(final String topic, CompletableFuture<Topic> topicFuture) { final long topicCreateTimeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); DestinationName destinationName = DestinationName.get(topic); if (!pulsar.getNamespaceService().isServiceUnitActive(destinationName)) { // namespace is being unloaded String msg = String.format("Namespace is being unloaded, cannot add topic %s", topic); log.warn(msg);/*from w w w. j a v a 2 s .c o m*/ pulsar.getExecutor().submit(() -> topics.remove(topic, topicFuture)); topicFuture.completeExceptionally(new ServiceUnitNotReadyException(msg)); return; } getManagedLedgerConfig(destinationName).thenAccept(config -> { // Once we have the configuration, we can proceed with the async open operation managedLedgerFactory.asyncOpen(destinationName.getPersistenceNamingEncoding(), config, new OpenLedgerCallback() { @Override public void openLedgerComplete(ManagedLedger ledger, Object ctx) { PersistentTopic persistentTopic = new PersistentTopic(topic, ledger, BrokerService.this); CompletableFuture<Void> replicationFuture = persistentTopic.checkReplication(); replicationFuture.thenRun(() -> { log.info("Created topic {}", topic); long topicLoadLatencyMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - topicCreateTimeMs; pulsarStats.recordTopicLoadTimeValue(topic, topicLoadLatencyMs); addTopicToStatsMaps(destinationName, persistentTopic); topicFuture.complete(persistentTopic); }); replicationFuture.exceptionally((ex) -> { log.warn("Replication check failed. Removing topic from topics list {}, {}", topic, ex); persistentTopic.stopReplProducers().whenComplete((v, exception) -> { topics.remove(topic, topicFuture); topicFuture.completeExceptionally(ex); }); return null; }); } @Override public void openLedgerFailed(ManagedLedgerException exception, Object ctx) { log.warn("Failed to create topic {}", topic, exception); topics.remove(topic, topicFuture); topicFuture.completeExceptionally(new PersistenceException(exception)); } }, null); }).exceptionally((exception) -> { log.warn("[{}] Failed to get topic configuration: {}", topic, exception.getMessage(), exception); // remove topic from topics-map in different thread to avoid possible deadlock if // createPersistentTopic-thread only tries to handle this future-result pulsar.getExecutor().submit(() -> topics.remove(topic, topicFuture)); topicFuture.completeExceptionally(exception); return null; }); }
From source file:org.apache.pulsar.client.impl.PulsarClientImpl.java
@Override public CompletableFuture<Reader> createReaderAsync(String topic, MessageId startMessageId, ReaderConfiguration conf) {// w w w .j a va 2s. com if (state.get() != State.Open) { return FutureUtil .failedFuture(new PulsarClientException.AlreadyClosedException("Client already closed")); } if (!DestinationName.isValid(topic)) { return FutureUtil .failedFuture(new PulsarClientException.InvalidTopicNameException("Invalid topic name")); } if (startMessageId == null) { return FutureUtil.failedFuture( new PulsarClientException.InvalidConfigurationException("Invalid startMessageId")); } if (conf == null) { return FutureUtil.failedFuture( new PulsarClientException.InvalidConfigurationException("Consumer configuration undefined")); } CompletableFuture<Reader> readerFuture = new CompletableFuture<>(); getPartitionedTopicMetadata(topic).thenAccept(metadata -> { if (log.isDebugEnabled()) { log.debug("[{}] Received topic metadata. partitions: {}", topic, metadata.partitions); } if (metadata.partitions > 1) { readerFuture.completeExceptionally( new PulsarClientException("Topic reader cannot be created on a partitioned topic")); return; } CompletableFuture<Consumer> consumerSubscribedFuture = new CompletableFuture<>(); // gets the next single threaded executor from the list of executors ExecutorService listenerThread = externalExecutorProvider.getExecutor(); ReaderImpl reader = new ReaderImpl(PulsarClientImpl.this, topic, startMessageId, conf, listenerThread, consumerSubscribedFuture); synchronized (consumers) { consumers.put(reader.getConsumer(), Boolean.TRUE); } consumerSubscribedFuture.thenRun(() -> { readerFuture.complete(reader); }).exceptionally(ex -> { log.warn("[{}] Failed to get create topic reader", topic, ex); readerFuture.completeExceptionally(ex); return null; }); }).exceptionally(ex -> { log.warn("[{}] Failed to get partitioned topic metadata", topic, ex); readerFuture.completeExceptionally(ex); return null; }); return readerFuture; }
From source file:org.spongepowered.common.util.ServerUtils.java
public static CompletableFuture<Optional<WorldProperties>> copyWorld(MinecraftServer server, WorldProperties worldProperties, String copyName) { checkArgument(WorldPropertyRegistryModule.getInstance().isWorldRegistered(worldProperties.getUniqueId()), "World properties not registered"); checkArgument(!WorldPropertyRegistryModule.getInstance().isWorldRegistered(copyName), "Destination world name already is registered"); WorldInfo info = (WorldInfo) worldProperties; WorldServer world = DimensionManager.getWorldFromDimId(((IMixinWorldInfo) info).getDimensionId()); if (world != null) { try {/*from w w w. java2s .c om*/ world.saveAllChunks(true, null); } catch (MinecraftException e) { Throwables.propagate(e); } ((IMixinMinecraftServer) server).setSaveEnabled(false); } CompletableFuture<Optional<WorldProperties>> future = SpongeScheduler.getInstance() .submitAsyncTask(new CopyWorldTask(info, copyName)); if (world != null) { // World was loaded future.thenRun(() -> ((IMixinMinecraftServer) server).setSaveEnabled(true)); } return future; }