List of usage examples for java.util.concurrent CompletableFuture complete
public boolean complete(T value)
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
private CompletableFuture<Void> checkPersistencePolicies() { TopicName topicName = TopicName.get(topic); CompletableFuture<Void> future = new CompletableFuture<>(); brokerService.getManagedLedgerConfig(topicName).thenAccept(config -> { // update managed-ledger config and managed-cursor.markDeleteRate this.ledger.setConfig(config); future.complete(null); }).exceptionally(ex -> {//from w ww .ja v a 2 s . c o m log.warn("[{}] Failed to update persistence-policies {}", topic, ex.getMessage()); future.completeExceptionally(ex); return null; }); return future; }
From source file:org.apache.pulsar.client.impl.ConsumerImpl.java
@Override protected CompletableFuture<Message<T>> internalReceiveAsync() { CompletableFuture<Message<T>> result = new CompletableFuture<>(); Message<T> message = null;/*w w w . j av a2s. com*/ try { lock.writeLock().lock(); message = incomingMessages.poll(0, TimeUnit.MILLISECONDS); if (message == null) { pendingReceives.add(result); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); result.completeExceptionally(e); } finally { lock.writeLock().unlock(); } if (message != null) { trackMessage(message); Message<T> interceptMsg = beforeConsume(message); messageProcessed(interceptMsg); result.complete(interceptMsg); } return result; }
From source file:com.hurence.logisland.engine.vanilla.stream.amqp.AmqpClientPipelineStream.java
private CompletableFuture<ProtonConnection> setupConnection() { CompletableFuture<ProtonConnection> completableFuture = new CompletableFuture<>(); String hostname = streamContext.getPropertyValue(StreamOptions.CONNECTION_HOST).asString(); int port = streamContext.getPropertyValue(StreamOptions.CONNECTION_PORT).asInteger(); int credits = streamContext.getPropertyValue(StreamOptions.LINK_CREDITS).asInteger(); String user = streamContext.getPropertyValue(StreamOptions.CONNECTION_AUTH_USERNAME).asString(); String password = streamContext.getPropertyValue(StreamOptions.CONNECTION_AUTH_PASSWORD).asString(); if (user != null && password != null) { options.addEnabledSaslMechanism("PLAIN"); } else if (streamContext.getPropertyValue(StreamOptions.CONNECTION_AUTH_TLS_CERT).isSet()) { String tlsCert = streamContext.getPropertyValue(StreamOptions.CONNECTION_AUTH_TLS_CERT).asString(); String tlsKey = streamContext.getPropertyValue(StreamOptions.CONNECTION_AUTH_TLS_KEY).asString(); String caCert = streamContext.getPropertyValue(StreamOptions.CONNECTION_AUTH_CA_CERT).asString(); options.addEnabledSaslMechanism("EXTERNAL").setHostnameVerificationAlgorithm("") .setPemKeyCertOptions(new PemKeyCertOptions().addCertPath(new File(tlsCert).getAbsolutePath()) .addKeyPath(new File(tlsKey).getAbsolutePath())); if (caCert != null) { options.setPemTrustOptions(new PemTrustOptions().addCertPath(new File(caCert).getAbsolutePath())); }/*from w w w . j ava2s. com*/ } protonClient.connect(options, hostname, port, user, password, event -> { if (event.failed()) { handleConnectionFailure(false); completableFuture.completeExceptionally(event.cause()); return; } connectionControl.connected(); completableFuture.complete(event.result()); protonConnection = event.result(); String containerId = streamContext.getPropertyValue(StreamOptions.CONTAINER_ID).asString(); if (containerId != null) { protonConnection.setContainer(containerId); } protonConnection.closeHandler(x -> { handleConnectionFailure(true); }).disconnectHandler(x -> { handleConnectionFailure(false); }).openHandler(onOpen -> { //setup the output path sender = protonConnection .createSender(streamContext.getPropertyValue(StreamOptions.WRITE_TOPIC).asString()); sender.setAutoDrained(true); sender.setAutoSettle(true); sender.open(); //setup the input path receiver = protonConnection .createReceiver(streamContext.getPropertyValue(StreamOptions.READ_TOPIC).asString()); receiver.setPrefetch(credits); receiver.handler((delivery, message) -> { try { Record record; if (deserializer == null) { record = RecordUtils.getKeyValueRecord( StringUtils.defaultIfEmpty(message.getSubject(), ""), new String(extractBodyContent(message.getBody()))); } else { record = deserializer .deserialize(new ByteArrayInputStream(extractBodyContent(message.getBody()))); if (!record.hasField(FieldDictionary.RECORD_KEY)) { record.setField(FieldDictionary.RECORD_KEY, FieldType.STRING, message.getSubject()); } } Collection<Record> r = Collections.singleton(record); for (ProcessContext processContext : streamContext.getProcessContexts()) { r = processContext.getProcessor().process(processContext, r); } List<Message> toAdd = new ArrayList<>(); for (Record out : r) { ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); serializer.serialize(byteOutputStream, out); Message mo = ProtonHelper.message(); if (out.hasField(FieldDictionary.RECORD_KEY)) { mo.setSubject(out.getField(FieldDictionary.RECORD_KEY).asString()); } if (StringUtils.isNotBlank(contentType)) { mo.setContentType(contentType); } mo.setMessageId(out.getId()); mo.setBody(new Data(Binary.create(ByteBuffer.wrap(byteOutputStream.toByteArray())))); toAdd.add(mo); } toAdd.forEach(sender::send); delivery.disposition(Accepted.getInstance(), true); } catch (Exception e) { Rejected rejected = new Rejected(); delivery.disposition(rejected, true); getLogger().warn("Unable to process message : " + e.getMessage()); } }).open(); }).open(); }); return completableFuture; }
From source file:org.apache.pulsar.compaction.TwoPhaseCompactor.java
private void phaseTwoLoop(RawReader reader, MessageId to, Map<String, MessageId> latestForKey, LedgerHandle lh, Semaphore outstanding, CompletableFuture<Void> promise) { reader.readNextAsync().whenCompleteAsync((m, exception) -> { if (exception != null) { promise.completeExceptionally(exception); return; } else if (promise.isDone()) { return; }//from www. ja v a 2s . co m MessageId id = m.getMessageId(); Optional<RawMessage> messageToAdd = Optional.empty(); if (RawBatchConverter.isReadableBatch(m)) { try { messageToAdd = RawBatchConverter.rebatchMessage(m, (key, subid) -> latestForKey.get(key).equals(subid)); } catch (IOException ioe) { log.info("Error decoding batch for message {}. Whole batch will be included in output", id, ioe); messageToAdd = Optional.of(m); } } else { Pair<String, Integer> keyAndSize = extractKeyAndSize(m); MessageId msg; if (keyAndSize == null) { // pass through messages without a key messageToAdd = Optional.of(m); } else if ((msg = latestForKey.get(keyAndSize.getLeft())) != null && msg.equals(id)) { // consider message only if present into latestForKey map if (keyAndSize.getRight() <= 0) { promise.completeExceptionally(new IllegalArgumentException( "Compaction phase found empty record from sorted key-map")); } messageToAdd = Optional.of(m); } else { m.close(); // Reached to last-id and phase-one found it deleted-message while iterating on ledger so, not // present under latestForKey. Complete the compaction. if (to.equals(id)) { promise.complete(null); } } } messageToAdd.ifPresent((toAdd) -> { try { outstanding.acquire(); CompletableFuture<Void> addFuture = addToCompactedLedger(lh, toAdd) .whenComplete((res, exception2) -> { outstanding.release(); if (exception2 != null) { promise.completeExceptionally(exception2); } }); if (to.equals(id)) { addFuture.whenComplete((res, exception2) -> { if (exception2 == null) { promise.complete(null); } }); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); promise.completeExceptionally(ie); } }); phaseTwoLoop(reader, to, latestForKey, lh, outstanding, promise); }, scheduler); }
From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java
private void getProcedureResult(final long procId, CompletableFuture<Void> future) { this.<GetProcedureResultResponse>newMasterCaller() .action((controller, stub) -> this .<GetProcedureResultRequest, GetProcedureResultResponse, GetProcedureResultResponse>call( controller, stub, GetProcedureResultRequest.newBuilder().setProcId(procId).build(), (s, c, req, done) -> s.getProcedureResult(c, req, done), (resp) -> resp)) .call().whenComplete((response, error) -> { if (error != null) { LOG.warn("failed to get the procedure result procId=" + procId, ConnectionUtils.translateException(error)); connection.RETRY_TIMER.newTimeout(t -> getProcedureResult(procId, future), pauseNs, TimeUnit.NANOSECONDS); return; }//from w ww .j av a 2 s .c o m if (response.getState() == GetProcedureResultResponse.State.RUNNING) { connection.RETRY_TIMER.newTimeout(t -> getProcedureResult(procId, future), pauseNs, TimeUnit.NANOSECONDS); return; } if (response.hasException()) { IOException ioe = ForeignExceptionUtil.toIOException(response.getException()); future.completeExceptionally(ioe); } else { future.complete(null); } }); }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
private CompletableFuture<Subscription> getDurableSubscription(String subscriptionName, InitialPosition initialPosition) { CompletableFuture<Subscription> subscriptionFuture = new CompletableFuture<>(); ledger.asyncOpenCursor(Codec.encode(subscriptionName), initialPosition, new OpenCursorCallback() { @Override/* w w w . jav a 2s .c o m*/ public void openCursorComplete(ManagedCursor cursor, Object ctx) { if (log.isDebugEnabled()) { log.debug("[{}][{}] Opened cursor", topic, subscriptionName); } subscriptionFuture.complete(subscriptions.computeIfAbsent(subscriptionName, name -> createPersistentSubscription(subscriptionName, cursor))); } @Override public void openCursorFailed(ManagedLedgerException exception, Object ctx) { log.warn("[{}] Failed to create subscription for {}: {}", topic, subscriptionName, exception.getMessage()); USAGE_COUNT_UPDATER.decrementAndGet(PersistentTopic.this); subscriptionFuture.completeExceptionally(new PersistenceException(exception)); if (exception instanceof ManagedLedgerFencedException) { // If the managed ledger has been fenced, we cannot continue using it. We need to close and reopen close(); } } }, null); return subscriptionFuture; }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
public synchronized void triggerOffload(MessageIdImpl messageId) throws AlreadyRunningException { if (currentOffload.isDone()) { CompletableFuture<MessageIdImpl> promise = currentOffload = new CompletableFuture<>(); getManagedLedger().asyncOffloadPrefix(PositionImpl.get(messageId.getLedgerId(), messageId.getEntryId()), new OffloadCallback() { @Override//from w w w. j av a2 s. com public void offloadComplete(Position pos, Object ctx) { PositionImpl impl = (PositionImpl) pos; promise.complete(new MessageIdImpl(impl.getLedgerId(), impl.getEntryId(), -1)); } @Override public void offloadFailed(ManagedLedgerException exception, Object ctx) { promise.completeExceptionally(exception); } }, null); } else { throw new AlreadyRunningException("Offload already in progress"); } }
From source file:com.canoo.dolphin.client.ClientContextFactory.java
/** * Create a {@link ClientContext} based on the given configuration. This method doesn't block and returns a * {@link CompletableFuture} to receive its result. If the {@link ClientContext} can't be created the * {@link CompletableFuture#get()} will throw a {@link ClientInitializationException}. * * @param clientConfiguration the configuration * @return the future// www . j av a 2s.co m */ public static CompletableFuture<ClientContext> connect(final ClientConfiguration clientConfiguration) { Assert.requireNonNull(clientConfiguration, "clientConfiguration"); final CompletableFuture<ClientContext> result = new CompletableFuture<>(); Level openDolphinLogLevel = clientConfiguration.getDolphinLogLevel(); Logger openDolphinLogger = Logger.getLogger("org.opendolphin"); openDolphinLogger.setLevel(openDolphinLogLevel); Executors.newSingleThreadExecutor().execute(() -> { try { final ForwardableCallback<DolphinRemotingException> remotingErrorHandler = new ForwardableCallback<>(); final ClientDolphin clientDolphin = new ClientDolphin(); clientDolphin.setClientModelStore(new ClientModelStore(clientDolphin)); final HttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager()); final ClientConnector clientConnector = new DolphinPlatformHttpClientConnector(clientDolphin, new OptimizedJsonCodec(), httpClient, clientConfiguration.getServerEndpoint(), remotingErrorHandler, clientConfiguration.getUiThreadHandler()); clientDolphin.setClientConnector(clientConnector); final DolphinCommandHandler dolphinCommandHandler = new DolphinCommandHandler(clientDolphin); final EventDispatcher dispatcher = new ClientEventDispatcher(clientDolphin); final BeanRepository beanRepository = new BeanRepositoryImpl(clientDolphin, dispatcher); final Converters converters = new Converters(beanRepository); final PresentationModelBuilderFactory builderFactory = new ClientPresentationModelBuilderFactory( clientDolphin); final ClassRepository classRepository = new ClassRepositoryImpl(clientDolphin, converters, builderFactory); final ListMapper listMapper = new ListMapperImpl(clientDolphin, classRepository, beanRepository, builderFactory, dispatcher); final BeanBuilder beanBuilder = new ClientBeanBuilderImpl(classRepository, beanRepository, listMapper, builderFactory, dispatcher); final ClientPlatformBeanRepository platformBeanRepository = new ClientPlatformBeanRepository( clientDolphin, beanRepository, dispatcher, converters); final ClientBeanManagerImpl clientBeanManager = new ClientBeanManagerImpl(beanRepository, beanBuilder, clientDolphin); final ControllerProxyFactory controllerProxyFactory = new ControllerProxyFactoryImpl( platformBeanRepository, dolphinCommandHandler, clientDolphin); final ClientContext clientContext = new ClientContextImpl(clientConfiguration, clientDolphin, controllerProxyFactory, dolphinCommandHandler, platformBeanRepository, clientBeanManager, remotingErrorHandler); clientDolphin.startPushListening(PlatformConstants.POLL_EVENT_BUS_COMMAND_NAME, PlatformConstants.RELEASE_EVENT_BUS_COMMAND_NAME); clientConfiguration.getUiThreadHandler() .executeInsideUiThread(() -> result.complete(clientContext)); } catch (Exception e) { result.obtrudeException(new ClientInitializationException("Can not connect to server!", e)); throw new ClientInitializationException(e); } }); return result; }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
CompletableFuture<Void> removeReplicator(String remoteCluster) { log.info("[{}] Removing replicator to {}", topic, remoteCluster); final CompletableFuture<Void> future = new CompletableFuture<>(); String name = PersistentReplicator.getReplicatorName(replicatorPrefix, remoteCluster); replicators.get(remoteCluster).disconnect().thenRun(() -> { ledger.asyncDeleteCursor(name, new DeleteCursorCallback() { @Override//from w w w .j a v a 2 s . c om public void deleteCursorComplete(Object ctx) { replicators.remove(remoteCluster); future.complete(null); } @Override public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) { log.error("[{}] Failed to delete cursor {} {}", topic, name, exception.getMessage(), exception); future.completeExceptionally(new PersistenceException(exception)); } }, null); }).exceptionally(e -> { log.error("[{}] Failed to close replication producer {} {}", topic, name, e.getMessage(), e); future.completeExceptionally(e); return null; }); return future; }
From source file:org.apache.pulsar.broker.service.persistent.PersistentTopic.java
/** * Delete the cursor ledger for a given subscription * * @param subscriptionName/* ww w. ja v a 2 s . c om*/ * Subscription for which the cursor ledger is to be deleted * @return Completable future indicating completion of unsubscribe operation Completed exceptionally with: * ManagedLedgerException if cursor ledger delete fails */ @Override public CompletableFuture<Void> unsubscribe(String subscriptionName) { CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>(); ledger.asyncDeleteCursor(Codec.encode(subscriptionName), new DeleteCursorCallback() { @Override public void deleteCursorComplete(Object ctx) { if (log.isDebugEnabled()) { log.debug("[{}][{}] Cursor deleted successfully", topic, subscriptionName); } subscriptions.remove(subscriptionName); unsubscribeFuture.complete(null); lastActive = System.nanoTime(); } @Override public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) { if (log.isDebugEnabled()) { log.debug("[{}][{}] Error deleting cursor for subscription", topic, subscriptionName, exception); } unsubscribeFuture.completeExceptionally(new PersistenceException(exception)); } }, null); return unsubscribeFuture; }