Package org.apache.dubbo.rpc
Class AsyncContextImpl
java.lang.Object
org.apache.dubbo.rpc.AsyncContextImpl
- All Implemented Interfaces:
AsyncContext
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
AsyncContextImpl
public AsyncContextImpl()
-
-
Method Details
-
write
Description copied from interface:AsyncContextwrite value and complete the async context.- Specified by:
writein interfaceAsyncContext- Parameters:
value- invoke result
-
isAsyncStarted
public boolean isAsyncStarted()- Specified by:
isAsyncStartedin interfaceAsyncContext- Returns:
- true if the async context is started
-
stop
public boolean stop()Description copied from interface:AsyncContextchange the context state to stop- Specified by:
stopin interfaceAsyncContext
-
start
public void start()Description copied from interface:AsyncContextchange the context state to start- Specified by:
startin interfaceAsyncContext
-
signalContextSwitch
public void signalContextSwitch()Description copied from interface:AsyncContextSignal 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:
signalContextSwitchin interfaceAsyncContext
-
resetContext
public void resetContext()Description copied from interface:AsyncContextReset 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:
resetContextin interfaceAsyncContext
-
getInternalFuture
-