Interface ContextPropagator


  • public interface ContextPropagator
    Context Propagators are used to propagate information from workflow to activity, workflow to child workflow, and workflow to child thread (using 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, byte[]> 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:
    Setting up your workflow/activity workers:
    
     Worker.FactoryOptions factoryOptions = new Worker.FactoryOptions.Builder()
                     .setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
                     .build();
     Worker.Factory factory = new Worker.Factory("cadence", 7933,"test-domain", factoryOptions);
     

    Creating your WorkflowClient:
    
     WorkflowOptions options = new WorkflowOptions.Builder()
                     .setExecutionStartToCloseTimeout(Duration.ofSeconds(5))
                     .setTaskList("myTaskList")
                     .setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
                     .build();
    
     WorkflowClient workflowClient = WorkflowClient.newInstance("cadence", 7933,"test-domain");
     

    If you want to have override the ContextPropagator instances for your activities, you can specify them at the ActivityOptions level like so:
    
     activities = Workflow.newActivityStub(Activity.class,
                     new ActivityOptions.Builder()
                             .setScheduleToCloseTimeout(Duration.ofSeconds(60))
                             .setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
                             .build());
     

    And similarly, if you wish to override them for child workflows, you can do so when creating a ChildWorkflowStub:
    
     ChildWorkflow childWorkflow = Workflow.newChildWorkflowStub(ChildWorkflow.class,
                    new ChildWorkflowOptions.Builder()
                            .setContextPropagators(Collections.singletonList(new MDCContextPropagator()))
                            .build());
     
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.lang.Object deserializeContext​(java.util.Map<java.lang.String,​byte[]> 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,​byte[]> serializeContext​(java.lang.Object context)
      Given context data, serialize it for transmission in the Cadence header
      void setCurrentContext​(java.lang.Object context)
      Sets the current context
    • Method Detail

      • getName

        java.lang.String getName()
        Returns the name of the context propagator (for use in serialization and transfer). ContextPropagators will only be given context information
        Returns:
        The name of this propagator
      • serializeContext

        java.util.Map<java.lang.String,​byte[]> serializeContext​(java.lang.Object context)
        Given context data, serialize it for transmission in the Cadence header
      • deserializeContext

        java.lang.Object deserializeContext​(java.util.Map<java.lang.String,​byte[]> context)
        Turn the serialized header data into context object(s)
      • getCurrentContext

        java.lang.Object getCurrentContext()
        Returns the current context in object form
      • setCurrentContext

        void setCurrentContext​(java.lang.Object context)
        Sets the current context