Strange output when do a print in @SignalMethod

So I created a signal method just print out the signal

    @WorkflowInterface
    public interface SignalWorkflow {

        // The Workflow method is called by the initiator either via code or CLI.
        @WorkflowMethod
        void start();

        @SignalMethod
        void print(String str);
    }

    public static class SignalWorkflowImpl implements SignalWorkflow {

        private int i = 0;

        @Override
        public void start() {
            Workflow.sleep(Duration.ofHours(1));
        }

        @Override
        public void print(String str) {
            i++;
            System.out.println("" + i + ": " + str);
        }
    }

I then send couple signals to the workflow:
tctl wf signal --workflow_id “dfcfc908-a3ba-4182-8e3f-caccaf378e2b” --name “print” --input “Hi1”
tctl wf signal --workflow_id “dfcfc908-a3ba-4182-8e3f-caccaf378e2b” --name “print” --input “Hi2”
tctl wf signal --workflow_id “dfcfc908-a3ba-4182-8e3f-caccaf378e2b” --name “print” --input “Hi3”
tctl wf signal --workflow_id “dfcfc908-a3ba-4182-8e3f-caccaf378e2b” --name “print” --input “Hi4”
And the output looks like this:
1: Hi1
1: Hi1
2: Hi2
1: Hi1
2: Hi2
3: Hi3
1: Hi1
2: Hi2
3: Hi3
4: Hi4

Every time I send a new signal, all previous signals got printed as well. Why is that?

Temporal replays workflow code from the beginning to recover its state. So it is expected that the same code is executed multiple times. Use a logger created through Workflow.getLogger to log messages from a workflow to avoid duplicated entries. You can also use Workflow.isReplaying to dedupe print statements. Note that isReplaying should never be used for any workflow logic beyond filtering out side effects as it would break determinism.

1 Like

Thank you very much @maxim! Logger works very well.
With the keyword “replay” you provided, I found this document page Implementing Workflows | Temporal
It says that Activity is not replayed. Is Logger an Activity?

No, the logger is completely local. It relies on isReplaying flag.