public interface ContextPropagator
Async
).
A sample ContextPropagator
that copies all MDC
entries starting
with a given prefix along the code path looks like this:
public class MDCContextPropagator implements ContextPropagator {
public String getName() {
return this.getClass().getName();
}
public Object getCurrentContext() {
Map<String, String> context = new HashMap<>();
for (Map.Entry<String, String> entry : MDC.getCopyOfContextMap().entrySet()) {
if (entry.getKey().startsWith("X-")) {
context.put(entry.getKey(), entry.getValue());
}
}
return context;
}
public void setCurrentContext(Object context) {
Map<String, String> contextMap = (Map<String, String>)context;
for (Map.Entry<String, String> entry : contextMap.entrySet()) {
MDC.put(entry.getKey(), entry.getValue());
}
}
public Map<String, byte[]> serializeContext(Object context) {
Map<String, String> contextMap = (Map<String, String>)context;
Map<String, byte[]> serializedContext = new HashMap<>();
for (Map.Entry<String, String> entry : contextMap.entrySet()) {
serializedContext.put(entry.getKey(), entry.getValue().getBytes(StandardCharsets.UTF_8));
}
return serializedContext;
}
public Object deserializeContext(Map<String, Payload> context) {
Map<String, String> contextMap = new HashMap<>();
for (Map.Entry<String, byte[]> entry : context.entrySet()) {
contextMap.put(entry.getKey(), new String(entry.getValue(), StandardCharsets.UTF_8));
}
return contextMap;
}
}
To set up the context propagators, you must configure them when:
WorkflowClientOptions options = WorkflowClientOptions.newBuilder()
.setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
.build();
WorkflowClient client = WorkflowClient.newInstance(service, factoryOptions);
ContextPropagator
instances for your activities, you can
specify them at the ActivityOptions
level like so:
activities = Workflow.newActivityStub(Activity.class,
ActivityOptions.newBuilder()
.setScheduleToCloseTimeout(Duration.ofSeconds(60))
.setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
.build());
ChildWorkflowStub
:
ChildWorkflow childWorkflow = Workflow.newChildWorkflowStub(ChildWorkflow.class,
ChildWorkflowOptions.newBuilder()
.setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
.build());
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
deserializeContext(java.util.Map<java.lang.String,io.temporal.api.common.v1.Payload> context)
Turn the serialized header data into context object(s)
|
java.lang.Object |
getCurrentContext()
Returns the current context in object form
|
java.lang.String |
getName()
Returns the name of the context propagator (for use in serialization and transfer).
|
java.util.Map<java.lang.String,io.temporal.api.common.v1.Payload> |
serializeContext(java.lang.Object context)
Given context data, serialize it for transmission in the RPC header
|
void |
setCurrentContext(java.lang.Object context)
Sets the current context
|
java.lang.String getName()
java.util.Map<java.lang.String,io.temporal.api.common.v1.Payload> serializeContext(java.lang.Object context)
java.lang.Object deserializeContext(java.util.Map<java.lang.String,io.temporal.api.common.v1.Payload> context)
java.lang.Object getCurrentContext()
void setCurrentContext(java.lang.Object context)