All Products
Search
Document Center

Enterprise Distributed Application Service:Call context

Last Updated:Mar 11, 2026

The call context carries call properties and transmitted custom data other than interface-specific parameters. Call properties include the caller IP address, application name, and timeout period. You can also transmit custom key-value data between consumers and providers without modifying the service interface.

HSF provides two classes for working with call context:

ClassPurpose
com.taobao.hsf.util.RequestCtxUtilGet and set built-in call properties such as timeout, IP routing, and caller info
RPCContextPass custom key-value data between consumers and providers

How RequestCtxUtil works

RequestCtxUtil stores call properties in a ThreadLocal variable. Each property is bound to the current thread and cleared after a single RPC call completes.

Important

Every getXxx call removes the property from ThreadLocal.

Consumer methods

Call these methods on the consumer side before or after an RPC call.

MethodDescription
setRequestTimeout()Set the timeout for the next call.
setUserId()Set the user ID for unit-service routing. Required for generic calls.
getProviderIp()Get the IP address of the provider that handled the last call.
setTargetServerIp(String ip)Route the next call to a specific provider. The IP must exist in the in-memory provider list from the registry.
setDirectTargetServerIp(String targetIp)Route the next call to a specific provider, bypassing the registry. The IP does not need to be in the in-memory provider list.

Provider methods

Call these methods on the provider side to inspect the incoming request.

MethodDescription
getClientIp()Get the caller's IP address.
getAppNameOfClient()Get the name of the caller's application.
isHttpRequest()Check whether the call arrived over HTTP.
getHttpHeader(String key)Get a specific HTTP request header value.

Pass custom data with RPCContext

RPCContext lets you attach arbitrary key-value pairs to an RPC call without modifying the service interface.

Values can be custom domain object (DO) types or basic types. Both the consumer and provider must be able to serialize the type.

Set attachments (consumer side)

Set attachments before making the RPC call. The context is transmitted automatically with the request.

// Set custom context before the RPC call
RPCContext rpcContext = RPCContext.getClientContext();
rpcContext.putAttachment("tetantId", "123");

// Make the RPC call. The context is sent along with the request.
orderService.queryOrder(1L);

Read attachments (provider side)

Read attachments inside the provider method implementation.

// Read custom context from the incoming request
RPCContext rpcContext = RPCContext.getServerContext();
String myContext = (String) rpcContext.getAttachment("tetantId");

ThreadLocal lifecycle

RequestCtxUtil stores call properties in a ThreadLocal variable. Context data is scoped to a single call on the current thread and cleared after the call completes.