-
Notifications
You must be signed in to change notification settings - Fork 182
Samples for Standalone Activities [DO NOT MERGE] #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GregoryTravis
wants to merge
3
commits into
main
Choose a base branch
from
gmt/standalone-activities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
core/src/main/java/io/temporal/samples/hello/HelloStandaloneActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package io.temporal.samples.hello; | ||
|
|
||
| import io.temporal.activity.ActivityInterface; | ||
| import io.temporal.activity.ActivityMethod; | ||
| import io.temporal.client.ActivityClient; | ||
| import io.temporal.client.ActivityClientOptions; | ||
| import io.temporal.client.StartActivityOptions; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.envconfig.ClientConfigProfile; | ||
| import io.temporal.serviceclient.WorkflowServiceStubs; | ||
| import io.temporal.worker.Worker; | ||
| import io.temporal.worker.WorkerFactory; | ||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Sample Temporal application that executes a Standalone Activity — an Activity that runs | ||
| * independently, without being orchestrated by a Workflow. Requires a local instance of the | ||
| * Temporal service to be running. | ||
| * | ||
| * <p>Unlike regular Activities, a Standalone Activity is started directly from a Temporal Client | ||
| * using {@link ActivityClient}, not from inside a Workflow Definition. Writing the Activity and | ||
| * registering it with the Worker is identical in both cases. | ||
| */ | ||
| public class HelloStandaloneActivity { | ||
|
|
||
| static final String TASK_QUEUE = "HelloStandaloneActivityTaskQueue"; | ||
| static final String ACTIVITY_ID = "hello-standalone-activity-id"; | ||
|
|
||
| /** | ||
| * Activity interface. Writing a Standalone Activity is identical to writing an Activity | ||
| * orchestrated by a Workflow — the same Activity can be used for both. | ||
| * | ||
| * @see io.temporal.activity.ActivityInterface | ||
| * @see io.temporal.activity.ActivityMethod | ||
| */ | ||
| @ActivityInterface | ||
| public interface GreetingActivities { | ||
|
|
||
| // Define your activity method which can be called directly from a Temporal Client. | ||
| @ActivityMethod | ||
| String composeGreeting(String greeting, String name); | ||
| } | ||
|
|
||
| /** Simple activity implementation that concatenates two strings. */ | ||
| public static class GreetingActivitiesImpl implements GreetingActivities { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class); | ||
|
|
||
| @Override | ||
| public String composeGreeting(String greeting, String name) { | ||
| log.info("Composing greeting..."); | ||
| return greeting + ", " + name + "!"; | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| // Load configuration from environment and files. | ||
| ClientConfigProfile profile; | ||
| try { | ||
| profile = ClientConfigProfile.load(); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to load client configuration", e); | ||
| } | ||
|
|
||
| // gRPC stubs wrapper that talks to the temporal service. | ||
| WorkflowServiceStubs service = | ||
| WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions()); | ||
|
|
||
| // WorkflowClient is required to create a Worker. | ||
| WorkflowClient workflowClient = | ||
| WorkflowClient.newInstance(service, profile.toWorkflowClientOptions()); | ||
|
|
||
| // Worker factory that can be used to create workers for specific task queues. | ||
| WorkerFactory factory = WorkerFactory.newInstance(workflowClient); | ||
|
|
||
| // Worker that listens on a task queue and hosts activity implementations. | ||
| Worker worker = factory.newWorker(TASK_QUEUE); | ||
|
|
||
| // Activities are stateless and thread safe. So a shared instance is used. | ||
| worker.registerActivitiesImplementations(new GreetingActivitiesImpl()); | ||
|
|
||
| // Start listening to the activity task queue. | ||
| factory.start(); | ||
|
|
||
| // ActivityClient executes standalone activities directly from application code, | ||
| // without a Workflow. | ||
| ActivityClient client = | ||
| ActivityClient.newInstance( | ||
| service, | ||
| ActivityClientOptions.newBuilder().setNamespace(profile.getNamespace()).build()); | ||
|
|
||
| // Options specifying the activity ID, task queue, and timeout. | ||
| StartActivityOptions options = | ||
| StartActivityOptions.newBuilder() | ||
| .setId(ACTIVITY_ID) | ||
| .setTaskQueue(TASK_QUEUE) | ||
| .setStartToCloseTimeout(Duration.ofSeconds(10)) | ||
| .build(); | ||
|
|
||
| try { | ||
| // Execute the activity and wait for its result. The typed API uses an unbound method | ||
| // reference so the SDK can infer the activity type name and result type automatically. | ||
| String result = | ||
| client.execute( | ||
| GreetingActivities.class, | ||
| GreetingActivities::composeGreeting, | ||
| options, | ||
| "Hello", | ||
| "World"); | ||
|
|
||
| System.out.println(result); | ||
| } finally { | ||
| // Shut down the worker before the service so polling threads stop cleanly. | ||
| factory.shutdown(); | ||
| service.shutdown(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/src/main/java/io/temporal/samples/standaloneactivities/CountActivities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.temporal.samples.standaloneactivities; | ||
|
|
||
| import static io.temporal.samples.standaloneactivities.StandaloneActivityWorker.TASK_QUEUE; | ||
|
|
||
| import io.temporal.client.ActivityClient; | ||
| import io.temporal.client.ActivityClientOptions; | ||
| import io.temporal.client.ActivityExecutionCount; | ||
| import io.temporal.envconfig.ClientConfigProfile; | ||
| import io.temporal.serviceclient.WorkflowServiceStubs; | ||
| import java.io.IOException; | ||
|
|
||
| /** Counts standalone activity executions on the task queue. */ | ||
| public class CountActivities { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| ClientConfigProfile profile = ClientConfigProfile.load(); | ||
| WorkflowServiceStubs service = | ||
| WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions()); | ||
|
|
||
| ActivityClient client = | ||
| ActivityClient.newInstance( | ||
| service, | ||
| ActivityClientOptions.newBuilder().setNamespace(profile.getNamespace()).build()); | ||
|
|
||
| try { | ||
| ActivityExecutionCount resp = client.countExecutions("TaskQueue = '" + TASK_QUEUE + "'"); | ||
|
|
||
| System.out.println("Total activities: " + resp.getCount()); | ||
| resp.getGroups() | ||
| .forEach( | ||
| group -> | ||
| System.out.println("Group " + group.getGroupValues() + ": " + group.getCount())); | ||
| } finally { | ||
| service.shutdown(); | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
core/src/main/java/io/temporal/samples/standaloneactivities/ExecuteActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package io.temporal.samples.standaloneactivities; | ||
|
|
||
| import static io.temporal.samples.standaloneactivities.StandaloneActivityWorker.TASK_QUEUE; | ||
|
|
||
| import io.temporal.client.ActivityClient; | ||
| import io.temporal.client.ActivityClientOptions; | ||
| import io.temporal.client.StartActivityOptions; | ||
| import io.temporal.envconfig.ClientConfigProfile; | ||
| import io.temporal.serviceclient.WorkflowServiceStubs; | ||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
|
|
||
| /** | ||
| * Executes a standalone activity and waits for the result. Requires a Worker running | ||
| * StandaloneActivityWorker. | ||
| */ | ||
| public class ExecuteActivity { | ||
|
|
||
| static final String ACTIVITY_ID = "standalone-activity-id"; | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| ClientConfigProfile profile = ClientConfigProfile.load(); | ||
| WorkflowServiceStubs service = | ||
| WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions()); | ||
|
|
||
| ActivityClient client = | ||
| ActivityClient.newInstance( | ||
| service, | ||
| ActivityClientOptions.newBuilder().setNamespace(profile.getNamespace()).build()); | ||
|
|
||
| StartActivityOptions options = | ||
| StartActivityOptions.newBuilder() | ||
| .setId(ACTIVITY_ID) | ||
| .setTaskQueue(TASK_QUEUE) | ||
| .setStartToCloseTimeout(Duration.ofSeconds(10)) | ||
| .build(); | ||
|
|
||
| try { | ||
| String result = | ||
| client.execute( | ||
| GreetingActivities.class, | ||
| GreetingActivities::composeGreeting, | ||
| options, | ||
| "Hello", | ||
| "World"); | ||
| System.out.println("Activity result: " + result); | ||
| } finally { | ||
| service.shutdown(); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/io/temporal/samples/standaloneactivities/GreetingActivities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.temporal.samples.standaloneactivities; | ||
|
|
||
| import io.temporal.activity.ActivityInterface; | ||
| import io.temporal.activity.ActivityMethod; | ||
|
|
||
| /** Activity interface shared by all programs in this sample. */ | ||
| @ActivityInterface | ||
| public interface GreetingActivities { | ||
|
|
||
| @ActivityMethod | ||
| String composeGreeting(String greeting, String name); | ||
| } |
16 changes: 16 additions & 0 deletions
16
core/src/main/java/io/temporal/samples/standaloneactivities/GreetingActivitiesImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package io.temporal.samples.standaloneactivities; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Activity implementation. */ | ||
| public class GreetingActivitiesImpl implements GreetingActivities { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(GreetingActivitiesImpl.class); | ||
|
|
||
| @Override | ||
| public String composeGreeting(String greeting, String name) { | ||
| log.info("Composing greeting..."); | ||
| return greeting + ", " + name + "!"; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
core/src/main/java/io/temporal/samples/standaloneactivities/ListActivities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.temporal.samples.standaloneactivities; | ||
|
|
||
| import static io.temporal.samples.standaloneactivities.StandaloneActivityWorker.TASK_QUEUE; | ||
|
|
||
| import io.temporal.client.ActivityClient; | ||
| import io.temporal.client.ActivityClientOptions; | ||
| import io.temporal.client.ActivityExecutionMetadata; | ||
| import io.temporal.envconfig.ClientConfigProfile; | ||
| import io.temporal.serviceclient.WorkflowServiceStubs; | ||
| import java.io.IOException; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** Lists standalone activity executions on the task queue. */ | ||
| public class ListActivities { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| ClientConfigProfile profile = ClientConfigProfile.load(); | ||
| WorkflowServiceStubs service = | ||
| WorkflowServiceStubs.newServiceStubs(profile.toWorkflowServiceStubsOptions()); | ||
|
|
||
| ActivityClient client = | ||
| ActivityClient.newInstance( | ||
| service, | ||
| ActivityClientOptions.newBuilder().setNamespace(profile.getNamespace()).build()); | ||
|
|
||
| try (Stream<ActivityExecutionMetadata> activities = | ||
| client.listExecutions("TaskQueue = '" + TASK_QUEUE + "'")) { | ||
| activities.forEach( | ||
| info -> | ||
| System.out.printf( | ||
| "ActivityID: %s, Type: %s, Status: %s%n", | ||
| info.getActivityId(), info.getActivityType(), info.getStatus())); | ||
| } finally { | ||
| service.shutdown(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on #773, I believe so.