use of java.util.concurrent.ScheduledThreadPoolExecutor in project toolkit by googleapis.
the class Pubsub method grpc.
private static void grpc(final Settings settings) throws Exception {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).build();
final Semaphore semaphore = new Semaphore(settings.numWorkers());
Metadata header = new Metadata();
header.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
final PublisherFutureStub stub = MetadataUtils.attachHeaders(PublisherGrpc.newFutureStub(channel), header);
final AtomicLong resetTime = new AtomicLong();
final AtomicLong numCalls = new AtomicLong();
final AtomicLong numErrs = new AtomicLong();
long endTime = System.nanoTime() + settings.warmDurNano() + settings.targetDurNano();
Thread resetter = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(settings.warmDurNano() / MILLION);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
numCalls.set(0);
numErrs.set(0);
resetTime.set(System.nanoTime());
}
});
resetter.start();
while (System.nanoTime() < endTime) {
semaphore.acquire(1);
Futures.addCallback(stub.getTopic(GetTopicRequest.newBuilder().setTopic(TOPIC_NAME_STRING).build()), new FutureCallback<Topic>() {
@Override
public void onSuccess(Topic topic) {
if (!topic.getName().equals(TOPIC_NAME_STRING)) {
numErrs.incrementAndGet();
}
both();
}
@Override
public void onFailure(Throwable t) {
numErrs.incrementAndGet();
both();
}
void both() {
numCalls.incrementAndGet();
semaphore.release(1);
}
});
}
long nCalls = numCalls.get();
long nErrs = numErrs.get();
long runDurNano = System.nanoTime() - resetTime.get();
System.out.println("errors: " + nErrs);
System.out.println("calls: " + nCalls);
System.out.println("time per call (ns): " + (runDurNano / nCalls));
System.out.println("QPS: " + (nCalls * BILLION / runDurNano));
channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
use of java.util.concurrent.ScheduledThreadPoolExecutor in project hive by apache.
the class SQLOperation method prepare.
/**
* Compile the query and extract metadata
*
* @throws HiveSQLException
*/
public void prepare(QueryState queryState) throws HiveSQLException {
setState(OperationState.RUNNING);
try {
driver = DriverFactory.newDriver(queryState, getParentSession().getUserName(), queryInfo);
// queryTimeout == 0 means no timeout
if (queryTimeout > 0) {
timeoutExecutor = new ScheduledThreadPoolExecutor(1);
Runnable timeoutTask = new Runnable() {
@Override
public void run() {
try {
String queryId = queryState.getQueryId();
LOG.info("Query timed out after: " + queryTimeout + " seconds. Cancelling the execution now: " + queryId);
SQLOperation.this.cancel(OperationState.TIMEDOUT);
} catch (HiveSQLException e) {
LOG.error("Error cancelling the query after timeout: " + queryTimeout + " seconds", e);
} finally {
// Stop
timeoutExecutor.shutdown();
}
}
};
timeoutExecutor.schedule(timeoutTask, queryTimeout, TimeUnit.SECONDS);
}
queryInfo.setQueryDisplay(driver.getQueryDisplay());
// set the operation handle information in Driver, so that thrift API users
// can use the operation handle they receive, to lookup query information in
// Yarn ATS
String guid64 = Base64.encodeBase64URLSafeString(getHandle().getHandleIdentifier().toTHandleIdentifier().getGuid()).trim();
driver.setOperationId(guid64);
// In Hive server mode, we are not able to retry in the FetchTask
// case, when calling fetch queries since execute() has returned.
// For now, we disable the test attempts.
response = driver.compileAndRespond(statement);
if (0 != response.getResponseCode()) {
throw toSQLException("Error while compiling statement", response);
}
mResultSchema = driver.getSchema();
// "explain" is an exception for now
if (driver.getPlan().getFetchTask() != null) {
// Schema has to be set
if (mResultSchema == null || !mResultSchema.isSetFieldSchemas()) {
throw new HiveSQLException("Error compiling query: Schema and FieldSchema " + "should be set when query plan has a FetchTask");
}
resultSchema = new TableSchema(mResultSchema);
setHasResultSet(true);
} else {
setHasResultSet(false);
}
// TODO explain should use a FetchTask for reading
for (Task<? extends Serializable> task : driver.getPlan().getRootTasks()) {
if (task.getClass() == ExplainTask.class) {
resultSchema = new TableSchema(mResultSchema);
setHasResultSet(true);
break;
}
}
} catch (HiveSQLException e) {
setState(OperationState.ERROR);
throw e;
} catch (Throwable e) {
setState(OperationState.ERROR);
throw new HiveSQLException("Error running query: " + e.toString(), e);
}
}
use of java.util.concurrent.ScheduledThreadPoolExecutor in project cxf by apache.
the class NotificatorService method start.
/**
* Starts this NotificatorService. You MUST run this method on every instance
* before starting to pass any events to it. Run it only once.
*/
public void start() {
obtainManager().registerNotificator(this);
service = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE);
}
use of java.util.concurrent.ScheduledThreadPoolExecutor in project tomee by apache.
the class ManagedScheduledExecutorServiceImplFactoryTest method createServiceTest.
@Test
public void createServiceTest() {
final ManagedScheduledExecutorServiceImplFactory factory = new ManagedScheduledExecutorServiceImplFactory();
factory.setThreadFactory(MyThreadFactory.class.getName());
final ManagedScheduledExecutorServiceImpl executorService = factory.create();
final ScheduledThreadPoolExecutor poolExecutor = (ScheduledThreadPoolExecutor) executorService.getDelegate();
Assert.assertEquals(poolExecutor.getThreadFactory().getClass(), ManagedThreadFactoryImpl.class);
}
use of java.util.concurrent.ScheduledThreadPoolExecutor in project springside4 by springside.
the class ThreadPoolUtilTest method wrapException.
@Test
public void wrapException() {
ScheduledThreadPoolExecutor executor = ThreadPoolBuilder.scheduledPool().build();
ExceptionTask task = new ExceptionTask();
executor.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS);
ThreadUtil.sleep(500);
// 线程第一次跑就被中断
assertThat(task.counter.get()).isEqualTo(1);
ThreadPoolUtil.gracefulShutdown(executor, 1000);
// //////
executor = ThreadPoolBuilder.scheduledPool().build();
ExceptionTask newTask = new ExceptionTask();
Runnable wrapTask = ThreadPoolUtil.safeRunnable(newTask);
executor.scheduleAtFixedRate(wrapTask, 0, 100, TimeUnit.MILLISECONDS);
ThreadUtil.sleep(500);
assertThat(newTask.counter.get()).isGreaterThan(2);
System.out.println("-------actual run:" + task.counter.get());
ThreadPoolUtil.gracefulShutdown(executor, 1000);
}
Aggregations