Class RpcContext

java.lang.Object
org.apache.dubbo.rpc.RpcContext
Direct Known Subclasses:
RpcContextAttachment, RpcServiceContext

public class RpcContext extends Object
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:
  • Method Details

    • getCancellationContext

      public static CancellationContext getCancellationContext()
    • removeCancellationContext

      public static void removeCancellationContext()
    • restoreCancellationContext

      public static void restoreCancellationContext(CancellationContext oldContext)
    • getServerContext

      public static RpcContextAttachment getServerContext()
      get server side context. ( A invalid input: '<'-- B , in B side)
      Returns:
      server context
    • getClientResponseContext

      public static RpcContextAttachment getClientResponseContext()
      remove server side context.
      See Also:
    • getServerResponseContext

      public static RpcContextAttachment getServerResponseContext()
    • removeClientResponseContext

      public static void removeClientResponseContext()
    • removeServerResponseContext

      public static void removeServerResponseContext()
    • getContext

      @Deprecated public static RpcContext getContext()
      Deprecated.
      get context.
      Returns:
      context
    • getClientAttachment

      public static RpcContextAttachment getClientAttachment()
      get consumer side attachment ( A --> B , in A side)
      Returns:
      context
    • getServerAttachment

      public static RpcContextAttachment 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

      public static RpcServiceContext getServiceContext()
      Using to pass environment parameters in the whole invocation. For example, `remotingApplicationName`, `remoteAddress`, etc. RpcServiceContext
      Returns:
      context
    • getCurrentServiceContext

      public static RpcServiceContext 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

      public Object 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

      public void setRequest(Object request)
    • getRequest

      public <T> T getRequest(Class<T> clazz)
      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

      public Object 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

      public void setResponse(Object response)
    • getResponse

      public <T> T getResponse(Class<T> clazz)
      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

      public <T> CompletableFuture<T> getCompletableFuture()
      get CompletableFuture.
      Type Parameters:
      T -
      Returns:
      future
    • getFuture

      public <T> Future<T> getFuture()
      get future.
      Type Parameters:
      T -
      Returns:
      future
    • setFuture

      public void setFuture(CompletableFuture<?> future)
      set future.
      Parameters:
      future -
    • getUrls

      public List<URL> getUrls()
    • setUrls

      public void setUrls(List<URL> urls)
    • getUrl

      public URL getUrl()
    • setUrl

      public void setUrl(URL url)
    • getMethodName

      public String getMethodName()
      get method name.
      Returns:
      method name.
    • setMethodName

      public void setMethodName(String methodName)
    • getParameterTypes

      public Class<?>[] getParameterTypes()
      get parameter types.
    • setParameterTypes

      public void setParameterTypes(Class<?>[] parameterTypes)
    • getArguments

      public Object[] getArguments()
      get arguments.
      Returns:
      arguments.
    • setArguments

      public void setArguments(Object[] arguments)
    • setLocalAddress

      public RpcContext setLocalAddress(String host, int port)
      set local address.
      Parameters:
      host -
      port -
      Returns:
      context
    • getLocalAddress

      public InetSocketAddress getLocalAddress()
      get local address.
      Returns:
      local address
    • setLocalAddress

      public RpcContext setLocalAddress(InetSocketAddress address)
      set local address.
      Parameters:
      address -
      Returns:
      context
    • getLocalAddressString

      public String getLocalAddressString()
    • getLocalHostName

      public String getLocalHostName()
      get local host name.
      Returns:
      local host name
    • setRemoteAddress

      public RpcContext setRemoteAddress(String host, int port)
      set remote address.
      Parameters:
      host -
      port -
      Returns:
      context
    • getRemoteAddress

      public InetSocketAddress getRemoteAddress()
      get remote address.
      Returns:
      remote address
    • setRemoteAddress

      public RpcContext setRemoteAddress(InetSocketAddress address)
      set remote address.
      Parameters:
      address -
      Returns:
      context
    • getRemoteApplicationName

      public String getRemoteApplicationName()
    • setRemoteApplicationName

      public RpcContext setRemoteApplicationName(String remoteApplicationName)
    • getRemoteAddressString

      public String getRemoteAddressString()
      get remote address string.
      Returns:
      remote address string.
    • getRemoteHostName

      public String getRemoteHostName()
      get remote host name.
      Returns:
      remote host name
    • getLocalHost

      public String getLocalHost()
      get local host.
      Returns:
      local host
    • getLocalPort

      public int getLocalPort()
      get local port.
      Returns:
      port
    • getRemoteHost

      public String getRemoteHost()
      get remote host.
      Returns:
      remote host
    • getRemotePort

      public int getRemotePort()
      get remote port.
      Returns:
      remote port
    • getAttachment

      public String getAttachment(String key)
      Parameters:
      key -
      Returns:
      attachment
    • getObjectAttachment

      public Object getObjectAttachment(String key)
      get attachment.
      Parameters:
      key -
      Returns:
      attachment
    • setAttachment

      public RpcContext setAttachment(String key, String value)
      set attachment.
      Parameters:
      key -
      value -
      Returns:
      context
    • setAttachment

      public RpcContext setAttachment(String key, Object value)
    • setObjectAttachment

      public RpcContext setObjectAttachment(String key, Object value)
    • removeAttachment

      public RpcContext removeAttachment(String key)
      remove attachment.
      Parameters:
      key -
      Returns:
      context
    • getAttachments

      @Deprecated public Map<String,String> getAttachments()
      Deprecated.
      get attachments.
      Returns:
      attachments
    • getObjectAttachments

      public Map<String,Object> getObjectAttachments()
      get attachments.
      Returns:
      attachments
    • setAttachments

      public RpcContext setAttachments(Map<String,String> attachment)
      set attachments
      Parameters:
      attachment -
      Returns:
      context
    • setObjectAttachments

      public RpcContext setObjectAttachments(Map<String,Object> attachment)
      set attachments
      Parameters:
      attachment -
      Returns:
      context
    • clearAttachments

      public void clearAttachments()
    • get

      @Deprecated public Map<String,Object> get()
      Deprecated.
      get values.
      Returns:
      values
    • set

      @Deprecated public RpcContext set(String key, Object value)
      Deprecated.
      set value.
      Parameters:
      key -
      value -
      Returns:
      context
    • remove

      @Deprecated public RpcContext remove(String key)
      Deprecated.
      remove value.
      Parameters:
      key -
      Returns:
      value
    • get

      @Deprecated public Object get(String key)
      Deprecated.
      get value.
      Parameters:
      key -
      Returns:
      value
    • isServerSide

      @Deprecated public boolean isServerSide()
      Deprecated.
      Replace to isProviderSide()
    • isClientSide

      @Deprecated public boolean isClientSide()
      Deprecated.
      Replace to isConsumerSide()
    • getInvokers

      @Deprecated public List<Invoker<?>> getInvokers()
      Deprecated.
      Replace to getUrls()
    • setInvokers

      public RpcContext setInvokers(List<Invoker<?>> invokers)
    • getInvoker

      @Deprecated public Invoker<?> getInvoker()
      Deprecated.
      Replace to getUrl()
    • setInvoker

      public RpcContext setInvoker(Invoker<?> invoker)
    • getInvocation

      @Deprecated public Invocation getInvocation()
      Deprecated.
      Replace to getMethodName(), getParameterTypes(), getArguments()
    • setInvocation

      public RpcContext setInvocation(Invocation invocation)
    • asyncCall

      public <T> CompletableFuture<T> asyncCall(Callable<T> callable)
      Async invocation. Timeout will be handled even if Future.get() is not called.
      Parameters:
      callable -
      Returns:
      get the return result from future.get()
    • asyncCall

      public void asyncCall(Runnable runnable)
      one way async call, send request only, and result is not required
      Parameters:
      runnable -
    • startAsync

      public static AsyncContext startAsync() throws IllegalStateException
      Returns:
      Throws:
      IllegalStateException
    • isAsyncStarted

      public boolean isAsyncStarted()
    • stopAsync

      public boolean stopAsync()
    • getAsyncContext

      public AsyncContext getAsyncContext()
    • getGroup

      public String getGroup()
    • getVersion

      public String getVersion()
    • getInterfaceName

      public String getInterfaceName()
    • getProtocol

      public String getProtocol()
    • getServiceKey

      public String getServiceKey()
    • getProtocolServiceKey

      public String getProtocolServiceKey()
    • getConsumerUrl

      public URL getConsumerUrl()
    • setConsumerUrl

      public void setConsumerUrl(URL consumerUrl)
    • setRpcContext

      @Deprecated public static void setRpcContext(URL url)
      Deprecated.
    • storeServiceContext

      public static RpcContext.RestoreServiceContext storeServiceContext()
    • restoreServiceContext

      public static void restoreServiceContext(RpcContext.RestoreServiceContext restoreServiceContext)