public class AsyncContextImpl extends Object implements AsyncContext
| 构造器和说明 |
|---|
AsyncContextImpl() |
| 限定符和类型 | 方法和说明 |
|---|---|
CompletableFuture<Object> |
getInternalFuture() |
boolean |
isAsyncStarted() |
void |
resetContext()
Reset Context is not necessary.
|
void |
signalContextSwitch()
Signal RpcContext switch.
|
void |
start()
change the context state to start
|
boolean |
stop()
change the context state to stop
|
void |
write(Object value)
write value and complete the async context.
|
public void write(Object value)
AsyncContextwrite 在接口中 AsyncContextvalue - invoke resultpublic boolean isAsyncStarted()
isAsyncStarted 在接口中 AsyncContextpublic boolean stop()
AsyncContextstop 在接口中 AsyncContextpublic void start()
AsyncContextstart 在接口中 AsyncContextpublic void signalContextSwitch()
AsyncContext
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;
}
}
signalContextSwitch 在接口中 AsyncContextpublic void resetContext()
AsyncContext
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;
}
}
resetContext 在接口中 AsyncContextpublic CompletableFuture<Object> getInternalFuture()
Copyright © 2011–2020 The Apache Software Foundation. All rights reserved.