Skip to content
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

Implement activity helpers #353

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ This doc is intended for contributors to `cadence-java-client` (hopefully that's
## Development Environment

* Java 8.
* Gradle build tool
* Gradle build tool 4.5.1
* Docker
* Apache Thrift 0.9.3

## Licence headers

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/uber/cadence/activity/ActivityHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 com.uber.cadence.activity;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that this class is an activity helper. This annotation applies only to parent activity
* interfaces, where activity methods are defined. Not required. By default all methods owned by an
* Activity, are {@link ActivityMethod}, even if is not annotated. Use it to avoid this behavior and
* be able to define or implement some commons behavior that could be extended by an Activity Method
* interface.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ActivityHelper {}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.uber.cadence.PollForActivityTaskResponse;
import com.uber.cadence.RespondActivityTaskCompletedRequest;
import com.uber.cadence.RespondActivityTaskFailedRequest;
import com.uber.cadence.activity.ActivityHelper;
import com.uber.cadence.activity.ActivityMethod;
import com.uber.cadence.client.ActivityCancelledException;
import com.uber.cadence.common.MethodRetry;
Expand Down Expand Up @@ -92,9 +93,16 @@ private void addActivityImplementation(
if (i.getType().getTypeName().startsWith("org.mockito")) {
continue;
}

for (Method method : i.getRawType().getMethods()) {
ActivityMethod annotation = method.getAnnotation(ActivityMethod.class);
String activityType;

ActivityHelper helper = i.getRawType().getAnnotation(ActivityHelper.class);
if (helper != null) {
continue;
}

if (annotation != null && !annotation.name().isEmpty()) {
activityType = annotation.name();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.uber.cadence.WorkflowExecution;
import com.uber.cadence.WorkflowExecutionInfo;
import com.uber.cadence.activity.Activity;
import com.uber.cadence.activity.ActivityHelper;
import com.uber.cadence.activity.ActivityMethod;
import com.uber.cadence.activity.ActivityOptions;
import com.uber.cadence.client.WorkflowClient;
Expand Down Expand Up @@ -151,6 +152,53 @@ public void testFailure() {
}
}

@ActivityHelper
public interface TestParentActivity {
String execute(String input);
}

public interface TestChild1Activity extends TestParentActivity {

@ActivityMethod(scheduleToCloseTimeoutSeconds = 3600)
@Override
String execute(String input);
}

public interface TestChild2Activity extends TestParentActivity {

@ActivityMethod(scheduleToCloseTimeoutSeconds = 3600)
@Override
String execute(String input);
}

private static class ActivityChild1Impl implements TestChild1Activity {

@Override
public String execute(String input) {
return Activity.getTask().getActivityType() + "-" + input;
}
}

private static class ActivityChild2Impl implements TestChild2Activity {

@Override
public String execute(String input) {
return Activity.getTask().getActivityType() + "-" + input;
}
}

@Test
public void testChildActivity() {
Worker worker = testEnvironment.newWorker(TASK_LIST);
worker.registerWorkflowImplementationTypes(ChildActivityWorkflow.class);
worker.registerActivitiesImplementations(new ActivityChild1Impl(), new ActivityChild2Impl());
testEnvironment.start();
WorkflowClient client = testEnvironment.newWorkflowClient();
TestWorkflow workflow = client.newWorkflowStub(TestWorkflow.class);
String result = workflow.workflow1("input1");
assertEquals("TestChild1Activity::execute-input1-TestChild2Activity::execute-input1", result);
}

public interface TestActivity {

@ActivityMethod(scheduleToCloseTimeoutSeconds = 3600)
Expand All @@ -176,6 +224,18 @@ public String workflow1(String input) {
}
}

public static class ChildActivityWorkflow implements TestWorkflow {

private final TestChild1Activity activity1 = Workflow.newActivityStub(TestChild1Activity.class);
private final TestChild2Activity activity2 = Workflow.newActivityStub(TestChild2Activity.class);

@Override
public String workflow1(String input) {
Workflow.sleep(Duration.ofHours(1)); // test time skipping
return activity1.execute(input) + "-" + activity2.execute(input);
}
}

@Test
public void testActivity() {
Worker worker = testEnvironment.newWorker(TASK_LIST);
Expand Down