Cancel currently running workflow from its running activity

You can look at the following samples:

  1. Terminate via api: samples-java/src/main/java/io/temporal/samples/terminateworkflow at master · temporalio/samples-j and associated test:
    samples-java/src/test/java/io/temporal/samples/terminateworkflow at master · temporalio/samples-j
  2. Cancellation: samples-java/HelloCancellationScope.java at master · temporalio/samples-java · GitHub and samples-java/HelloDetachedCancellationScope.java at master · temporalio/samples-java · GitHub

I would also consider reading this thread to learn more about cancellation.

Note that you can canel/terminate workflows also via tctl - Temporal CLI - tctl | Temporal documentation