本文整理汇总了Java中com.google.common.util.concurrent.SettableFuture.set方法的典型用法代码示例。如果您正苦于以下问题:Java SettableFuture.set方法的具体用法?Java SettableFuture.set怎么用?Java SettableFuture.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.util.concurrent.SettableFuture
的用法示例。
在下文中一共展示了SettableFuture.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateCommitted
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
/**
* Find the median value of the list of matchIndex, this value is the committedIndex since, by definition, half of the
* matchIndex values are greater and half are less than this value. So, at least half of the replicas have stored the
* median value, this is the definition of committed.
*/
private void updateCommitted() {
List<ReplicaManager> sorted = newArrayList(managers.values());
Collections.sort(sorted, new Comparator<ReplicaManager>() {
@Override
public int compare(ReplicaManager o, ReplicaManager o2) {
return Longs.compare(o.getMatchIndex(), o2.getMatchIndex());
}
});
final int middle = (int) Math.ceil(sorted.size() / 2.0);
final long committed = sorted.get(middle).getMatchIndex();
log.updateCommitIndex(committed);
SortedMap<Long, SettableFuture<Boolean>> entries = requests.headMap(committed + 1);
for (SettableFuture<Boolean> f : entries.values()) {
f.set(true);
}
entries.clear();
}
示例2: deleteNode
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Override
protected ListenableFuture<Boolean> deleteNode(String path, boolean deleteChildrenIfNecessary) {
final SettableFuture<Boolean> future = SettableFuture.create();
boolean ret = true;
if (FileUtils.isFileExists(path)) {
if (!deleteChildrenIfNecessary && FileUtils.hasChildren(path)) {
LOG.severe("delete called on a path with children but deleteChildrenIfNecessary is false: "
+ path);
ret = false;
} else {
ret = FileUtils.deleteFile(path);
}
}
future.set(ret);
return future;
}
示例3: setException
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
private void setException(SettableFuture<Long> result, Throwable e, CreateTableAnalyzedStatement statement) {
e = Exceptions.unwrap(e);
String message = e.getMessage();
// sometimes message is empty
if ("mapping [default]".equals(message) && e.getCause() != null) {
// this is a generic mapping parse exception,
// the cause has usually a better more detailed error message
result.setException(e.getCause());
} else if (statement.ifNotExists() &&
(e instanceof IndexAlreadyExistsException
|| (e instanceof IndexTemplateAlreadyExistsException && statement.templateName() != null))) {
result.set(null);
} else {
result.setException(e);
}
}
示例4: externalMiner_shouldWork
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Test
public void externalMiner_shouldWork() throws Exception {
final Block startBestBlock = bc.getBlockchain().getBestBlock();
final SettableFuture<MinerIfc.MiningResult> futureBlock = SettableFuture.create();
blockMiner.setExternalMiner(new MinerIfc() {
@Override
public ListenableFuture<MiningResult> mine(Block block) {
// System.out.print("Mining requested");
return futureBlock;
}
@Override
public boolean validate(BlockHeader blockHeader) {
return true;
}
});
Block b = bc.getBlockchain().createNewBlock(startBestBlock, EMPTY_LIST, EMPTY_LIST);
Ethash.getForBlock(SystemProperties.getDefault(), b.getNumber()).mineLight(b).get();
futureBlock.set(new MinerIfc.MiningResult(ByteUtil.byteArrayToLong(b.getNonce()), b.getMixHash(), b));
assertThat(bc.getBlockchain().getBestBlock().getNumber(), is(startBestBlock.getNumber() + 1));
}
示例5: saveSnapshotsToFile
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@SuppressWarnings("checkstyle:IllegalCatch")
private static void saveSnapshotsToFile(DatastoreSnapshotList snapshots, String fileName,
SettableFuture<RpcResult<Void>> returnFuture) {
try (FileOutputStream fos = new FileOutputStream(fileName)) {
SerializationUtils.serialize(snapshots, fos);
returnFuture.set(newSuccessfulResult());
LOG.info("Successfully backed up datastore to file {}", fileName);
} catch (IOException | RuntimeException e) {
onDatastoreBackupFailure(fileName, returnFuture, e);
}
}
示例6: buyCar
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Override
public Future<RpcResult<Void>> buyCar(BuyCarInput input) {
LOG.info("Routed RPC buyCar : generating notification for buying car [{}]", input);
SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
CarBoughtBuilder carBoughtBuilder = new CarBoughtBuilder();
carBoughtBuilder.setCarId(input.getCarId());
carBoughtBuilder.setPersonId(input.getPersonId());
notificationProvider.publish(carBoughtBuilder.build());
futureResult.set(RpcResultBuilder.<Void>success().build());
return futureResult;
}
示例7: testAwaitDone_Future
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
public void testAwaitDone_Future() {
final SettableFuture<Void> future = SettableFuture.create();
Object x = new Object() {
@Override protected void finalize() { future.set(null); }
};
x = null; // Hint to the JIT that x is unreachable
GcFinalization.awaitDone(future);
assertTrue(future.isDone());
assertFalse(future.isCancelled());
}
示例8: processUTXOMessage
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
protected void processUTXOMessage(UTXOsMessage m) {
SettableFuture<UTXOsMessage> future = null;
lock.lock();
try {
if (getutxoFutures != null)
future = getutxoFutures.pollFirst();
} finally {
lock.unlock();
}
if (future != null)
future.set(m);
}
示例9: batchOperationComplete
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Override
public void batchOperationComplete(FlowRuleBatchEvent event) {
final Long batchId = event.subject().batchId();
SettableFuture<CompletedBatchOperation> future
= pendingFutures.getIfPresent(batchId);
if (future != null) {
future.set(event.result());
pendingFutures.invalidate(batchId);
}
notifyDelegate(event);
}
示例10: processResponse
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Override
public void processResponse(Object readResponse, SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
if (ReadDataReply.isSerializedType(readResponse)) {
ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
} else {
returnFuture.setException(new ReadFailedException("Invalid response reading data for path " + getPath()));
}
}
示例11: completeExists
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
private void completeExists(final SettableFuture<Boolean> future, final Response<?, ?> response) {
LOG.debug("Exists request completed with {}", response);
if (response instanceof ExistsTransactionSuccess) {
future.set(((ExistsTransactionSuccess) response).getExists());
} else {
failFuture(future, response);
}
recordFinishedRequest(response);
}
示例12: testScanMessageAndNotifyMessageListener
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Test
public void testScanMessageAndNotifyMessageListener() throws Exception {
SettableFuture<ReleaseMessage> someListenerFuture = SettableFuture.create();
ReleaseMessageListener someListener = (message, channel) -> someListenerFuture.set(message);
releaseMessageScanner.addMessageListener(someListener);
String someMessage = "someMessage";
long someId = 100;
ReleaseMessage someReleaseMessage = assembleReleaseMessage(someId, someMessage);
when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(0L)).thenReturn(
Lists.newArrayList(someReleaseMessage));
ReleaseMessage someListenerMessage =
someListenerFuture.get(5000, TimeUnit.MILLISECONDS);
assertEquals(someMessage, someListenerMessage.getMessage());
assertEquals(someId, someListenerMessage.getId());
SettableFuture<ReleaseMessage> anotherListenerFuture = SettableFuture.create();
ReleaseMessageListener anotherListener = (message, channel) -> anotherListenerFuture.set(message);
releaseMessageScanner.addMessageListener(anotherListener);
String anotherMessage = "anotherMessage";
long anotherId = someId + 1;
ReleaseMessage anotherReleaseMessage = assembleReleaseMessage(anotherId, anotherMessage);
when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(someId)).thenReturn(
Lists.newArrayList(anotherReleaseMessage));
ReleaseMessage anotherListenerMessage =
anotherListenerFuture.get(5000, TimeUnit.MILLISECONDS);
assertEquals(anotherMessage, anotherListenerMessage.getMessage());
assertEquals(anotherId, anotherListenerMessage.getId());
}
示例13: testCautiousAdoptProvidePreResolveSuccess
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
/**
* Test the completion and callback invocation of {@link RedFutureHub}
* cautious union of provided and adopted futures that previously were successfully resolved
*/
@Test
public void testCautiousAdoptProvidePreResolveSuccess() throws Throwable {
AtomicBoolean reachedSuccessBlock = new AtomicBoolean(false);
AtomicBoolean reachedFailureBlock = new AtomicBoolean(false);
AtomicBoolean reachedFinallyBlock = new AtomicBoolean(false);
RedFutureHub hub = RedFuture.hub();
OpenRedFuture future1 = RedFuture.future();
OpenRedFuture future2 = RedFuture.future();
OpenRedFuture future3 = RedFuture.future();
OpenRedFuture future4 = hub.provideFuture();
OpenRedFutureOf<Object> futureOf1 = hub.provideFutureOf();
OpenRedFutureOf<Object> futureOf2 = hub.provideFutureOf();
SettableFuture<Object> settableFuture1 = SettableFuture.create();
SettableFuture<Object> settableFuture2 = SettableFuture.create();
SettableFuture<Object> settableFuture3 = SettableFuture.create();
SettableFuture<Object> settableFuture4 = SettableFuture.create();
SettableFuture<Object> settableFuture5 = SettableFuture.create();
List<RedFuture> redFutureCollection = new LinkedList<>();
redFutureCollection.add(future2);
redFutureCollection.add(futureOf1);
List<ListenableFuture> listenableFutureCollection = new LinkedList<>();
listenableFutureCollection.add(settableFuture1);
listenableFutureCollection.add(settableFuture2);
hub.adoptFuture(future1);
hub.adoptFutures(redFutureCollection);
hub.adoptFutures(future3, futureOf2);
hub.adoptListenableFuture(settableFuture3);
hub.adoptListenableFutures(listenableFutureCollection);
hub.adoptListenableFutures(settableFuture4, settableFuture5);
future1.resolve();
future2.resolve();
future3.resolve();
future4.resolve();
futureOf1.resolve(new Object());
futureOf2.resolve(new Object());
settableFuture1.set(new Object());
settableFuture2.set(new Object());
settableFuture3.set(new Object());
settableFuture4.set(new Object());
settableFuture5.set(new Object());
RedFuture union = hub.uniteCautiously();
union.addSuccessCallback(() -> reachedSuccessBlock.set(true));
union.addFailureCallback(throwable -> reachedFailureBlock.set(true));
union.addFinallyCallback(() -> reachedFinallyBlock.set(true));
Assert.assertTrue(reachedFinallyBlock.get());
Assert.assertTrue(reachedSuccessBlock.get());
Assert.assertFalse(reachedFailureBlock.get());
}
示例14: testCautiousAdoptPostResolveSuccess
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
/**
* Test the completion and callback invocation of {@link RedFutureHub}
* cautious union of adopted futures that later were successfully resolved
*/
@Test
public void testCautiousAdoptPostResolveSuccess() throws Throwable {
AtomicBoolean reachedSuccessBlock = new AtomicBoolean(false);
AtomicBoolean reachedFailureBlock = new AtomicBoolean(false);
AtomicBoolean reachedFinallyBlock = new AtomicBoolean(false);
RedFutureHub hub = RedFuture.hub();
OpenRedFuture future1 = RedFuture.future();
OpenRedFuture future2 = RedFuture.future();
OpenRedFuture future3 = RedFuture.future();
OpenRedFutureOf<Object> futureOf1 = hub.provideFutureOf();
OpenRedFutureOf<Object> futureOf2 = hub.provideFutureOf();
SettableFuture<Object> settableFuture1 = SettableFuture.create();
SettableFuture<Object> settableFuture2 = SettableFuture.create();
SettableFuture<Object> settableFuture3 = SettableFuture.create();
SettableFuture<Object> settableFuture4 = SettableFuture.create();
SettableFuture<Object> settableFuture5 = SettableFuture.create();
List<RedFuture> redFutureCollection = new LinkedList<>();
redFutureCollection.add(future2);
redFutureCollection.add(futureOf1);
List<ListenableFuture> listenableFutureCollection = new LinkedList<>();
listenableFutureCollection.add(settableFuture1);
listenableFutureCollection.add(settableFuture2);
hub.adoptFuture(future1);
hub.adoptFutures(redFutureCollection);
hub.adoptFutures(future3, futureOf2);
hub.adoptListenableFuture(settableFuture3);
hub.adoptListenableFutures(listenableFutureCollection);
hub.adoptListenableFutures(settableFuture4, settableFuture5);
RedFuture union = hub.uniteCautiously();
union.addSuccessCallback(() -> reachedSuccessBlock.set(true));
union.addFailureCallback(throwable -> reachedFailureBlock.set(true));
union.addFinallyCallback(() -> reachedFinallyBlock.set(true));
future1.resolve();
future2.resolve();
future3.resolve();
futureOf1.resolve(new Object());
futureOf2.resolve(new Object());
settableFuture1.set(new Object());
settableFuture2.set(new Object());
settableFuture3.set(new Object());
settableFuture4.set(new Object());
Assert.assertFalse(reachedFinallyBlock.get());
Assert.assertFalse(reachedSuccessBlock.get());
Assert.assertFalse(reachedFailureBlock.get());
settableFuture5.set(new Object());
Assert.assertTrue(reachedFinallyBlock.get());
Assert.assertTrue(reachedSuccessBlock.get());
Assert.assertFalse(reachedFailureBlock.get());
}
示例15: testFireOnceMajoritySuccess
import com.google.common.util.concurrent.SettableFuture; //导入方法依赖的package包/类
@Test
public void testFireOnceMajoritySuccess() {
SettableFuture<Boolean> f1 = SettableFuture.create();
SettableFuture<Boolean> f2 = SettableFuture.create();
SettableFuture<Boolean> f3 = SettableFuture.create();
List<ListenableFuture<Boolean>> responses = Lists.newArrayList(f1, f2, f3, Futures.<Boolean> immediateFuture(Boolean.FALSE));
ListenableFuture<Boolean> collector = majorityResponse(responses, Identity);
f1.set(Boolean.TRUE);
assertFalse(collector.isDone());
f2.set(Boolean.TRUE);
assertTrue(collector.isDone());
assertTrue(Futures.getUnchecked(collector));
}