本文整理汇总了Java中com.twitter.common.quantity.Time类的典型用法代码示例。如果您正苦于以下问题:Java Time类的具体用法?Java Time怎么用?Java Time使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Time类属于com.twitter.common.quantity包,在下文中一共展示了Time类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: apply
import com.twitter.common.quantity.Time; //导入依赖的package包/类
@Override
public TServer apply(ServerSetup setup) throws TTransportException {
TNonblockingServerSocket socket = setup.getSocketTimeout() == null
? new TNonblockingServerSocket(new InetSocketAddress("0.0.0.0", setup.getPort()))
: new TNonblockingServerSocket(new InetSocketAddress("0.0.0.0", setup.getPort()),
setup.getSocketTimeout().as(Time.MILLISECONDS));
setup.setSocket(getServerSocketFor(socket));
THsHaServer.Args options = new THsHaServer.Args(socket);
options.processor(setup.getProcessor());
if (setup.getNumThreads() > 0) {
options.workerThreads(setup.getNumThreads());
}
// default queue size to num threads: max response time becomes double avg service time
final BlockingQueue<Runnable> queue =
new ArrayBlockingQueue<Runnable>(setup.getQueueSize() > 0 ? setup.getQueueSize()
: options.getWorkerThreads());
final ThreadPoolExecutor invoker = new ThreadPoolExecutor(options.getWorkerThreads(),
options.getWorkerThreads(), options.getStopTimeoutVal(), options.getStopTimeoutUnit(), queue);
final String serverName = (setup.getName() != null ? setup.getName() : "no-name");
return new THsHaServer(options);
}
示例2: invoke
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* Convenience method for invoking the method and shunting the capture into the callback if
* the call is asynchronous.
*
* @param method The method being invoked.
* @param args The arguments to call {@code method} with.
* @param callback The callback to use if the method is asynchronous.
* @param capture The result capture to notify of the call result.
* @param connectTimeoutOverride Optional override for the default connection timeout.
* @return The return value from invoking the method.
* @throws Throwable Exception, as prescribed by the method's contract.
*/
protected final Object invoke(Method method, Object[] args,
AsyncMethodCallback callback, final ResultCapture capture,
Amount<Long, Time> connectTimeoutOverride) throws Throwable {
// Swap the wrapped callback out for ours.
if (callback != null) {
callback = new WrappedMethodCallback(callback, capture);
}
try {
Object result = decoratedCaller.call(method, args, callback, connectTimeoutOverride);
if (callback == null && capture != null) capture.success();
return result;
} catch (Throwable t) {
// We allow this one to go to both sync and async captures.
if (callback != null) {
callback.onError((Exception)t);
return null;
} else {
if (capture != null) capture.fail(t);
throw t;
}
}
}
示例3: getIfaceThrift
import com.twitter.common.quantity.Time; //导入依赖的package包/类
protected Thrift<HelloClient> getIfaceThrift(DynamicHostSet backends) {
Config thriftConfig = Config.builder()
.withRequestTimeout(Amount.of(0L, Time.SECONDS))
.withRetries(3)
.disableStats()
.withDebug(true)
.retryOn(ImmutableSet.<Class<? extends Exception>>builder()
.add(ThriftException.class)
.add(IOException.class)
.add(ConnectFailedException.class)
.add(TTimeoutException.class)
.add(ResourceExhaustedException.class)
.add(TTransportException.class).build())
.create();
try {
return ThriftFactory.create(HelloClient.class)
.useFramedTransport(true)
.withMaxConnectionsPerEndpoint(5)
.withThriftConfig(thriftConfig)
.build(backends);
} catch (Exception e) {
return null;
}
}
示例4: testTwoServer
import com.twitter.common.quantity.Time; //导入依赖的package包/类
@Test
public void testTwoServer() throws TException, InterruptedException {
ThriftServer server1 = mockServer(7911).awaitForAlive();
ThriftServer server2 = mockServer(7912).awaitForAlive();
Thrift<HelloClient> thrift = getIfaceThrift();
Hello.Iface iface = thrift.create();
ConcurrentMap<String, AtomicLong> hits = Maps.newConcurrentMap();
for (int i = 0; i < 10; i++) {
String port = iface.hi();
assertNotNull(port);
hits.putIfAbsent(port, new AtomicLong(0));
hits.get(port).incrementAndGet();
}
assertHit(hits, "7911");
assertHit(hits, "7912");
server1.awaitShutdown(Amount.of(5L, Time.SECONDS));
server2.awaitShutdown(Amount.of(5L, Time.SECONDS));
}
示例5: of
import com.twitter.common.quantity.Time; //导入依赖的package包/类
public static DLZkServerSet of(URI uri,
int zkSessionTimeoutMs) {
// Create zookeeper and server set
String zkPath = uri.getPath() + "/" + ZNODE_WRITE_PROXY;
Iterable<InetSocketAddress> zkAddresses = getZkAddresses(uri);
ZooKeeperClient zkClient =
new ZooKeeperClient(Amount.of(zkSessionTimeoutMs, Time.MILLISECONDS), zkAddresses);
ServerSet serverSet = ServerSets.create(zkClient, ZooDefs.Ids.OPEN_ACL_UNSAFE, zkPath);
return new DLZkServerSet(zkClient, serverSet);
}
示例6: getClient
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* Return Thrift service client interface.
*
* @return OperationsThriftService.Iface
*/
public OperationsThriftService.Iface getClient() {
return thrift.builder()
.disableStats()
.withRequestTimeout(Amount.of(socketTimeout, Time.SECONDS))
.create();
}
示例7: start
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* Start neighbor connection if it not started yet.
*/
public synchronized void start() {
if (!started) {
executor = Executors.newFixedThreadPool(maxNumberConnection);
messageQueue = new LinkedBlockingQueue<>(messageQueueLength);
workers = new LinkedList<>();
clientFactory = ThriftFactory.create(OperationsThriftService.Iface.class);
InetSocketAddress address = new InetSocketAddress(
connectionInfo.getThriftHost().toString(), connectionInfo.getThriftPort()
);
Set<InetSocketAddress> backends = new HashSet<>();
backends.add(address);
thrift = clientFactory.withMaxConnectionsPerEndpoint(maxNumberConnection)
.withSocketTimeout(Amount.of(socketTimeout, Time.SECONDS))
.withClientFactory(new Function<TTransport, OperationsThriftService.Iface>() {
@Override
public Iface apply(TTransport transport) {
TProtocol protocol = new TBinaryProtocol(transport);
TMultiplexedProtocol mprotocol = new TMultiplexedProtocol(
protocol, KaaThriftService.OPERATIONS_SERVICE.getServiceName()
);
return new OperationsThriftService.Client(mprotocol);
}
}).build(backends);
for (int i = 0; i < maxNumberConnection; i++) {
EventWorker worker = new EventWorker(template);
workers.add(executor.submit(worker));
}
started = true;
} else {
LOG.debug("Neighbor Connection {} is already started", getId());
}
}
示例8: create
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* create a client to a single endpoint
*
* @param endpoint
* @return
*/
public T create(HostAndPort endpoint, Amount<Long, Time> requestTimeout) {
T client = clientBuffer.get(endpoint);
if (client == null) {
synchronized (Thrift.class) {
client = clientBuffer.get(endpoint);
if (client == null) {
SingleBackend<HostAndPort> singleBackend = new SingleBackend<>(endpoint);
client = createClient(singleBackend, defaultConfig, null, requestTimeout);
clientBuffer.put(endpoint, client);
}
}
}
return client;
}
示例9: createClient
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* create a thrift client for calling
*
* @param loadBalancer
* @param config
* @param collectionCallback, collection callback, will apply this callback when get a collection from pool success.
* @return
*/
private T createClient(LoadBalancer<HostAndPort> loadBalancer, Config config, Closure<ThriftConnection<T>> collectionCallback,Amount<Long, Time> requestTimeout) {
// lease/call/[invalidate]/release
boolean debug = config.isDebug();
Caller decorated = new ThriftCaller<>(connectionPool, loadBalancer, clientFactory, collectionCallback);
// [retry]
if (config.getMaxRetries() > 0) {
decorated = new RetryingCaller(decorated, config.getMaxRetries(), config.getRetryableExceptions(), debug);
}
// [deadline]
if (requestTimeout!=null && requestTimeout.getValue() > 0) {
decorated = new DeadlineCaller(decorated, executorService, requestTimeout);
}
final Caller caller = decorated;
final MethodInterceptor invocationHandler = new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
AsyncMethodCallback callback = null;
return caller.call(method, args, callback, null);
}
};
return (T) Enhancer.create(serviceInterface, invocationHandler);
}
示例10: build
import com.twitter.common.quantity.Time; //导入依赖的package包/类
public Thrift<T> build(DynamicHostSet hostSets) {
checkBaseState();
Preconditions.checkNotNull(hostSets);
ThriftPoolConfig cfg = new ThriftPoolConfig();
cfg.put("thrift.pool.transport.framed",String.valueOf(framedTransport));
cfg.put("thrift.pool.maxTotalPerKey", String.valueOf(maxConnectionsPerEndpoint));
cfg.put("thrift.pool.transport.socket_timeout", String.valueOf(thriftConfig.getSocketTimeout().as(Time.MILLISECONDS)));
ThriftConnectionPool<T> pool = new ThriftConnectionPool(serviceInterface, cfg);
ExecutorService executorService = createManagedThreadpool();
Function<TTransport, T> clientFactory = createClientFactory(serviceInterface);
LoadBalancer<HostAndPort> loadBalancer = createLoadBalancer(hostSets);
return new Thrift<>(thriftConfig, executorService, pool, serviceName,
serviceInterface, clientFactory,loadBalancer);
}
示例11: awaitShutdown
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* Attempts to shut down this server, and waits for the shutdown operation to complete.
*
* @param timeout Maximum amount of time to wait for shutdown before giving up. a timeout of
* zero means wait forever.
* @throws InterruptedException If interrupted while waiting for shutdown.
*/
public void awaitShutdown(Amount<Long, Time> timeout) throws InterruptedException {
Preconditions.checkNotNull(timeout);
shutdown();
if (listeningThread != null) {
listeningThread.join(timeout.as(Time.MILLISECONDS));
}
}
示例12: ServerSetup
import com.twitter.common.quantity.Time; //导入依赖的package包/类
public ServerSetup(String name, int port, TProcessor processor, TProtocolFactory protoFactory,
int numThreads, int queueSize, Amount<Integer, Time> socketTimeout) {
Preconditions.checkArgument(port >= 0 && port < 0xFFFF, "Invalid port: " + port);
Preconditions.checkArgument(numThreads != 0);
Preconditions.checkArgument(queueSize != 0);
if (socketTimeout != null) Preconditions.checkArgument(socketTimeout.getValue() >= 0);
this.name = name;
this.port = port;
this.processor = processor;
this.protoFactory = protoFactory;
this.numThreads = numThreads;
this.queueSize = queueSize;
this.socketTimeout = socketTimeout;
}
示例13: withRequestTimeout
import com.twitter.common.quantity.Time; //导入依赖的package包/类
/**
* Specifies that all calls be subject to a global timeout. This deadline includes all call
* activities, including obtaining a free connection and any automatic retries.
*/
public final T withRequestTimeout(Amount<Long, Time> timeout) {
Preconditions.checkNotNull(timeout);
Preconditions.checkArgument(timeout.getValue() >= 0,
"A negative deadline is invalid: %s", timeout);
config.requestTimeout = timeout;
return getThis();
}
示例14: call
import com.twitter.common.quantity.Time; //导入依赖的package包/类
@Override
public Object call(final Method method, final Object[] args,
final AsyncMethodCallback callback,
final Amount<Long, Time> connectTimeoutOverride) throws Throwable {
AtomicInteger attempts = new AtomicInteger();
Throwable exception = null;
boolean continueLoop;
try {
do {
try {
// If this is an async call, the looping will be handled within the capture.
return invoke(method, args, callback, null, connectTimeoutOverride);
} catch (Throwable t) {
exception = t;
if (!isRetryable(t)) {
if (debug) {
LOG.debug(String.format(
"Call failed with un-retryable exception of [%s]: %s, previous exceptions: %s",
t.getClass().getName(), t.getMessage(), t.getLocalizedMessage()));
}
throw t;
}
}
continueLoop = attempts.incrementAndGet() <= retries;
} while (continueLoop);
}finally {
// some exception found and retry fail
if (exception!=null) {
if(debug) {
LOG.debug(
String.format("Retried %d times, last error: %s",
attempts.get(), exception));
}
}
}
throw exception;
}
示例15: testSingleCall
import com.twitter.common.quantity.Time; //导入依赖的package包/类
@Test
public void testSingleCall() throws TException, InterruptedException {
ThriftServer server = mockServer(7911);
server.awaitForAlive();
Thrift<HelloClient> thrift = getIfaceThrift();
thrift.create().hi();
server.awaitShutdown(Amount.of(5L, Time.SECONDS));
}