Search in sources :

Example 1 with ThreadlessExecutor

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();
}
Also used : ThreadlessExecutor(org.apache.dubbo.common.threadpool.ThreadlessExecutor)

Example 2 with ThreadlessExecutor

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);
}
Also used : ThreadlessExecutor(org.apache.dubbo.common.threadpool.ThreadlessExecutor)

Example 3 with ThreadlessExecutor

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);
}
Also used : ThreadlessExecutor(org.apache.dubbo.common.threadpool.ThreadlessExecutor) MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) ExecutorService(java.util.concurrent.ExecutorService) DateTimeFormatter(java.time.format.DateTimeFormatter) TimeoutException(org.apache.dubbo.remoting.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

ThreadlessExecutor (org.apache.dubbo.common.threadpool.ThreadlessExecutor)3 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ExecutorService (java.util.concurrent.ExecutorService)1 Channel (org.apache.dubbo.remoting.Channel)1 TimeoutException (org.apache.dubbo.remoting.TimeoutException)1 Request (org.apache.dubbo.remoting.exchange.Request)1 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)1 Test (org.junit.jupiter.api.Test)1