Package org.apache.dubbo.rpc
Class RpcContext
java.lang.Object
org.apache.dubbo.rpc.RpcContext
- Direct Known Subclasses:
RpcContextAttachment,RpcServiceContext
Thread local context. (API, ThreadLocal, ThreadSafe)
Note: RpcContext is a temporary state holder. States in RpcContext changes every time when request is sent or received.
There are four kinds of RpcContext, which are ServerContext, ClientAttachment, ServerAttachment and ServiceContext. ServiceContext: Using to pass environment parameters in the whole invocation. For example, `remotingApplicationName`, `remoteAddress`, etc.RpcServiceContext
ClientAttachment, ServerAttachment and ServiceContext are using to transfer attachments.
Imaging a situation like this, A is calling B, and B will call C, after that, B wants to return some attachments back to A.
ClientAttachment is using to pass attachments to next hop as a consumer. ( A --> B , in A side)
ServerAttachment is using to fetch attachments from previous hop as a provider. ( A --> B , in B side)
ServerContext is using to return some attachments back to client as a provider. ( A invalid input: '<'-- B , in B side)
The reason why using `ServiceContext` is to make API compatible with previous.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classUsed to temporarily store and restore all kinds of contexts of current thread.static class -
Method Summary
Modifier and TypeMethodDescriptionvoidone way async call, send request only, and result is not required<T> CompletableFuture<T> Async invocation.booleanvoidclearAfterEachInvoke(boolean remove) voidget()Deprecated.Deprecated.Object[]get arguments.getAttachment(String key) also seegetObjectAttachment(String).Deprecated.static CancellationContextstatic RpcContextAttachmentget consumer side attachment ( A --> B , in A side)static RpcContextAttachmentremove server side context.<T> CompletableFuture<T> get CompletableFuture.static RpcContextDeprecated.static RpcServiceContext<T> Future<T> get future.getGroup()Deprecated.Replace to getMethodName(), getParameterTypes(), getArguments()Invoker<?> Deprecated.Replace to getUrl()Deprecated.Replace to getUrls()get local address.get local host.get local host name.intget local port.get method name.get attachment.get attachments.Class<?>[]get parameter types.get remote address.get remote address string.get remote host.get remote host name.intget remote port.Get the request object of the underlying RPC protocol, e.g.<T> TgetRequest(Class<T> clazz) Get the request object of the underlying RPC protocol, e.g.Get the response object of the underlying RPC protocol, e.g.<T> TgetResponse(Class<T> clazz) Get the response object of the underlying RPC protocol, e.g.static RpcContextAttachmentget provider side attachment from consumer ( A --> B , in B side)static RpcContextAttachmentget server side context. ( A invalid input: '<'-- B , in B side)static RpcContextAttachmentstatic RpcServiceContextUsing to pass environment parameters in the whole invocation.getUrl()getUrls()booleanbooleanDeprecated.Replace to isConsumerSide()booleanis consumer side.booleanis provider side.booleanDeprecated.Replace to isProviderSide()Deprecated.removeAttachment(String key) remove attachment.static voidstatic voidstatic voidstatic voidcustomized for internal use.static voidstatic voidstatic voidstatic voidstatic voidrestoreCancellationContext(CancellationContext oldContext) static voidrestoreServiceContext(RpcContext.RestoreServiceContext restoreServiceContext) Deprecated.voidsetArguments(Object[] arguments) setAttachment(String key, Object value) setAttachment(String key, String value) set attachment.setAttachments(Map<String, String> attachment) set attachmentsvoidsetConsumerUrl(URL consumerUrl) voidsetFuture(CompletableFuture<?> future) set future.setInvocation(Invocation invocation) setInvoker(Invoker<?> invoker) setInvokers(List<Invoker<?>> invokers) setLocalAddress(String host, int port) set local address.setLocalAddress(InetSocketAddress address) set local address.voidsetMethodName(String methodName) setObjectAttachment(String key, Object value) setObjectAttachments(Map<String, Object> attachment) set attachmentsvoidsetParameterTypes(Class<?>[] parameterTypes) setRemoteAddress(String host, int port) set remote address.setRemoteAddress(InetSocketAddress address) set remote address.setRemoteApplicationName(String remoteApplicationName) voidsetRequest(Object request) voidsetResponse(Object response) static voidsetRpcContext(URL url) Deprecated.voidvoidstatic AsyncContextboolean
-
Method Details
-
getCancellationContext
-
removeCancellationContext
public static void removeCancellationContext() -
restoreCancellationContext
-
getServerContext
get server side context. ( A invalid input: '<'-- B , in B side)- Returns:
- server context
-
getClientResponseContext
remove server side context.- See Also:
-
getServerResponseContext
-
removeClientResponseContext
public static void removeClientResponseContext() -
removeServerResponseContext
public static void removeServerResponseContext() -
getContext
Deprecated.get context.- Returns:
- context
-
getClientAttachment
get consumer side attachment ( A --> B , in A side)- Returns:
- context
-
getServerAttachment
get provider side attachment from consumer ( A --> B , in B side)- Returns:
- context
-
removeServerContext
public static void removeServerContext() -
canRemove
public boolean canRemove() -
clearAfterEachInvoke
public void clearAfterEachInvoke(boolean remove) -
getServiceContext
Using to pass environment parameters in the whole invocation. For example, `remotingApplicationName`, `remoteAddress`, etc.RpcServiceContext- Returns:
- context
-
getCurrentServiceContext
-
removeServiceContext
public static void removeServiceContext() -
removeClientAttachment
public static void removeClientAttachment() -
removeServerAttachment
public static void removeServerAttachment() -
removeContext
public static void removeContext()customized for internal use. -
getRequest
Get the request object of the underlying RPC protocol, e.g. HttpServletRequest- Returns:
- null if the underlying protocol doesn't provide support for getting request
-
setRequest
-
getRequest
Get the request object of the underlying RPC protocol, e.g. HttpServletRequest- Returns:
- null if the underlying protocol doesn't provide support for getting request or the request is not of the specified type
-
getResponse
Get the response object of the underlying RPC protocol, e.g. HttpServletResponse- Returns:
- null if the underlying protocol doesn't provide support for getting response
-
setResponse
-
getResponse
Get the response object of the underlying RPC protocol, e.g. HttpServletResponse- Returns:
- null if the underlying protocol doesn't provide support for getting response or the response is not of the specified type
-
isProviderSide
public boolean isProviderSide()is provider side.- Returns:
- provider side.
-
isConsumerSide
public boolean isConsumerSide()is consumer side.- Returns:
- consumer side.
-
getCompletableFuture
get CompletableFuture.- Type Parameters:
T-- Returns:
- future
-
getFuture
get future.- Type Parameters:
T-- Returns:
- future
-
setFuture
set future.- Parameters:
future-
-
getUrls
-
setUrls
-
getUrl
-
setUrl
-
getMethodName
get method name.- Returns:
- method name.
-
setMethodName
-
getParameterTypes
get parameter types. -
setParameterTypes
-
getArguments
get arguments.- Returns:
- arguments.
-
setArguments
-
setLocalAddress
set local address.- Parameters:
host-port-- Returns:
- context
-
getLocalAddress
get local address.- Returns:
- local address
-
setLocalAddress
set local address.- Parameters:
address-- Returns:
- context
-
getLocalAddressString
-
getLocalHostName
get local host name.- Returns:
- local host name
-
setRemoteAddress
set remote address.- Parameters:
host-port-- Returns:
- context
-
getRemoteAddress
get remote address.- Returns:
- remote address
-
setRemoteAddress
set remote address.- Parameters:
address-- Returns:
- context
-
getRemoteApplicationName
-
setRemoteApplicationName
-
getRemoteAddressString
get remote address string.- Returns:
- remote address string.
-
getRemoteHostName
get remote host name.- Returns:
- remote host name
-
getLocalHost
get local host.- Returns:
- local host
-
getLocalPort
public int getLocalPort()get local port.- Returns:
- port
-
getRemoteHost
get remote host.- Returns:
- remote host
-
getRemotePort
public int getRemotePort()get remote port.- Returns:
- remote port
-
getAttachment
also seegetObjectAttachment(String).- Parameters:
key-- Returns:
- attachment
-
getObjectAttachment
get attachment.- Parameters:
key-- Returns:
- attachment
-
setAttachment
set attachment.- Parameters:
key-value-- Returns:
- context
-
setAttachment
-
setObjectAttachment
-
removeAttachment
remove attachment.- Parameters:
key-- Returns:
- context
-
getAttachments
Deprecated.get attachments.- Returns:
- attachments
-
getObjectAttachments
get attachments.- Returns:
- attachments
-
setAttachments
set attachments- Parameters:
attachment-- Returns:
- context
-
setObjectAttachments
set attachments- Parameters:
attachment-- Returns:
- context
-
clearAttachments
public void clearAttachments() -
get
Deprecated.get values.- Returns:
- values
-
set
Deprecated.set value.- Parameters:
key-value-- Returns:
- context
-
remove
Deprecated.remove value.- Parameters:
key-- Returns:
- value
-
get
Deprecated.get value.- Parameters:
key-- Returns:
- value
-
isServerSide
Deprecated.Replace to isProviderSide() -
isClientSide
Deprecated.Replace to isConsumerSide() -
getInvokers
Deprecated.Replace to getUrls() -
setInvokers
-
getInvoker
Deprecated.Replace to getUrl() -
setInvoker
-
getInvocation
Deprecated.Replace to getMethodName(), getParameterTypes(), getArguments() -
setInvocation
-
asyncCall
Async invocation. Timeout will be handled even ifFuture.get()is not called.- Parameters:
callable-- Returns:
- get the return result from
future.get()
-
asyncCall
one way async call, send request only, and result is not required- Parameters:
runnable-
-
startAsync
- Returns:
- Throws:
IllegalStateException
-
isAsyncStarted
public boolean isAsyncStarted() -
stopAsync
public boolean stopAsync() -
getAsyncContext
-
getGroup
-
getVersion
-
getInterfaceName
-
getProtocol
-
getServiceKey
-
getProtocolServiceKey
-
getConsumerUrl
-
setConsumerUrl
-
setRpcContext
Deprecated. -
storeServiceContext
-
restoreServiceContext
-