本文整理汇总了Java中com.twitter.util.ExceptionalFunction类的典型用法代码示例。如果您正苦于以下问题:Java ExceptionalFunction类的具体用法?Java ExceptionalFunction怎么用?Java ExceptionalFunction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExceptionalFunction类属于com.twitter.util包,在下文中一共展示了ExceptionalFunction类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getManyToGetOneFunction
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
private ExceptionalFunction<TerrapinResponse, TerrapinSingleResponse> getManyToGetOneFunction(
final ByteBuffer key) {
return new ExceptionalFunction<TerrapinResponse, TerrapinSingleResponse>() {
@Override
public TerrapinSingleResponse applyE(TerrapinResponse response) throws Throwable {
TerrapinSingleResponse singleResponse = response.getResponseMap().get(key);
if (singleResponse == null) {
return new TerrapinSingleResponse();
} else {
if (singleResponse.isSetErrorCode()) {
throw new TerrapinGetException().setErrorCode(singleResponse.getErrorCode());
} else {
return singleResponse;
}
}
}
};
}
示例2: lockStream
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
/**
* Elective stream lock--readers are not required to acquire the lock before using the stream.
*/
synchronized Future<Void> lockStream() {
if (null == lockAcquireFuture) {
final Function0<DistributedLock> lockFunction = new ExceptionalFunction0<DistributedLock>() {
@Override
public DistributedLock applyE() throws IOException {
// Unfortunately this has a blocking call which we should not execute on the
// ZK completion thread
BKLogReadHandler.this.readLock = new ZKDistributedLock(
lockStateExecutor,
lockFactory,
readLockPath,
conf.getLockTimeoutMilliSeconds(),
statsLogger.scope("read_lock"));
LOG.info("acquiring readlock {} at {}", getLockClientId(), readLockPath);
return BKLogReadHandler.this.readLock;
}
};
lockAcquireFuture = ensureReadLockPathExist().flatMap(new ExceptionalFunction<Void, Future<Void>>() {
@Override
public Future<Void> applyE(Void in) throws Throwable {
return scheduler.apply(lockFunction).flatMap(new ExceptionalFunction<DistributedLock, Future<Void>>() {
@Override
public Future<Void> applyE(DistributedLock lock) throws IOException {
return acquireLockOnExecutorThread(lock);
}
});
}
});
}
return lockAcquireFuture;
}
示例3: processReaderOperation
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
<T> Future<T> processReaderOperation(final Function<BKLogReadHandler, Future<T>> func) {
initializeFuturePool(false);
return readerFuturePool.apply(new ExceptionalFunction0<BKLogReadHandler>() {
@Override
public BKLogReadHandler applyE() throws Throwable {
return getReadHandlerForListener(true);
}
}).flatMap(new ExceptionalFunction<BKLogReadHandler, Future<T>>() {
@Override
public Future<T> applyE(final BKLogReadHandler readHandler) throws Throwable {
return func.apply(readHandler);
}
});
}
示例4: testReaderLockCloseInAcquireCallback
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
@Test(timeout = 60000)
public void testReaderLockCloseInAcquireCallback() throws Exception {
final String name = runtime.getMethodName();
DistributedLogManager dlm = createNewDLM(conf, name);
BKAsyncLogWriter writer = (BKAsyncLogWriter)(dlm.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.closeAndComplete();
final CountDownLatch latch = new CountDownLatch(1);
Future<AsyncLogReader> futureReader1 = dlm.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
futureReader1.flatMap(new ExceptionalFunction<AsyncLogReader, Future<Void>>() {
@Override
public Future<Void> applyE(AsyncLogReader reader) throws IOException {
return reader.asyncClose().map(new AbstractFunction1<Void, Void>() {
@Override
public Void apply(Void result) {
latch.countDown();
return null;
}
});
}
});
latch.await();
dlm.close();
}
示例5: initialize
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
protected void initialize() throws Exception {
if (this.shardConfigFilePath != null) {
String fullFilePath = getClass().getResource("/" + shardConfigFilePath).getPath();
ConfigFileWatcher.defaultInstance().addWatch(
fullFilePath, new ExceptionalFunction<byte[], Void>() {
@Override
public synchronized Void applyE(byte[] bytes) throws Exception {
processConfigUpdate(bytes);
return null;
}
});
}
// Initialize the future pool we will use to make blocking calls to Redis.
// We size the future pool such that there is one thread for every available connection.
int futurePoolSize = configuration.getInt("BACKEND_CONNECTIONS_PER_SHARD") * getShards().size();
this.futurePool = new ExecutorServiceFuturePool(Executors.newFixedThreadPool(
futurePoolSize,
new ThreadFactoryBuilder().setDaemon(true).setNameFormat(
backendName + "FuturePool-%d").build()));
// Create a map of queueName -> aync semaphore to control dequeue concurrency.
// We configure the map to create entries on demand since queues can be created at any time.
final int dequeueConcurrencyPerQueue =
configuration.getInt("BACKEND_DEQUEUE_CONCURRENCY_PER_QUEUE_PER_SHARD") * getShards()
.size();
// We set maxWaiters on the async semaphore to the max concurrency on the server as an
// additional safety measure.
final int maxWaiters = configuration.getInt("MAX_CONCURRENT_REQUESTS");
this.dequeueSemaphoreMap = CacheBuilder.newBuilder().build(
new CacheLoader<String, AsyncSemaphore>() {
@Override
public AsyncSemaphore load(String queueName) throws Exception {
AsyncSemaphore asyncSemaphore =
new AsyncSemaphore(dequeueConcurrencyPerQueue, maxWaiters);
Stats.setGauge("dequeue-semaphore-waiters-" + queueName, asyncSemaphore.numWaiters());
return asyncSemaphore;
}
});
}
示例6: initialize
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
public void initialize() throws Exception {
// Check if use of serverset is enabled, and if so, register a change monitor so we
// can find out how many PinLater servers are active. We use this to compute the
// per-server rate limit.
if (pinlaterServerSetEnabled) {
MorePreconditions.checkNotBlank(pinlaterServerSetPath);
LOG.info("Monitoring pinlater serverset: {}", pinlaterServerSetPath);
String fullServerSetPath = getClass().getResource("/" + pinlaterServerSetPath).getPath();
ServerSet serverSet = new ConfigFileServerSet(fullServerSetPath);
serverSet.monitor(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
@Override
public void onChange(ImmutableSet<ServiceInstance> hostSet) {
int oldSize = numPinLaterServers.get();
int newSize = hostSet.size();
if (newSize == 0) {
LOG.error("PinLater serverset is empty, ignoring and keeping old size: {}", oldSize);
return;
}
if (oldSize == newSize) {
LOG.info("PinLater serverset update, size unchanged: {}", oldSize);
return;
}
LOG.info("PinLater serverset update, old size: {}, new size: {}", oldSize, newSize);
numPinLaterServers.set(newSize);
rebuild();
}
});
} else {
LOG.info("PinLater server set is disabled; rate limits will be applied per server.");
}
if (queueConfigFilePath == null || queueConfigFilePath.isEmpty()) {
LOG.info("Queue config zookeeper path not specified, using defaults.");
return;
}
LOG.info("Registering watch on queue config: {}", queueConfigFilePath);
String fullQueueConfigFilePath = getClass().getResource("/" + queueConfigFilePath).getPath();
ConfigFileWatcher.defaultInstance().addWatch(
fullQueueConfigFilePath, new ExceptionalFunction<byte[], Void>() {
@Override
public Void applyE(byte[] bytes) throws Exception {
QueueConfigSchema queueConfigSchema = QueueConfigSchema.load(bytes);
LOG.info("Queue config update, new value: {}", queueConfigSchema);
queueConfigSchemaRef.set(queueConfigSchema);
rebuild();
return null;
}
});
}
示例7: get
import com.twitter.util.ExceptionalFunction; //导入依赖的package包/类
@Override
public Future<TerrapinSingleResponse> get(final TerrapinGetRequest request) {
final long startTimeMillis = System.currentTimeMillis();
if (request.getClusterList().isEmpty()) {
return Future.exception(new TerrapinGetException("Cluster list is empty", TerrapinGetErrorCode.INVALID_REQUEST));
}
ReplicatedTerrapinClient terrapinClient = getReplicatedTerrapinClient(request.getClusterList());
if (terrapinClient == null) {
return Future.exception(new TerrapinGetException(
"Clusters [" + Joiner.on(", ").join(request.getClusterList()) + "] not found.",
TerrapinGetErrorCode.CLUSTER_NOT_FOUND));
}
RequestOptions options;
if (request.isSetOptions()) {
options = request.getOptions();
} else {
options = new RequestOptions();
}
try {
return terrapinClient.getMany(request.getFileSet(),
Sets.newHashSet(ByteBuffer.wrap(request.getKey())), options).map(
new ExceptionalFunction<TerrapinResponse, TerrapinSingleResponse>() {
@Override
public TerrapinSingleResponse applyE(TerrapinResponse response)
throws TerrapinGetException {
ByteBuffer keyBuf = ByteBuffer.wrap(request.getKey());
if (response.getResponseMap().containsKey(keyBuf)) {
TerrapinSingleResponse returnResponse = response.getResponseMap().get(keyBuf);
if (returnResponse.isSetErrorCode()) {
throw new TerrapinGetException("Read failed.", returnResponse.getErrorCode());
} else {
Stats.addMetric(request.getFileSet() + "-value-size", returnResponse.getValue().length);
Stats.addMetric("value-size", returnResponse.getValue().length);
return returnResponse;
}
} else {
return new TerrapinSingleResponse();
}
}
}).rescue(new Function<Throwable, Future<TerrapinSingleResponse>>() {
@Override
public Future<TerrapinSingleResponse> apply(Throwable t) {
return getExceptionFuture(t);
}
}).ensure(new Function0<BoxedUnit>() {
@Override
public BoxedUnit apply() {
int timeMillis = (int)(System.currentTimeMillis() - startTimeMillis);
Stats.addMetric(request.getFileSet() + "-lookup-latency-ms", timeMillis);
Stats.addMetric("lookup-latency-ms", timeMillis);
return BoxedUnit.UNIT;
}
});
} catch (Exception e) {
return getExceptionFuture(e);
}
}