Java Code Examples for com.google.common.util.concurrent.ListenableFuture

The following examples show how to use com.google.common.util.concurrent.ListenableFuture. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Executes a query to retrieve all the table rows
 *
 * @param query The Query instance to execute
 */
public ListenableFuture<MobileServiceList<E>> execute(Query query) {
    final SettableFuture<MobileServiceList<E>> future = SettableFuture.create();
    ListenableFuture<JsonElement> internalFuture = mInternalTable.execute(query);
    Futures.addCallback(internalFuture, new FutureCallback<JsonElement>() {
        @Override
        public void onFailure(Throwable exc) {
            future.setException(exc);
        }

        @Override
        public void onSuccess(JsonElement result) {
            processQueryResults(result, future);
        }
    }, MoreExecutors.directExecutor());

    return future;
}
 
Example 2
/**
 * Unregisters the client for native notifications
 *
 * @param callback The operation callback
 * @deprecated use {@link #unregister()} instead
 */
public void unregister(final UnregisterCallback callback) {
    ListenableFuture<Void> deleteInstallationFuture = deleteInstallation();

    Futures.addCallback(deleteInstallationFuture, new FutureCallback<Void>() {
        @Override
        public void onFailure(Throwable exception) {
            if (exception instanceof Exception) {
                callback.onUnregister((Exception) exception);
            }
        }

        @Override
        public void onSuccess(Void v) {
            callback.onUnregister(null);
        }
    }, MoreExecutors.directExecutor());
}
 
Example 3
Source Project: drift   Source File: TestingMethodInvocationStat.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void recordResult(long startTime, ListenableFuture<Object> result)
{
    invocations.incrementAndGet();
    result.addListener(
            () -> {
                lastStartTime.set(startTime);
                try {
                    result.get();
                    successes.incrementAndGet();
                }
                catch (Throwable throwable) {
                    failures.incrementAndGet();
                }
            },
            directExecutor());
}
 
Example 4
Source Project: connector-sdk   Source File: GroupsServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static <T> ListenableFuture<T> batchRequest(
    AbstractGoogleJsonClientRequest<T> requestToExecute,
    RetryPolicy retryPolicy,
    BatchRequestService batchService)
    throws IOException {
  AsyncRequest<T> request =
      new AsyncRequest<>(requestToExecute, retryPolicy, GROUP_SERVICE_STATS);
  try {
    API_RATE_LIMITER.acquire();
    batchService.add(request);
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new IOException("Interrupted while batching request", e);
  }
  return request.getFuture();
}
 
Example 5
Source Project: arcusplatform   Source File: HealthCheckDirectiveHandler.java    License: Apache License 2.0 6 votes vote down vote up
private ListenableFuture<HealthCheckResponse> ping() {
   PlatformMessage msg = PlatformMessage.buildRequest(MessageBody.ping(), AlexaUtil.ADDRESS_BRIDGE, STATUS_SERVICE)
         .withCorrelationId(IrisUUID.randomUUID().toString())
         .create();

   ListenableFuture<PlatformMessage> future = platSvc.request(
      msg,
      (pm) -> Objects.equals(msg.getCorrelationId(), pm.getCorrelationId()), config.getHealthCheckTimeoutSecs()
   );

   return Futures.transform(future, (Function<PlatformMessage, HealthCheckResponse>) input -> {
      HealthCheckResponse response = new HealthCheckResponse();
      response.setHealthy(true);
      response.setDescription("The system is currently healthy");
      return response;
   }, MoreExecutors.directExecutor());
}
 
Example 6
Source Project: GreenBits   Source File: GaService.java    License: GNU General Public License v3.0 6 votes vote down vote up
public ListenableFuture<String>
sendTransaction(final List<byte[]> sigs, final Object twoFacData) {
    // FIXME: The server should return the full limits including is_fiat from send_tx
    return Futures.transform(mClient.sendTransaction(sigs, twoFacData),
                             new Function<String, String>() {
               @Override
               public String apply(final String txHash) {
                   try {
                       mLimitsData = mClient.getSpendingLimits();
                   } catch (final Exception e) {
                       // We don't know what the new limit is so nuke it
                       mLimitsData.mData.put("total", 0);
                       e.printStackTrace();
                   }
                   return txHash;
               }
    }, mExecutor);
}
 
Example 7
Source Project: connector-sdk   Source File: IdentityGroup.java    License: Apache License 2.0 6 votes vote down vote up
/** Removes {@link IdentityGroup} from Cloud Identity Groups API using {@code service}. */
@Override
public ListenableFuture<Boolean> unmap(IdentityService service) throws IOException {
  ListenableFuture<Operation> deleteGroup = service.deleteGroup(groupResourceName.get());
  return Futures.transform(
      deleteGroup,
      new Function<Operation, Boolean>() {
        @Override
        @Nullable
        public Boolean apply(@Nullable Operation input) {
          try {
            validateOperation(input);
            return true;
          } catch (IOException e) {
            logger.log(Level.WARNING, String.format("Failed to delete Group %s", groupKey), e);
            return false;
          }
        }
      },
      getExecutor());
}
 
Example 8
Source Project: iotplatform   Source File: BaseAlarmService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<AlarmInfo> findAlarmInfoByIdAsync(AlarmId alarmId) {
    log.trace("Executing findAlarmInfoByIdAsync [{}]", alarmId);
    validateId(alarmId, "Incorrect alarmId " + alarmId);
    return Futures.transform(alarmDao.findAlarmByIdAsync(alarmId.getId()),
            (AsyncFunction<Alarm, AlarmInfo>) alarm1 -> {
                AlarmInfo alarmInfo = new AlarmInfo(alarm1);
                return Futures.transform(
                        entityService.fetchEntityNameAsync(alarmInfo.getOriginator()), (Function<String, AlarmInfo>)
                                originatorName -> {
                                    alarmInfo.setOriginatorName(originatorName);
                                    return alarmInfo;
                                }
                );
            });
}
 
Example 9
Source Project: presto   Source File: SqlTaskExecution.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<?> processFor(Duration duration)
{
    Driver driver;
    synchronized (this) {
        // if close() was called before we get here, there's not point in even creating the driver
        if (closed) {
            return Futures.immediateFuture(null);
        }

        if (this.driver == null) {
            this.driver = driverSplitRunnerFactory.createDriver(driverContext, partitionedSplit);
        }

        driver = this.driver;
    }

    return driver.processFor(duration);
}
 
Example 10
@Test
public void testGetChildren_NewChild()
{
    TestCar car = _model.getObjectFactory().create(TestCar.class, Collections.<String, Object>singletonMap(ConfiguredObject.NAME, "myCar"), null);

    String engineName = "myEngine";
    Map<String, Object> engineAttributes = new HashMap<>();
    engineAttributes.put(ConfiguredObject.NAME, engineName);

    TestEngine engine = (TestEngine) car.createChild(TestEngine.class, engineAttributes);

    // Check we can observe the new child from the parent

    assertEquals((long) 1, (long) car.getChildren(TestEngine.class).size());
    assertEquals(engine, car.getChildById(TestEngine.class, engine.getId()));
    assertEquals(engine, car.getChildByName(TestEngine.class, engine.getName()));

    ListenableFuture attainedChild = car.getAttainedChildByName(TestEngine.class, engine.getName());
    assertNotNull(attainedChild);
    assertTrue("Engine should have already attained state", attainedChild.isDone());
}
 
Example 11
Source Project: codebuff   Source File: LocalCache.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Refreshes the value associated with {@code key}, unless another thread is already doing so.
 * Returns the newly refreshed value associated with {@code key} if it was refreshed inline, or
 * {@code null} if another thread is performing the refresh or if an error occurs during
 * refresh.
 */

@Nullable
V refresh(K key, int hash, CacheLoader<? super K, V> loader, boolean checkTime) {
  final LoadingValueReference<K, V> loadingValueReference = insertLoadingValueReference(key, hash, checkTime);
  if (loadingValueReference == null) {
    return null;
  }
  ListenableFuture<V> result = loadAsync(key, hash, loadingValueReference, loader);
  if (result.isDone()) {
    try {
      return Uninterruptibles.getUninterruptibly(result);
    } catch (Throwable t) {
      // don't let refresh exceptions propagate; error was already logged
    }
  }
  return null;
}
 
Example 12
Source Project: codebuff   Source File: LocalCache.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
ListenableFuture<V> loadAsync(
  final K key,
  final int hash,
  final LoadingValueReference<K, V> loadingValueReference,
  CacheLoader<? super K, V> loader) {
  final ListenableFuture<V> loadingFuture = loadingValueReference.loadFuture(key, loader);
  loadingFuture.addListener(new Runnable() {
                              @Override
                              public void run() {
                                try {
                                  getAndRecordStats(key, hash, loadingValueReference, loadingFuture);
                                } catch (Throwable t) {
                                  logger.log(Level.WARNING, "Exception thrown during refresh", t);
                                  loadingValueReference.setException(t);
                                }
                              }
                            }, directExecutor());
  return loadingFuture;
}
 
Example 13
Source Project: android-test   Source File: JavascriptEvaluation.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<String> apply(final PreparedScript in) {
  if (null != in.conduit) {
    return Futures.<String>immediateFailedFuture(
        new RuntimeException("Conduit script cannot be used"));
  } else {
    final ValueCallbackFuture<String> result = new ValueCallbackFuture<String>();
    if (Looper.myLooper() == Looper.getMainLooper()) {
      in.view.evaluateJavascript(in.script, result);
    } else {
      in.view.post(
          new Runnable() {
            @Override
            public void run() {
              in.view.evaluateJavascript(in.script, result);
            }
          });
    }
    return result;
  }
}
 
Example 14
public ListenableFuture<?> publishAsync(final Exchange exchange, final Message message, final @Nullable BasicProperties properties, final @Nullable Publish publish) {
    // NOTE: Serialization must happen synchronously, because getter methods may not be thread-safe
    final String payload = gson.toJson(message);
    final AMQP.BasicProperties finalProperties = getProperties(message, properties);
    final Publish finalPublish = Publish.forMessage(message, publish);

    if(this.executorService == null) throw new IllegalStateException("Not connected");
    return this.executorService.submit(new Runnable() {
        @Override
        public void run() {
            try {
                publish(exchange, payload, finalProperties, finalPublish);
            } catch(Throwable e) {
                logger.log(Level.SEVERE, "Unhandled exception publishing message type " + finalProperties.getType(), e);
            }
        }
    });
}
 
Example 15
Source Project: Groza   Source File: DeviceServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<List<Device>> findDevicesByQuery(DeviceSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Device>> devices = Futures.transformAsync(relations, r -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Device>> futures = new ArrayList<>();
        for (EntityRelation relation : r) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.DEVICE) {
                futures.add(findDeviceByIdAsync(new DeviceId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });

    devices = Futures.transform(devices, new Function<List<Device>, List<Device>>() {
        @Nullable
        @Override
        public List<Device> apply(@Nullable List<Device> deviceList) {
            return deviceList == null ? Collections.emptyList() : deviceList.stream().filter(device -> query.getDeviceTypes().contains(device.getType())).collect(Collectors.toList());
        }
    });

    return devices;
}
 
Example 16
Source Project: async-datastore-client   Source File: ExampleAsync.java    License: Apache License 2.0 5 votes vote down vote up
private static ListenableFuture<MutationResult> addData(final Datastore datastore) {
  final Insert insert = QueryBuilder.insert("employee", 1234567L)
      .value("fullname", "Fred Blinge")
      .value("inserted", new Date())
      .value("age", 40);
  return datastore.executeAsync(insert);
}
 
Example 17
Source Project: TakinRPC   Source File: Leader.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public ListenableFuture<Boolean> commitOperation(@Nonnull RaftStateContext ctx, @Nonnull byte[] operation) throws RaftException {

    resetTimeout(ctx);
    long index = log.append(operation);
    SettableFuture<Boolean> f = SettableFuture.create();
    requests.put(index, f);
    return commit(ctx);

}
 
Example 18
Source Project: flink   Source File: CassandraPojoOutputFormat.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void writeRecord(OUT record) throws IOException {
	if (exception != null) {
		throw new IOException("write record failed", exception);
	}

	ListenableFuture<Void> result = mapper.saveAsync(record);
	Futures.addCallback(result, callback);
}
 
Example 19
@Override
public ListenableFuture<FindMultiResponse<Session>> staff(ServerDoc.Network network, boolean disguised) {
    return client().get(new QueryUri(collectionUri())
                            .put("network", network)
                            .put("staff", true)
                            .put("online", true)
                            .put("disguised", disguised)
                            .encode(),
                        meta.multiResponseType(),
                        HttpOption.INFINITE_RETRY);
}
 
Example 20
Source Project: arcusplatform   Source File: ReflectiveCommandHandler.java    License: Apache License 2.0 5 votes vote down vote up
private static Function<Object, ListenableFuture<MessageBody>> extractResponseWrapper(CommandDefinition command, Method m) {
	boolean methodIsAsync = ListenableFuture.class.equals(m.getReturnType());
	Type returnType = m.getGenericReturnType();
	Map<String, AttributeDefinition> returnDefinitions = command.getReturnParameters();

   if(returnDefinitions.size() > 0 && !returnDefinitions.containsKey("response") && !returnDefinitions.containsKey("attributes")) {
      throw new IllegalArgumentException("The reflective command handler only supports commands with no return parameters, or 1 return parameter named 'response' or 'attribute'");
   }

	AttributeType responseType = VoidType.INSTANCE;
	if(returnDefinitions.containsKey("response")) {
	   responseType = returnDefinitions.get("response").getAttributeType();
	} else if(returnDefinitions.containsKey("attributes")) {
	   responseType = returnDefinitions.get("attributes").getAttributeType();
	}
	if(methodIsAsync) {
		if(returnType instanceof ParameterizedType) {
			returnType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
		}
		else if(returnType instanceof Class) {
			LOGGER.warn("Missing type information due to erasures, possible cast issues");
			returnType = responseType.getJavaType();
		}
		else {
			throw new IllegalArgumentException("Invalid return type [" + m.getGenericReturnType() + "] expected [" + responseType + "] or [ListenableFuture<" + responseType + ">]");
		}
	}

	if(!responseType.isAssignableFrom(returnType)) {
		throw new IllegalArgumentException("Invalid return type [" + m.getGenericReturnType() + "] expected [" + responseType + "] or [ListenableFuture<" + responseType + ">]");
	}
	com.google.common.base.Function<Object, MessageBody> translator = createTranslator(command);
	if(methodIsAsync) {
		return (o) -> ReflectiveCommandHandler.translateFromAsync(o, translator);
	}
	else {
		return (o) -> ReflectiveCommandHandler.translateFromSync(o, translator);
	}
}
 
Example 21
Source Project: presto   Source File: TestPartitionedOutputBuffer.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDestroyFreesReader()
{
    PartitionedOutputBuffer buffer = createPartitionedBuffer(
            createInitialEmptyOutputBuffers(PARTITIONED)
                    .withBuffer(FIRST, 0)
                    .withNoMoreBufferIds(),
            sizeOfPages(5));
    assertFalse(buffer.isFinished());

    // attempt to get a page
    ListenableFuture<BufferResult> future = buffer.get(FIRST, 0, sizeOfPages(10));

    // verify we are waiting for a page
    assertFalse(future.isDone());

    // add one page
    addPage(buffer, createPage(0));

    // verify we got one page
    assertBufferResultEquals(TYPES, getFuture(future, NO_WAIT), bufferResult(0, createPage(0)));

    // attempt to get another page, and verify we are blocked
    future = buffer.get(FIRST, 1, sizeOfPages(10));
    assertFalse(future.isDone());

    // destroy the buffer
    buffer.destroy();
    assertQueueClosed(buffer, FIRST, 1);

    // verify the future completed
    assertBufferResultEquals(TYPES, getFuture(future, NO_WAIT), emptyResults(TASK_INSTANCE_ID, 1, false));
}
 
Example 22
Source Project: presto   Source File: LazyOutputBuffer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<?> isFull()
{
    OutputBuffer outputBuffer;
    synchronized (this) {
        checkState(delegate != null, "Buffer has not been initialized");
        outputBuffer = delegate;
    }
    return outputBuffer.isFull();
}
 
Example 23
@SuppressWarnings("WeakerAccess")
void tryToProvidePreviewSurface() {
    /*
      Should only continue if:
      - The preview size has been specified.
      - The textureView's surfaceTexture is available (after TextureView
      .SurfaceTextureListener#onSurfaceTextureAvailable is invoked)
      - The surfaceCompleter has been set (after CallbackToFutureAdapter
      .Resolver#attachCompleter is invoked).
     */
    if (mResolution == null || mSurfaceTexture == null || mSurfaceRequest == null) {
        return;
    }

    mSurfaceTexture.setDefaultBufferSize(mResolution.getWidth(), mResolution.getHeight());

    final Surface surface = new Surface(mSurfaceTexture);
    final ListenableFuture<Result> surfaceReleaseFuture =
            CallbackToFutureAdapter.getFuture(completer -> {
                mSurfaceRequest.provideSurface(surface,
                        CameraXExecutors.directExecutor(), completer::set);
                return "provideSurface[request=" + mSurfaceRequest + " surface=" + surface
                        + "]";
            });
    mSurfaceReleaseFuture = surfaceReleaseFuture;
    mSurfaceReleaseFuture.addListener(() -> {
        surface.release();
        if (mSurfaceReleaseFuture == surfaceReleaseFuture) {
            mSurfaceReleaseFuture = null;
        }
    }, ContextCompat.getMainExecutor(mTextureView.getContext()));

    mSurfaceRequest = null;

    correctPreviewForCenterCrop(mParent, mTextureView, mResolution);
}
 
Example 24
Source Project: shardingsphere   Source File: ExecutorKernel.java    License: Apache License 2.0 5 votes vote down vote up
private <I, O> Collection<ListenableFuture<Collection<O>>> asyncExecute(final List<InputGroup<I>> inputGroups, final ExecutorCallback<I, O> callback) {
    Collection<ListenableFuture<Collection<O>>> result = new LinkedList<>();
    for (InputGroup<I> each : inputGroups) {
        result.add(asyncExecute(each, callback));
    }
    return result;
}
 
Example 25
Source Project: big-c   Source File: AsyncLoggerSet.java    License: Apache License 2.0 5 votes vote down vote up
QuorumCall<AsyncLogger, Void> doPreUpgrade() {
  Map<AsyncLogger, ListenableFuture<Void>> calls =
      Maps.newHashMap();
  for (AsyncLogger logger : loggers) {
    ListenableFuture<Void> future =
        logger.doPreUpgrade();
    calls.put(logger, future);
  }
  return QuorumCall.create(calls);
}
 
Example 26
Source Project: green_android   Source File: Peer.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Asks the connected peer for the given transaction from its memory pool. Transactions in the chain cannot be
 * retrieved this way because peers don't have a transaction ID to transaction-pos-on-disk index, and besides,
 * in future many peers will delete old transaction data they don't need.
 */
@SuppressWarnings("unchecked")
// The 'unchecked conversion' warning being suppressed here comes from the sendSingleGetData() formally returning
// ListenableFuture instead of ListenableFuture<Transaction>. This is okay as sendSingleGetData() actually returns
// ListenableFuture<Transaction> in this context. Note that sendSingleGetData() is also used for Blocks.
public ListenableFuture<Transaction> getPeerMempoolTransaction(Sha256Hash hash) {
    // This does not need to be locked.
    // TODO: Unit test this method.
    log.info("Request to fetch peer mempool tx  {}", hash);
    GetDataMessage getdata = new GetDataMessage(params);
    getdata.addTransaction(hash);
    return sendSingleGetData(getdata);
}
 
Example 27
Source Project: drift   Source File: ThrowingServiceHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public synchronized ListenableFuture<String> await()
{
    createFuture.set(null);
    if (awaitFuture == null) {
        awaitFuture = SettableFuture.create();
    }
    return awaitFuture;
}
 
Example 28
Source Project: bazel-buildfarm   Source File: CFCExecFileSystem.java    License: Apache License 2.0 5 votes vote down vote up
private ListenableFuture<Void> linkDirectory(
    Path execPath, Digest digest, Map<Digest, Directory> directoriesIndex) {
  return transformAsync(
      fileCache.putDirectory(digest, directoriesIndex, fetchService),
      (cachePath) -> {
        Files.createSymbolicLink(execPath, cachePath);
        return immediateFuture(null);
      },
      fetchService);
}
 
Example 29
Source Project: emodb   Source File: S3ScanWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Starts an asynchronous upload and returns a ListenableFuture for handling the result.
 */
synchronized ListenableFuture<String> upload() {
    // Reset values from possible prior attempt
    _attempts += 1;
    _bytesTransferred = 0;

    // Separate the future returned to the caller from the future generated by submitting the
    // putObject request.  If the writer is closed then uploadFuture may be canceled before it executes,
    // in which case it may not trigger any callbacks.  To ensure there is always a callback resultFuture is
    // tracked independently and, in the event that the upload is aborted, gets set on abort().
    _resultFuture = SettableFuture.create();

    _uploadFuture = _uploadService.submit(new Runnable() {
        @Override
        public void run() {
            try {
                ProgressListener progressListener = new ProgressListener() {
                    @Override
                    public void progressChanged(ProgressEvent progressEvent) {
                        // getBytesTransferred() returns zero for all events not pertaining to the file transfer
                        _bytesTransferred += progressEvent.getBytesTransferred();
                    }
                };

                PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, _key, _file);
                putObjectRequest.setGeneralProgressListener(progressListener);
                PutObjectResult result = _amazonS3.putObject(putObjectRequest);
                _resultFuture.set(result.getETag());
            } catch (Throwable t) {
                _resultFuture.setException(t);
            }
        }
    });

    return _resultFuture;
}
 
Example 30
Source Project: hivemq-community-edition   Source File: TopicTreeStartup.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Populates the topic tree with all information from the ClientSessionPersistence
 */
private void populateTopicTree() {
    final ListenableFuture<Set<String>> clientsFuture = clientSessionPersistence.getAllClients();
    // Blocking. The TopicTreeStartup needs to be done before new connections are allowed.
    try {
        final Set<String> clients = clientsFuture.get();
        for (final String client : clients) {
            final Set<Topic> clientSubscriptions = clientSessionSubscriptionPersistence.getSubscriptions(client);
            final ClientSession session = clientSessionPersistence.getSession(client, false);
            if (session == null || session.getSessionExpiryInterval() == SESSION_EXPIRE_ON_DISCONNECT) {
                // We don't have to remove the subscription from the topic tree, since it is not added to the topic tree yet.
                clientSessionSubscriptionPersistence.removeAllLocally(client);
                continue;
            }

            for (final Topic topic : clientSubscriptions) {
                final SharedSubscription sharedSubscription = sharedSubscriptionService.checkForSharedSubscription(topic.getTopic());
                if (sharedSubscription == null) {
                    topicTree.addTopic(client, topic, SubscriptionFlags.getDefaultFlags(false, topic.isRetainAsPublished(), topic.isNoLocal()), null);
                } else {
                    topicTree.addTopic(client, new Topic(sharedSubscription.getTopicFilter(), topic.getQoS(), topic.isNoLocal(), topic.isRetainAsPublished()), SubscriptionFlags.getDefaultFlags(true, topic.isRetainAsPublished(), topic.isNoLocal()), sharedSubscription.getShareName());
                }
            }
        }
    } catch (final Exception ex) {
        log.error("Failed to bootstrap topic tree.", ex);
    }
}
 
Example 31
Source Project: iotplatform   Source File: JpaAlarmDao.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type) {
  return service.submit(() -> {
    List<AlarmEntity> latest = alarmRepository.findLatestByOriginatorAndType(
        UUIDConverter.fromTimeUUID(tenantId.getId()), UUIDConverter.fromTimeUUID(originator.getId()),
        originator.getEntityType(), type, new PageRequest(0, 1));
    return latest.isEmpty() ? null : DaoUtil.getData(latest.get(0));
  });
}
 
Example 32
Source Project: phoenix-omid   Source File: InMemoryCommitTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Void> deleteCommitEntry(long startTimestamp) {
    startTimestamp = removeCheckpointBits(startTimestamp);
    SettableFuture<Void> f = SettableFuture.create();
    table.remove(startTimestamp);
    f.set(null);
    return f;
}
 
Example 33
@Nullable
private static TestContext fromClasses(
    ListenableFuture<TargetInfo> target, Set<PsiClass> classes) {
  Map<PsiClass, Collection<Location<?>>> methodsPerClass =
      classes.stream().collect(Collectors.toMap(c -> c, c -> ImmutableList.of()));
  String filter = BlazeJUnitTestFilterFlags.testFilterForClassesAndMethods(methodsPerClass);
  if (filter == null || filter.isEmpty()) {
    return null;
  }

  PsiClass sampleClass =
      classes.stream()
          .min(
              Comparator.comparing(
                  PsiClass::getName, Comparator.nullsLast(Comparator.naturalOrder())))
          .orElse(null);
  if (sampleClass == null) {
    return null;
  }
  String name = sampleClass.getName();
  if (name != null && classes.size() > 1) {
    name += String.format(" and %s others", classes.size() - 1);
  }
  return TestContext.builder(sampleClass, ExecutorType.DEBUG_SUPPORTED_TYPES)
      .setTarget(target)
      .setTestFilter(filter)
      .setDescription(name)
      .build();
}
 
Example 34
Source Project: aliyun-log-java-producer   Source File: Producer.java    License: Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, List, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example 35
Source Project: presto   Source File: BackgroundHiveSplitLoader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(HiveSplitSource splitSource)
{
    this.hiveSplitSource = splitSource;
    for (int i = 0; i < loaderConcurrency; i++) {
        ListenableFuture<?> future = ResumableTasks.submit(executor, new HiveSplitLoaderTask());
        addExceptionCallback(future, hiveSplitSource::fail); // best effort; hiveSplitSource could be already completed
    }
}
 
Example 36
Source Project: presto   Source File: SqlTaskManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<TaskStatus> getTaskStatus(TaskId taskId, TaskState currentState)
{
    requireNonNull(taskId, "taskId is null");
    requireNonNull(currentState, "currentState is null");

    SqlTask sqlTask = tasks.getUnchecked(taskId);
    sqlTask.recordHeartbeat();
    return sqlTask.getTaskStatus(currentState);
}
 
Example 37
Source Project: presto   Source File: OperatorContext.java    License: Apache License 2.0 5 votes vote down vote up
public void recordBlocked(ListenableFuture<?> blocked)
{
    requireNonNull(blocked, "blocked is null");

    BlockedMonitor monitor = new BlockedMonitor();

    BlockedMonitor oldMonitor = blockedMonitor.getAndSet(monitor);
    if (oldMonitor != null) {
        oldMonitor.run();
    }

    blocked.addListener(monitor, executor);
    // Do not register blocked with driver context.  The driver handles this directly.
}
 
Example 38
Source Project: twill   Source File: ZKDiscoveryService.java    License: Apache License 2.0 5 votes vote down vote up
ListenableFuture<?> asyncCancel() {
  if (!cancelled.compareAndSet(false, true)) {
    return Futures.immediateFuture(null);
  }

  // Take a snapshot of the volatile path.
  String path = this.path;

  // If it is null, meaning cancel is called before the ephemeral node is created, hence
  // setPath() will be called in future (through zk callback when creation is completed)
  // so that deletion will be done in setPath().
  if (path == null) {
    return Futures.immediateFuture(null);
  }

  // Remove this Cancellable from the map so that upon session expiration won't try to register.
  lock.lock();
  try {
    discoverables.remove(discoverable, this);
  } finally {
    lock.unlock();
  }

  // Delete the path. It's ok if the path not exists
  // (e.g. what session expired and before node has been re-created)
  try {
    return ZKOperations.ignoreError(zkClient.delete(path), KeeperException.NoNodeException.class, path);
  } catch (Exception e) {
    return Futures.immediateFailedFuture(e);
  }
}
 
Example 39
Source Project: qpid-broker-j   Source File: BrokerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected <C extends ConfiguredObject> ListenableFuture<C> addChildAsync(final Class<C> childClass,
                                                                      final Map<String, Object> attributes)
{
    if (childClass == VirtualHostNode.class)
    {
        return (ListenableFuture<C>) createVirtualHostNodeAsync(attributes);
    }
    else
    {
        return super.addChildAsync(childClass, attributes);
    }
}
 
Example 40
Source Project: iotplatform   Source File: AssetController.java    License: Apache License 2.0 5 votes vote down vote up
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/asset/types", method = RequestMethod.GET)
@ResponseBody
public List<TenantAssetType> getAssetTypes() throws IoTPException {
  try {
    SecurityUser user = getCurrentUser();
    TenantId tenantId = user.getTenantId();
    ListenableFuture<List<TenantAssetType>> assetTypes = assetService.findAssetTypesByTenantId(tenantId);
    return checkNotNull(assetTypes.get());
  } catch (Exception e) {
    throw handleException(e);
  }
}