use of org.apache.dubbo.common.threadpool.ThreadlessExecutor in project dubbo by alibaba.
the class AsyncRpcResult method get.
/**
* This method will always return after a maximum 'timeout' waiting:
* 1. if value returns before timeout, return normally.
* 2. if no value returns after timeout, throw TimeoutException.
*
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
@Override
public Result get() throws InterruptedException, ExecutionException {
if (executor != null && executor instanceof ThreadlessExecutor) {
ThreadlessExecutor threadlessExecutor = (ThreadlessExecutor) executor;
threadlessExecutor.waitAndDrain();
}
return responseFuture.get();
}
use of org.apache.dubbo.common.threadpool.ThreadlessExecutor in project dubbo by alibaba.
the class AsyncRpcResult method get.
@Override
public Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
if (executor != null && executor instanceof ThreadlessExecutor) {
ThreadlessExecutor threadlessExecutor = (ThreadlessExecutor) executor;
threadlessExecutor.waitAndDrain();
}
return responseFuture.get(timeout, unit);
}
use of org.apache.dubbo.common.threadpool.ThreadlessExecutor in project dubbo by alibaba.
the class DefaultFutureTest method interruptSend.
/**
* for example, it will print like this:
*before a future is create , time is : 2021-01-22 10:55:03
* null
* after a future is timeout , time is : 2021-01-22 10:55:05
*/
@Test
public void interruptSend() throws Exception {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("before a future is create , time is : " + LocalDateTime.now().format(formatter));
// timeout after 1 seconds.
Channel channel = new MockedChannel();
int channelId = 10;
Request request = new Request(channelId);
ExecutorService sharedExecutor = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().createExecutorIfAbsent(URL.valueOf("dubbo://127.0.0.1:23456"));
ThreadlessExecutor executor = new ThreadlessExecutor(sharedExecutor);
DefaultFuture f = DefaultFuture.newFuture(channel, request, 1000, executor);
// mark the future is sent
DefaultFuture.sent(channel, request);
// get operate will throw a interrupted exception, because the thread is interrupted.
try {
new InterruptThread(Thread.currentThread()).start();
executor.waitAndDrain();
f.get();
} catch (Exception e) {
Assertions.assertTrue(e instanceof InterruptedException, "catch exception is not interrupted exception!");
System.out.println(e.getMessage());
}
// waiting timeout check task finished
Thread.sleep(1500);
System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
DefaultFuture future = DefaultFuture.getFuture(channelId);
// waiting future should be removed by time out check task
Assertions.assertNull(future);
}
Aggregations