From b858441918900eb9a7b2de2ee3f30d5cc8279479 Mon Sep 17 00:00:00 2001 From: Miguel Prieto Date: Mon, 14 Oct 2024 13:42:45 -0300 Subject: [PATCH] Added very simple examples used in "Getting Started" --- .../conductor-client/README.md | 4 +- .../gettingstarted/CreateWorkflow.java | 50 +++++++++++++++++++ .../conductor/gettingstarted/HelloWorker.java | 36 +++++++++++++ .../gettingstarted/StartWorkflow.java | 18 +++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/CreateWorkflow.java create mode 100644 conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/HelloWorker.java create mode 100644 conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/StartWorkflow.java diff --git a/conductor-clients/java/conductor-java-sdk/conductor-client/README.md b/conductor-clients/java/conductor-java-sdk/conductor-client/README.md index 38aefa6fc..18be60b21 100644 --- a/conductor-clients/java/conductor-java-sdk/conductor-client/README.md +++ b/conductor-clients/java/conductor-java-sdk/conductor-client/README.md @@ -87,4 +87,6 @@ public class HelloWorker implements Worker { runnerConfigurer.init(); } } -``` \ No newline at end of file +``` + +> **Note:** The full code for the above examples can be found [here](../examples/src/main/java/com/netflix/conductor/gettingstarted). diff --git a/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/CreateWorkflow.java b/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/CreateWorkflow.java new file mode 100644 index 000000000..a65254f6e --- /dev/null +++ b/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/CreateWorkflow.java @@ -0,0 +1,50 @@ +package com.netflix.conductor.gettingstarted; + +import com.netflix.conductor.sdk.workflow.def.WorkflowBuilder; +import com.netflix.conductor.sdk.workflow.def.tasks.SimpleTask; +import com.netflix.conductor.sdk.workflow.executor.WorkflowExecutor; + +/** + * Very Simple Workflow As Code Example. + * + * You can use the following code to register this "hello_workflow" workflow. + * + * { + * "name": "hello_workflow", + * "description": "Hello Workflow!", + * "version": 1, + * "tasks": [ + * { + * "name": "hello_task", + * "taskReferenceName": "hello_task_ref", + * "type": "SIMPLE", + * "inputParameters": {} + * } + * ], + * "inputParameters": [], + * "outputParameters": { + * + * }, + * "schemaVersion": 2, + * "restartable": true, + * "workflowStatusListenerEnabled": false, + * "ownerEmail": "example@orkes.io", + * "timeoutPolicy": "ALERT_ONLY", + * "timeoutSeconds": 0 + * } + */ +public class CreateWorkflow { + + public static void main(String[] args) { + var executor = new WorkflowExecutor("http://localhost:8080/api"); + var workflow = new WorkflowBuilder(executor) + .name("hello_workflow") + .version(1) + .description("Hello Workflow!") + .ownerEmail("examples@orkes.io") + .add(new SimpleTask("hello_task", "hello_task_ref")) + .build(); + workflow.registerWorkflow(true, true); + executor.shutdown(); + } +} diff --git a/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/HelloWorker.java b/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/HelloWorker.java new file mode 100644 index 000000000..27b5ce446 --- /dev/null +++ b/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/HelloWorker.java @@ -0,0 +1,36 @@ +package com.netflix.conductor.gettingstarted; + +import com.netflix.conductor.client.automator.TaskRunnerConfigurer; +import com.netflix.conductor.client.http.ConductorClient; +import com.netflix.conductor.client.http.TaskClient; +import com.netflix.conductor.client.worker.Worker; +import com.netflix.conductor.common.metadata.tasks.Task; +import com.netflix.conductor.common.metadata.tasks.TaskResult; + +import java.util.List; + +public class HelloWorker implements Worker { + + @Override + public TaskResult execute(Task task) { + var taskResult = new TaskResult(task); + taskResult.setStatus(TaskResult.Status.COMPLETED); + taskResult.getOutputData().put("message", "Hello World!"); + return taskResult; + } + + @Override + public String getTaskDefName() { + return "hello_task"; + } + + public static void main(String[] args) { + var client = new ConductorClient("http://localhost:8080/api"); + var taskClient = new TaskClient(client); + var runnerConfigurer = new TaskRunnerConfigurer + .Builder(taskClient, List.of(new HelloWorker())) + .withThreadCount(10) + .build(); + runnerConfigurer.init(); + } +} diff --git a/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/StartWorkflow.java b/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/StartWorkflow.java new file mode 100644 index 000000000..6c53e44ca --- /dev/null +++ b/conductor-clients/java/conductor-java-sdk/examples/src/main/java/com/netflix/conductor/gettingstarted/StartWorkflow.java @@ -0,0 +1,18 @@ +package com.netflix.conductor.gettingstarted; + +import com.netflix.conductor.client.http.ConductorClient; +import com.netflix.conductor.client.http.WorkflowClient; +import com.netflix.conductor.common.metadata.workflow.StartWorkflowRequest; + +public class StartWorkflow { + + public static void main(String[] args) { + var client = new ConductorClient("http://localhost:8080/api"); + var workflowClient = new WorkflowClient(client); + var workflowId = workflowClient.startWorkflow(new StartWorkflowRequest() + .withName("hello_workflow") + .withVersion(1)); + + System.out.println("Started workflow " + workflowId); + } +}