Report events to another system from WorkflowImpl Class

Hello,

I have a usecase where I need to report status of each activity from workflowimpl class to another system thru Rest API . I wanted to check if there is a way to inject dependencies in workflowImpl so that these events can be reported ?

Yes, use interceptors. Note that this reporting should also be executed from activities.

Here is a trivial interceptor implementation: sdk-java/temporal-testing/src/main/java/io/temporal/testing/internal/TracingWorkerInterceptor.java at c6cceca24d01665b486872f9e4b186bc8f0326af · temporalio/sdk-java · GitHub

Hello,

Looking some guidance on publishing events when

1)workflow is initiated - using SimpleCountWorkflowInboundCallsInterceptor to intercept workflow
2)before and after each activity with the status - SimpleCountActivityInboundCallsInterceptor to intercept Activity
3)work flow completed - which base interceptor to override?

Also, i am looking to construct an object for publishing when the workflow is initiated(at SimpleCountWorkflowInboundCallsInterceptor ) and pass to all the activities so that i can just update the activity status . will passing thru headers work for this case.

@Slf4j
public class SimpleCountWorkflowInboundCallsInterceptor
    extends WorkflowInboundCallsInterceptorBase {

  private WorkflowInfo workflowInfo;

  public SimpleCountWorkflowInboundCallsInterceptor(WorkflowInboundCallsInterceptor next) {
    super(next);
  }

  @Override
  public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
    this.workflowInfo = Workflow.getInfo();
    super.init(new SimpleCountWorkflowOutboundCallsInterceptor(outboundCalls));
  }

**@Override**
**  public WorkflowOutput execute(WorkflowInput input) {**
**    //Publish Events**
**    return super.execute(input);**
**  }**
}

Using SimpleCountActivityInboundCallsInterceptor to intercept Activity

@Slf4j
public class SimpleCountActivityInboundCallsInterceptor
    extends ActivityInboundCallsInterceptorBase {

  private ActivityExecutionContext activityExecutionContext;

  public SimpleCountActivityInboundCallsInterceptor(ActivityInboundCallsInterceptor next) {
    super(next);
  }

  @Override
  public void init(ActivityExecutionContext context) {
    this.activityExecutionContext = context;
    super.init(context);
  }

  @Override
  public ActivityOutput execute(ActivityInput input) {
  //Publish Events
    ActivityOutput  output=super.execute(input);
 // Publish Events
  }
}

Sounds reasonable. Use activities to publish events.

Hi Maxim,

can you comment on this

Also, i am looking to construct an object for publishing when the workflow is initiated(at SimpleCountWorkflowInboundCallsInterceptor ) and pass to all the activities so that i can just update the activity status . will passing thru headers work for this case? if so can you point me to the sample cod

how do i access the payload send to workflow from activity and also the activity status

i am looking to construct an object for publishing when the workflow is initiated(at SimpleCountWorkflowInboundCallsInterceptor ) and pass to all the activities so that i can just update the activity status

Is this object serializable to a byte array? This is required to pass it from workflow to activities.

how do i access the payload send to workflow from activity and also the activity status

You cannot. Activities are by definition only receive arguments and return result. They don’t share state with workflow.

I have a feeling that you are solving the wrong problem. Why do you want to report the status of activities and workflow to an external system? Can you query Temporal for this information instead?

@maxim

As part of business requirement we are supposed to send status of each activity (before and after execution) to another internal system
like activity start time , activity end time, activity name, no of attempts , activity status along with data coming from request.

need help on how this can be achieved . whats the best solution if interceptors dont work for this case
point me to any sample code.

public class SimpleCountActivityInboundCallsInterceptor
    extends ActivityInboundCallsInterceptorBase {

  private ActivityExecutionContext activityExecutionContext;

  public SimpleCountActivityInboundCallsInterceptor(ActivityInboundCallsInterceptor next) {
    super(next);
  }

  @Override
  public void init(ActivityExecutionContext context) {
    this.activityExecutionContext = context;
    super.init(context);
  }

  @Override
  public ActivityOutput execute(ActivityInput input) {
  
    return super.execute(input);
  }
}

You can use an activity interceptor as well.

I am already using activity interceptors as
You suggested

SimpleCountActivityInboundCallsInterceptor (code pasted above ) I am not sure how I can get the activity status and also the payload which was passed on to the workflow