diff --git a/core/src/main/java/io/temporal/samples/basic/Makefile b/core/src/main/java/io/temporal/samples/basic/Makefile new file mode 100644 index 000000000..2c8535ace --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/Makefile @@ -0,0 +1,9 @@ +worker: + ls *.java | entr -r \ + sh -c 'cd $$(git rev-parse --show-toplevel) && ./gradlew -q execute -P mainClass=io.temporal.samples.basic.MyWorker' + +run: + cd $$(git rev-parse --show-toplevel) && \ + ./gradlew -q execute -P mainClass=io.temporal.samples.basic.MyStarter + +.PHONY: run worker diff --git a/core/src/main/java/io/temporal/samples/basic/MyActivity.java b/core/src/main/java/io/temporal/samples/basic/MyActivity.java new file mode 100644 index 000000000..a1d6709d4 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/MyActivity.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved + * + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.temporal.samples.basic; + +import io.temporal.activity.ActivityInterface; +import io.temporal.activity.ActivityMethod; + +@ActivityInterface +public interface MyActivity { + + @ActivityMethod + int myActivityMethod(); +} diff --git a/core/src/main/java/io/temporal/samples/basic/MyActivityImpl.java b/core/src/main/java/io/temporal/samples/basic/MyActivityImpl.java new file mode 100644 index 000000000..9ba8e9bcf --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/MyActivityImpl.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved + * + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.temporal.samples.basic; + +public class MyActivityImpl implements MyActivity { + @Override + public int myActivityMethod() { + return 7; + } +} diff --git a/core/src/main/java/io/temporal/samples/basic/MyStarter.java b/core/src/main/java/io/temporal/samples/basic/MyStarter.java new file mode 100644 index 000000000..c6b00acc9 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/MyStarter.java @@ -0,0 +1,34 @@ +package io.temporal.samples.basic; + +import io.temporal.api.enums.v1.WorkflowIdReusePolicy; +import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowOptions; +import io.temporal.serviceclient.WorkflowServiceStubs; + +public class MyStarter { + + static final String WORKFLOW_ID = "wid"; + + public static void main(String[] args) throws Exception { + + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkflowOptions workflowOptions = + WorkflowOptions.newBuilder() + .setTaskQueue(MyWorker.TASK_QUEUE) + .setWorkflowId(WORKFLOW_ID) + .setWorkflowIdReusePolicy( + WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_TERMINATE_IF_RUNNING) + .setWorkflowRunTimeout(java.time.Duration.ofSeconds(2)) + .build(); + MyWorkflow workflow = client.newWorkflowStub(MyWorkflow.class, workflowOptions); + + WorkflowClient.start(workflow::run); + + int result = workflow.myUpdate(); + System.out.println(result); + System.exit(0); + } +} diff --git a/core/src/main/java/io/temporal/samples/basic/MyWorker.java b/core/src/main/java/io/temporal/samples/basic/MyWorker.java new file mode 100644 index 000000000..e30be7173 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/MyWorker.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved + * + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.temporal.samples.basic; + +import io.temporal.client.WorkflowClient; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerFactory; + +public class MyWorker { + static final String TASK_QUEUE = "tq"; + + public static void main(String[] args) throws Exception { + + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(TASK_QUEUE); + worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class); + worker.registerActivitiesImplementations(new MyActivityImpl()); + factory.start(); + } +} diff --git a/core/src/main/java/io/temporal/samples/basic/MyWorkflow.java b/core/src/main/java/io/temporal/samples/basic/MyWorkflow.java new file mode 100644 index 000000000..15bed82cf --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/MyWorkflow.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved + * + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.temporal.samples.basic; + +import io.temporal.workflow.UpdateMethod; +import io.temporal.workflow.WorkflowInterface; +import io.temporal.workflow.WorkflowMethod; + +@WorkflowInterface +public interface MyWorkflow { + @WorkflowMethod + void run(); + + @UpdateMethod + int myUpdate(); +} diff --git a/core/src/main/java/io/temporal/samples/basic/MyWorkflowImpl.java b/core/src/main/java/io/temporal/samples/basic/MyWorkflowImpl.java new file mode 100644 index 000000000..9d04f3925 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/basic/MyWorkflowImpl.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved + * + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package io.temporal.samples.basic; + +import io.temporal.activity.ActivityOptions; +import io.temporal.common.RetryOptions; +import io.temporal.workflow.Workflow; +import java.time.Duration; + +public class MyWorkflowImpl implements MyWorkflow { + + private final MyActivity activity = + Workflow.newActivityStub( + MyActivity.class, + ActivityOptions.newBuilder() + .setStartToCloseTimeout(Duration.ofSeconds(2)) + .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) + .build()); + + private boolean done = false; + + @Override + public void run() { + Workflow.await(() -> done); + } + + @Override + public int myUpdate() { + Workflow.sleep(Duration.ofSeconds(4)); + int result = activity.myActivityMethod(); + done = true; + return result; + } +} diff --git a/core/src/main/java/io/temporal/samples/hello/Scratchpad.java b/core/src/main/java/io/temporal/samples/hello/Scratchpad.java new file mode 100644 index 000000000..95c06de1c --- /dev/null +++ b/core/src/main/java/io/temporal/samples/hello/Scratchpad.java @@ -0,0 +1,74 @@ +package io.temporal.samples.hello; + +import io.temporal.activity.ActivityInterface; +import io.temporal.activity.ActivityOptions; +import io.temporal.api.enums.v1.WorkflowIdReusePolicy; +import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowOptions; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerFactory; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.WorkflowInterface; +import io.temporal.workflow.WorkflowMethod; +import java.time.Duration; + +public class Scratchpad { + static final String TASK_QUEUE = "ScratchpadTaskQueue"; + static final String WORKFLOW_ID = "ScratchpadWorkflow"; + + @WorkflowInterface + public interface MyWorkflow { + @WorkflowMethod + String getGreeting(String name); + } + + @ActivityInterface + public interface MyActivities { + String composeGreeting(String greeting, String name); + } + + public static class MyWorkflowImpl implements MyWorkflow { + + private final MyActivities activities = + Workflow.newActivityStub( + MyActivities.class, + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build()); + + @Override + public String getGreeting(String name) { + return activities.composeGreeting("Hello", name); + } + } + + static class MyActivitiesImpl implements MyActivities { + @Override + public String composeGreeting(String greeting, String name) { + return greeting + " " + name + "!"; + } + } + + public static void main(String[] args) { + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + WorkflowClient client = WorkflowClient.newInstance(service); + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(TASK_QUEUE); + worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class); + worker.registerActivitiesImplementations(new MyActivitiesImpl()); + factory.start(); + MyWorkflow workflow = + client.newWorkflowStub( + MyWorkflow.class, + WorkflowOptions.newBuilder() + .setWorkflowId(WORKFLOW_ID) + .setTaskQueue(TASK_QUEUE) + .setWorkflowIdReusePolicy( + WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_TERMINATE_IF_RUNNING) + .build()); + + String greeting = workflow.getGreeting("World"); + + System.out.println(greeting); + System.exit(0); + } +} diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/Makefile b/core/src/main/java/io/temporal/samples/workflowtimeout/Makefile new file mode 100644 index 000000000..2c8535ace --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/Makefile @@ -0,0 +1,9 @@ +worker: + ls *.java | entr -r \ + sh -c 'cd $$(git rev-parse --show-toplevel) && ./gradlew -q execute -P mainClass=io.temporal.samples.basic.MyWorker' + +run: + cd $$(git rev-parse --show-toplevel) && \ + ./gradlew -q execute -P mainClass=io.temporal.samples.basic.MyStarter + +.PHONY: run worker diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/MyActivity.java b/core/src/main/java/io/temporal/samples/workflowtimeout/MyActivity.java new file mode 100644 index 000000000..05ce12c83 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/MyActivity.java @@ -0,0 +1,11 @@ +package io.temporal.samples.workflowtimeout; + +import io.temporal.activity.ActivityInterface; +import io.temporal.activity.ActivityMethod; + +@ActivityInterface +public interface MyActivity { + + @ActivityMethod + int myActivityMethod(); +} diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/MyActivityImpl.java b/core/src/main/java/io/temporal/samples/workflowtimeout/MyActivityImpl.java new file mode 100644 index 000000000..0a66ea77d --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/MyActivityImpl.java @@ -0,0 +1,8 @@ +package io.temporal.samples.workflowtimeout; + +public class MyActivityImpl implements MyActivity { + @Override + public int myActivityMethod() { + return 7; + } +} diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/MyStarter.java b/core/src/main/java/io/temporal/samples/workflowtimeout/MyStarter.java new file mode 100644 index 000000000..f71148790 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/MyStarter.java @@ -0,0 +1,38 @@ +package io.temporal.samples.workflowtimeout; + +import io.temporal.api.enums.v1.WorkflowIdConflictPolicy; +import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowOptions; +import io.temporal.serviceclient.WorkflowServiceStubs; + +public class MyStarter { + + static final String WORKFLOW_ID = "wid"; + + public static void main(String[] args) throws Exception { + + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkflowOptions workflowOptions = + WorkflowOptions.newBuilder() + .setTaskQueue(MyWorker.TASK_QUEUE) + .setWorkflowId(WORKFLOW_ID) + .setWorkflowIdConflictPolicy( + WorkflowIdConflictPolicy.WORKFLOW_ID_CONFLICT_POLICY_TERMINATE_EXISTING) + .setWorkflowExecutionTimeout(java.time.Duration.ofSeconds(2)) + .build(); + MyWorkflow workflow = client.newWorkflowStub(MyWorkflow.class, workflowOptions); + + WorkflowClient.start(workflow::run); + + int upResult = workflow.myUpdate(); + System.out.println("upResult: " + upResult); + + int wfResult = workflow.run(); + System.out.println("wfResult: " + wfResult); + + System.exit(0); + } +} diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorker.java b/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorker.java new file mode 100644 index 000000000..0c20a264d --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorker.java @@ -0,0 +1,23 @@ +package io.temporal.samples.workflowtimeout; + +import io.temporal.client.WorkflowClient; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerFactory; + +public class MyWorker { + static final String TASK_QUEUE = "tq"; + + public static void main(String[] args) throws Exception { + + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(TASK_QUEUE); + worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class); + worker.registerActivitiesImplementations(new MyActivityImpl()); + factory.start(); + } +} diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorkflow.java b/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorkflow.java new file mode 100644 index 000000000..b1ab8a990 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorkflow.java @@ -0,0 +1,14 @@ +package io.temporal.samples.workflowtimeout; + +import io.temporal.workflow.UpdateMethod; +import io.temporal.workflow.WorkflowInterface; +import io.temporal.workflow.WorkflowMethod; + +@WorkflowInterface +public interface MyWorkflow { + @WorkflowMethod + int run(); + + @UpdateMethod + int myUpdate(); +} diff --git a/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorkflowImpl.java b/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorkflowImpl.java new file mode 100644 index 000000000..db43b55a7 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workflowtimeout/MyWorkflowImpl.java @@ -0,0 +1,34 @@ +package io.temporal.samples.workflowtimeout; + +import io.temporal.activity.ActivityOptions; +import io.temporal.common.RetryOptions; +import io.temporal.workflow.Workflow; +import java.time.Duration; + +public class MyWorkflowImpl implements MyWorkflow { + + private final MyActivity activity = + Workflow.newActivityStub( + MyActivity.class, + ActivityOptions.newBuilder() + .setStartToCloseTimeout(Duration.ofSeconds(2)) + .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) + .build()); + + // private boolean done; + + @Override + public int run() { + Workflow.sleep(Duration.ofSeconds(3)); + // Workflow.await(() -> done); + return 8; + } + + @Override + public int myUpdate() { + Workflow.sleep(Duration.ofSeconds(2)); + int result = activity.myActivityMethod(); + // done = true; + return result; + } +}