本文整理汇总了Java中com.twitter.util.Try类的典型用法代码示例。如果您正苦于以下问题:Java Try类的具体用法?Java Try怎么用?Java Try使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Try类属于com.twitter.util包,在下文中一共展示了Try类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendSpans
import com.twitter.util.Try; //导入依赖的package包/类
@Override public void sendSpans(final List<byte[]> spans, final Callback callback) {
Trace.letClear(new AbstractFunction0<Void>() {
@Override public Void apply() {
try {
if (closeCalled) throw new IllegalStateException("closed");
client.apply(makeRequest(spans)).respond(new AbstractFunction1<Try<Rep>, BoxedUnit>() {
@Override public BoxedUnit apply(Try<Rep> result) {
if (result.isReturn()) {
callback.onComplete();
} else {
callback.onError(result.throwable());
}
return BoxedUnit.UNIT;
}
});
} catch (Throwable e) {
callback.onError(e);
if (e instanceof Error) throw (Error) e;
}
return null;
}
});
}
示例2: asyncWriteBulk
import com.twitter.util.Try; //导入依赖的package包/类
private List<Future<DLSN>> asyncWriteBulk(List<LogRecord> records) {
final ArrayList<Future<DLSN>> results = new ArrayList<Future<DLSN>>(records.size());
Iterator<LogRecord> iterator = records.iterator();
while (iterator.hasNext()) {
LogRecord record = iterator.next();
Future<DLSN> future = asyncWrite(record, !iterator.hasNext());
results.add(future);
// Abort early if an individual write has already failed.
Option<Try<DLSN>> result = future.poll();
if (result.isDefined() && result.get().isThrow()) {
break;
}
}
if (records.size() > results.size()) {
appendCancelledFutures(results, records.size() - results.size());
}
return results;
}
示例3: testGetEmptyClusters
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testGetEmptyClusters() {
ByteBuffer key = ByteBuffer.wrap(KEY);
TerrapinGetRequest request = prepareGetRequest();
RequestOptions options = new RequestOptions();
options.setSelectionPolicy(SelectionPolicy.PRIMARY_FIRST);
request.setOptions(options);
request.setClusterList(ImmutableList.copyOf(new String[]{}));
TerrapinResponse response = new TerrapinResponse().setResponseMap(ImmutableMap.of(
key, new TerrapinSingleResponse().setValue(ByteBuffer.wrap("value".getBytes()))));
when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn(
Future.value(response));
when(mockClient2.getManyNoRetries(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn(
Future.value(response));
Try<TerrapinSingleResponse> singleResponseTry = serviceIface.get(request).get(Duration.forever());
assertTrue(singleResponseTry.isThrow());
assertEquals(TerrapinGetErrorCode.INVALID_REQUEST,
((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode());
}
示例4: testMultiGetEmptyClusters
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testMultiGetEmptyClusters() {
TerrapinMultiGetRequest request = prepareMultiGetRequest();
RequestOptions options = new RequestOptions();
options.setSelectionPolicy(SelectionPolicy.PRIMARY_FIRST);
request.setOptions(options);
request.setClusterList(ImmutableList.copyOf(new String[]{}));
TerrapinResponse response = new TerrapinResponse().setResponseMap(ImmutableMap.of(
ByteBuffer.wrap("key1".getBytes()),
new TerrapinSingleResponse().setValue("value1".getBytes()),
ByteBuffer.wrap("key2".getBytes()),
new TerrapinSingleResponse().setErrorCode(TerrapinGetErrorCode.READ_ERROR)));
Set<ByteBuffer> keys = Sets.newHashSet(ByteBuffer.wrap("key1".getBytes()),
ByteBuffer.wrap("key2".getBytes()),
ByteBuffer.wrap("key3".getBytes()));
when(mockClient1.getMany(eq(FILESET), eq(keys))).thenReturn(Future.value(response));
when(mockClient2.getManyNoRetries(eq(FILESET), eq(keys))).thenReturn(Future.value(response));
Try<TerrapinResponse> returnResponseTry = serviceIface.multiGet(request).get(Duration.forever());
assertTrue(returnResponseTry.isThrow());
assertEquals(TerrapinGetErrorCode.INVALID_REQUEST,
((TerrapinGetException)((Throw)returnResponseTry).e()).getErrorCode());
}
示例5: testMultiGetError
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testMultiGetError() {
TerrapinMultiGetRequest request = prepareMultiGetRequest();
Set<ByteBuffer> keys = Sets.newHashSet(ByteBuffer.wrap("key1".getBytes()),
ByteBuffer.wrap("key2".getBytes()),
ByteBuffer.wrap("key3".getBytes()));
when (mockClient1.getMany(eq(FILESET), eq(keys))).thenReturn(
Future.<TerrapinResponse>exception(new TerrapinGetException("Failed",
TerrapinGetErrorCode.FILE_SET_NOT_FOUND)));
Try<TerrapinResponse> responseTry = serviceIface.multiGet(request).get(Duration.forever());
assertTrue(responseTry.isThrow());
assertEquals(TerrapinGetErrorCode.FILE_SET_NOT_FOUND,
((TerrapinGetException)((Throw)responseTry).e()).getErrorCode());
when(mockClient1.getMany(eq(FILESET), eq(keys))).thenThrow(
new RuntimeException(new NullPointerException()));
responseTry = serviceIface.multiGet(request).get(Duration.forever());
assertTrue(responseTry.isThrow());
assertEquals(TerrapinGetErrorCode.OTHER,
((TerrapinGetException)((Throw)responseTry).e()).getErrorCode());
}
示例6: testGetErrorMultipleResources
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testGetErrorMultipleResources() throws Exception {
TerrapinInternalGetRequest request = new TerrapinInternalGetRequest();
MultiKey multiKey1 = new MultiKey().setResource("resource1").setPartition("1");
multiKey1.addToKey(ByteBuffer.wrap("k1".getBytes()));
MultiKey multiKey2 = new MultiKey().setResource("resource2").setPartition("1");
multiKey2.addToKey(ByteBuffer.wrap("k2".getBytes()));
request.addToKeyList(multiKey1);
request.addToKeyList(multiKey2);
Reader mockReader = mock(Reader.class);
when(mockResourcePartitionMap.getReader(eq("resource1"), eq("1"))).thenReturn(mockReader);
Try<TerrapinResponse> responseTry = serverImpl.get(request).get(Duration.forever());
TerrapinGetException e = (TerrapinGetException)((Throw)responseTry).e();
assertEquals(TerrapinGetErrorCode.INVALID_REQUEST, e.getErrorCode());
}
示例7: satisfyPromiseAsync
import com.twitter.util.Try; //导入依赖的package包/类
<T> void satisfyPromiseAsync(final Promise<T> promise, final Try<T> result) {
scheduler.submit(new SafeRunnable() {
@Override
public void safeRun() {
promise.update(result);
}
});
}
示例8: isDefiniteFailure
import com.twitter.util.Try; //导入依赖的package包/类
private boolean isDefiniteFailure(Try<DLSN> result) {
boolean def = false;
try {
result.get();
} catch (Exception ex) {
if (ex instanceof OwnershipAcquireFailedException ||
ex instanceof AlreadyClosedException ||
ex instanceof LockingException) {
def = true;
}
}
return def;
}
示例9: asTryList
import com.twitter.util.Try; //导入依赖的package包/类
private Future<List<Try<DLSN>>> asTryList(Future<List<Future<DLSN>>> futureList) {
return futureList.flatMap(new AbstractFunction1<List<Future<DLSN>>, Future<List<Try<DLSN>>>>() {
@Override
public Future<List<Try<DLSN>>> apply(List<Future<DLSN>> results) {
return Future$.MODULE$.collectToTry(results);
}
});
}
示例10: setResponse
import com.twitter.util.Try; //导入依赖的package包/类
protected void setResponse(Response response) {
Return<Response> responseTry = new Return(response);
boolean isEmpty = result.updateIfEmpty(responseTry);
if (!isEmpty) {
Option<Try<Response>> resultTry = result.poll();
logger.error("Result set multiple times. Value='{}', New='{}'", resultTry, responseTry);
}
}
示例11: dequeueJobs
import com.twitter.util.Try; //导入依赖的package包/类
public Future<PinLaterDequeueResponse> dequeueJobs(final String source,
final PinLaterDequeueRequest request) {
Future<PinLaterDequeueResponse> dequeueFuture;
try {
dequeueFuture = dequeueSemaphoreMap.get(request.getQueueName()).acquire().flatMap(
new Function<Permit, Future<PinLaterDequeueResponse>>() {
@Override
public Future<PinLaterDequeueResponse> apply(final Permit permit) {
return futurePool.apply(new ExceptionalFunction0<PinLaterDequeueResponse>() {
@Override
public PinLaterDequeueResponse applyE() throws Throwable {
return dequeueJobsImpl(source, request, numAutoRetries);
}
}).respond(new Function<Try<PinLaterDequeueResponse>, BoxedUnit>() {
@Override
public BoxedUnit apply(Try<PinLaterDequeueResponse> responseTry) {
permit.release();
return BoxedUnit.UNIT;
}
});
}
});
} catch (ExecutionException e) {
// The dequeueSemaphoreMap's get() can in theory throw an ExecutionException, but we
// never expect it in practice since our load method is simply new'ing up an AsyncSemaphore.
dequeueFuture = Future.exception(e);
}
// Dequeue requests can contain ack requests as payloads. If so, we execute both in parallel.
Future<Void> ackFuture = request.isSetJobAckRequest()
? ackDequeuedJobsImpl(request.getJobAckRequest()) : Future.Void();
return dequeueFuture.join(ackFuture).map(
new Function<Tuple2<PinLaterDequeueResponse, Void>, PinLaterDequeueResponse>() {
@Override
public PinLaterDequeueResponse apply(Tuple2<PinLaterDequeueResponse, Void> tuple) {
return tuple._1();
}
});
}
示例12: issueEnqueueRequests
import com.twitter.util.Try; //导入依赖的package包/类
private void issueEnqueueRequests(PinLater.ServiceIface iface) throws InterruptedException {
Preconditions.checkNotNull(queueName, "Queue was not specified.");
final AtomicLong queriesIssued = new AtomicLong(0);
final Semaphore permits = new Semaphore(concurrency);
while (numQueries == -1 || queriesIssued.get() < numQueries) {
final PinLaterEnqueueRequest request = new PinLaterEnqueueRequest();
request.setQueueName(queueName);
for (int i = 0; i < batchSize; i++) {
PinLaterJob job = new PinLaterJob(ByteBuffer.wrap(
new String("task_" + random.nextInt(Integer.MAX_VALUE)).getBytes()));
job.setPriority(priority);
request.addToJobs(job);
}
final long startTimeNanos = System.nanoTime();
queriesIssued.incrementAndGet();
permits.acquire();
iface.enqueueJobs(REQUEST_CONTEXT, request).respond(
new Function<Try<PinLaterEnqueueResponse>, BoxedUnit>() {
@Override
public BoxedUnit apply(Try<PinLaterEnqueueResponse> responseTry) {
permits.release();
statsLogger.requestComplete(
Duration.fromNanoseconds(System.nanoTime() - startTimeNanos));
if (responseTry.isThrow()) {
LOG.info("Exception for request: " + request + " : " + ((Throw) responseTry).e());
}
return BoxedUnit.UNIT;
}
});
}
permits.acquire(concurrency);
LOG.info("Enqueue queries issued: " + queriesIssued);
}
示例13: testGetClusterNotFound
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testGetClusterNotFound() {
TerrapinGetRequest request = prepareGetRequest().setClusterList(ImmutableList.of(
"random-cluster"));
Try<TerrapinSingleResponse> singleResponseTry =
serviceIface.get(request).get(Duration.forever());
assertTrue(singleResponseTry.isThrow());
assertEquals(TerrapinGetErrorCode.CLUSTER_NOT_FOUND,
((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode());
}
示例14: testGetError
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testGetError() {
// Test the case where We get back an error set through an error code set in
// TerrapinSingleResponse.
ByteBuffer key = ByteBuffer.wrap(KEY);
TerrapinGetRequest request = prepareGetRequest();
TerrapinResponse response = new TerrapinResponse().setResponseMap(ImmutableMap.of(
key, new TerrapinSingleResponse().setErrorCode(TerrapinGetErrorCode.OTHER)));
when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn(
Future.value(response));
Try<TerrapinSingleResponse> singleResponseTry = serviceIface.get(request).get(
Duration.forever());
assertTrue(singleResponseTry.isThrow());
assertEquals(TerrapinGetErrorCode.OTHER,
((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode());
// Test the case where the call to the client library itself bails out due to a
// legit error.
when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn(
Future.<TerrapinResponse>exception(new TerrapinGetException("Failed.",
TerrapinGetErrorCode.FILE_SET_NOT_FOUND)));
singleResponseTry = serviceIface.get(request).get(Duration.forever());
assertTrue(singleResponseTry.isThrow());
assertEquals(TerrapinGetErrorCode.FILE_SET_NOT_FOUND,
((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode());
// Test the case where the call to the client library bails out due to a runtime
// exception.
when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenThrow(
new RuntimeException(new NullPointerException()));
singleResponseTry = serviceIface.get(request).get(Duration.forever());
assertTrue(singleResponseTry.isThrow());
assertEquals(TerrapinGetErrorCode.OTHER,
((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode());
}
示例15: testMultiGetClusterNotFound
import com.twitter.util.Try; //导入依赖的package包/类
@Test
public void testMultiGetClusterNotFound() {
TerrapinMultiGetRequest request = prepareMultiGetRequest().setClusterList(ImmutableList.of(
"random-cluster"));
Try<TerrapinResponse> responseTry =
serviceIface.multiGet(request).get(Duration.forever());
assertTrue(responseTry.isThrow());
assertEquals(TerrapinGetErrorCode.CLUSTER_NOT_FOUND,
((TerrapinGetException)((Throw)responseTry).e()).getErrorCode());
}