Class AsyncContextImpl

java.lang.Object
org.apache.dubbo.rpc.AsyncContextImpl
All Implemented Interfaces:
AsyncContext

public class AsyncContextImpl extends Object implements AsyncContext
  • Constructor Details

    • AsyncContextImpl

      public AsyncContextImpl()
  • Method Details

    • write

      public void write(Object value)
      Description copied from interface: AsyncContext
      write value and complete the async context.
      Specified by:
      write in interface AsyncContext
      Parameters:
      value - invoke result
    • isAsyncStarted

      public boolean isAsyncStarted()
      Specified by:
      isAsyncStarted in interface AsyncContext
      Returns:
      true if the async context is started
    • stop

      public boolean stop()
      Description copied from interface: AsyncContext
      change the context state to stop
      Specified by:
      stop in interface AsyncContext
    • start

      public void start()
      Description copied from interface: AsyncContext
      change the context state to start
      Specified by:
      start in interface AsyncContext
    • signalContextSwitch

      public void signalContextSwitch()
      Description copied from interface: AsyncContext
      Signal RpcContext switch. Use this method to switch RpcContext from a Dubbo thread to a new thread created by the user. Note that you should use it in a new thread like this: public class AsyncServiceImpl implements AsyncService { public String sayHello(String name) { final AsyncContext asyncContext = RpcContext.startAsync(); new Thread(() -> { // right place to use this method asyncContext.signalContextSwitch(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } asyncContext.write("Hello " + name + ", response from provider."); }).start(); return null; } }
      Specified by:
      signalContextSwitch in interface AsyncContext
    • resetContext

      public void resetContext()
      Description copied from interface: AsyncContext
      Reset Context is not necessary. Only reset context after result was write back if it is necessary. public class AsyncServiceImpl implements AsyncService { public String sayHello(String name) { final AsyncContext asyncContext = RpcContext.startAsync(); new Thread(() -> {

      // the right place to use this method asyncContext.signalContextSwitch();

      try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } asyncContext.write("Hello " + name + ", response from provider."); // only reset after asyncContext.write() asyncContext.resetContext(); }).start(); return null; } } public class AsyncServiceImpl implements AsyncService { public CompletableFuture sayHello(String name) { CompletableFuture future = new CompletableFuture(); final AsyncContext asyncContext = RpcContext.startAsync(); new Thread(() -> { // the right place to use this method asyncContext.signalContextSwitch(); // some operations... future.complete(); // only reset after future.complete() asyncContext.resetContext(); }).start(); return future; } }

      Specified by:
      resetContext in interface AsyncContext
    • getInternalFuture

      public CompletableFuture<Object> getInternalFuture()