本文整理汇总了Java中com.google.common.util.concurrent.CheckedFuture类的典型用法代码示例。如果您正苦于以下问题:Java CheckedFuture类的具体用法?Java CheckedFuture怎么用?Java CheckedFuture使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CheckedFuture类属于com.google.common.util.concurrent包,在下文中一共展示了CheckedFuture类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listenForFailure
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
private CheckedFuture<Void, TransactionCommitFailedException> listenForFailure(
final WriteTransaction tx, final CheckedFuture<Void, TransactionCommitFailedException> future) {
Futures.addCallback(future, new FutureCallback<Void>() {
@Override
public void onFailure(final Throwable t) {
failTransactionChain(tx,t);
}
@Override
public void onSuccess(final Void result) {
// Intentionally NOOP
}
});
return future;
}
示例2: submit
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Override
public CheckedFuture<Void, TransactionCommitFailedException> submit(
final DOMDataWriteTransaction transaction, final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
checkNotFailed();
checkNotClosed();
final CheckedFuture<Void, TransactionCommitFailedException> ret = broker.submit(transaction, cohorts);
COUNTER_UPDATER.incrementAndGet(this);
Futures.addCallback(ret, new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
transactionCompleted();
}
@Override
public void onFailure(final Throwable t) {
transactionFailed(transaction, t);
}
});
return ret;
}
示例3: readFromGreetingRegistry
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
private String readFromGreetingRegistry(HelloWorldInput input) {
String result = "Hello " + input.getName();
ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
InstanceIdentifier<GreetingRegistryEntry> iid = toInstanceIdentifier(input);
CheckedFuture<Optional<GreetingRegistryEntry>, ReadFailedException> future =
transaction.read(LogicalDatastoreType.CONFIGURATION, iid);
Optional<GreetingRegistryEntry> optional = Optional.absent();
try {
optional = future.checkedGet();
} catch (ReadFailedException e) {
LOG.warn("Reading greeting failed:",e);
}
if(optional.isPresent()) {
result = optional.get().getGreeting();
}
return result;
}
示例4: testCheckedGetThrowsApplicationExceptionOnCancellation
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
/**
* Tests that the {@link CheckedFuture#checkedGet()} method throws the correct
* type of cancellation exception when it is cancelled.
*/
public void testCheckedGetThrowsApplicationExceptionOnCancellation() {
final CheckedFuture<Boolean, ?> future =
createCheckedFuture(Boolean.TRUE, null, latch);
assertFalse(future.isDone());
assertFalse(future.isCancelled());
new Thread(new Runnable() {
@Override
public void run() {
future.cancel(true);
}
}).start();
try {
future.checkedGet();
fail("RPC Should have been cancelled.");
} catch (Exception e) {
checkCancelledException(e);
}
assertTrue(future.isDone());
assertTrue(future.isCancelled());
}
示例5: executeList
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Override
public void executeList() {
final LogicalDatastoreType dsType = getDataStoreType();
final org.opendaylight.yangtools.yang.common.QName olId = QName.create(OuterList.QNAME, "id");
final YangInstanceIdentifier pid =
YangInstanceIdentifier.builder().node(TestExec.QNAME).node(OuterList.QNAME).build();
try (DOMDataReadOnlyTransaction tx = domDataBroker.newReadOnlyTransaction()) {
for (int l = 0; l < outerListElem; l++) {
YangInstanceIdentifier yid = pid.node(new NodeIdentifierWithPredicates(OuterList.QNAME, olId, l));
Optional<NormalizedNode<?,?>> optionalDataObject;
CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> submitFuture = tx.read(dsType, yid);
try {
optionalDataObject = submitFuture.checkedGet();
if (optionalDataObject != null && optionalDataObject.isPresent()) {
txOk++;
}
} catch (final ReadFailedException e) {
LOG.warn("failed to ....", e);
txError++;
}
}
}
}
示例6: submit
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Override
public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
Preconditions.checkState(!closed, "Transaction %s is already closed", identifier);
final Set<DOMStoreWriteTransaction> txns = ImmutableSet.copyOf(idToTransaction.values());
final List<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
for (DOMStoreWriteTransaction tx : txns) {
cohorts.add(tx.ready());
}
try {
return Futures.immediateCheckedFuture(new CommitCoordinationTask(this, cohorts, null).call());
} catch (TransactionCommitFailedException e) {
return Futures.immediateFailedCheckedFuture(e);
}
}
示例7: testInvokeRpcWithNullInput
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
/**
* This test method invokes and executes the remote rpc.
*/
@Test
public void testInvokeRpcWithNullInput() throws Exception {
final ContainerNode rpcOutput = makeRPCOutput("bar");
final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
@SuppressWarnings({"unchecked", "rawtypes"})
final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
(ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
assertEquals(rpcOutput, result.getResult());
}
示例8: invokeRpc
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
final AbstractDOMRpcRoutingTableEntry entry = rpcs.get(type);
if (entry == null) {
return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(
new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
}
return entry.invokeRpc(input);
}
示例9: invokeRpc
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Nonnull
@Override
public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull DOMRpcIdentifier rpc, @Nullable NormalizedNode<?, ?> input) {
LOG.debug("get-singleton-constant invoked, current value: {}", constant);
final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
.withNodeIdentifier(new NodeIdentifier(CONSTANT))
.withValue(constant)
.build();
final ContainerNode result = ImmutableContainerNodeBuilder.create()
.withNodeIdentifier(new NodeIdentifier(OUTPUT))
.withChild(value)
.build();
return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
}
示例10: read
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Override
public CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
LOG.debug("Tx: {} Read: {}", getIdentifier(), path);
checkNotNull(path, "Path must not be null.");
final DataTreeSnapshot snapshot = stableSnapshot;
if (snapshot == null) {
return Futures.immediateFailedCheckedFuture(new ReadFailedException("Transaction is closed"));
}
try {
return Futures.immediateCheckedFuture(snapshot.readNode(path));
} catch (Exception e) {
LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read failed",e));
}
}
示例11: executeRead
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
private <T> CheckedFuture<T, ReadFailedException> executeRead(final String shardName,
final AbstractRead<T> readCmd) {
Preconditions.checkState(type != TransactionType.WRITE_ONLY,
"Reads from write-only transactions are not allowed");
LOG.debug("Tx {} {} {}", getIdentifier(), readCmd.getClass().getSimpleName(), readCmd.getPath());
final SettableFuture<T> proxyFuture = SettableFuture.create();
TransactionContextWrapper contextWrapper = getContextWrapper(shardName);
contextWrapper.maybeExecuteTransactionOperation(new TransactionOperation() {
@Override
public void invoke(final TransactionContext transactionContext) {
transactionContext.executeRead(readCmd, proxyFuture);
}
});
return MappingCheckedFuture.create(proxyFuture, ReadFailedException.MAPPER);
}
示例12: getSource
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Override
public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) {
LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());
Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier);
final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
@Override
public void onComplete(Throwable throwable,
YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
if (yangTextSchemaSourceSerializationProxy != null) {
res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
}
if (throwable != null) {
res.setException(throwable);
}
}
}, executionContext);
return Futures.makeChecked(res, MAPPER);
}
示例13: testInvokeRpcWithRemoteFailedFuture
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
/**
* This test method invokes and executes the remote rpc.
*/
@Test(expected = DOMRpcException.class)
public void testInvokeRpcWithRemoteFailedFuture() throws Exception {
final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
@SuppressWarnings({"unchecked", "rawtypes"})
final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
(ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new RemoteDOMRpcException(
"Test Exception", null)));
final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
}
示例14: testBasicProducer
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
/**
* A simple unbound producer. It write some basic things into the data store based on the
* test model.
* @throws DOMDataTreeProducerException
* @throws TransactionCommitFailedException
*/
@Test
public final void testBasicProducer() throws DOMDataTreeProducerException, TransactionCommitFailedException {
// Create a producer. It is an AutoCloseable resource, hence the try-with pattern
try (final DOMDataTreeProducer prod = service().createProducer(Collections.singleton(UNORDERED_CONTAINER_TREE))) {
assertNotNull(prod);
final DOMDataWriteTransaction tx = prod.createTransaction(true);
assertNotNull(tx);
tx.put(LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID, ImmutableContainerNodeBuilder.create().build());
final CheckedFuture<Void, TransactionCommitFailedException> f = tx.submit();
assertNotNull(f);
f.checkedGet();
}
}
示例15: submit
import com.google.common.util.concurrent.CheckedFuture; //导入依赖的package包/类
@Override
public CheckedFuture<Void, TransactionCommitFailedException> submit() {
final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
checkRunning(impl);
final Collection<T> txns = getSubtransactions();
final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
// FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
for (final T txn : txns) {
cohorts.add(txn.ready());
}
final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
FUTURE_UPDATER.lazySet(this, ret);
return ret;
}