Skip to content

Commit

Permalink
Add ActivityMethod fields back for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Liang Mei committed Oct 29, 2020
1 parent b4db385 commit 4a32bb1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
* }
* </code></pre>
*
* When <code>CImpl</code> instance is registered with the {@link com.uber.cadence.worker.Worker} the
* following activities are registered:
* When <code>CImpl</code> instance is registered with the {@link com.uber.cadence.worker.Worker}
* the following activities are registered:
*
* <p>
*
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/uber/cadence/activity/ActivityMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Duration;

/**
* Indicates that the method is an activity method. This annotation applies only to activity
Expand All @@ -32,4 +33,46 @@

/** Name of the workflow type. Default is {short class name}::{method name} */
String name() default "";

/**
* Overall timeout workflow is willing to wait for activity to complete. It includes time in a
* task list (use {@link #scheduleToStartTimeoutSeconds()} to limit it) plus activity execution
* time (use {@link #startToCloseTimeoutSeconds()} to limit it). Either this option or both
* schedule to start and start to close are required.
*
* @deprecated use {@link ActivityOptions.Builder#setScheduleToCloseTimeout(Duration)} instead.
*/
int scheduleToCloseTimeoutSeconds() default 0;

/**
* Time activity can stay in task list before it is picked up by a worker. If schedule to close is
* not provided then both this and start to close are required.
*
* @deprecated use {@link ActivityOptions.Builder#setScheduleToStartTimeout(Duration)} instead.
*/
int scheduleToStartTimeoutSeconds() default 0;

/**
* Maximum activity execution time after it was sent to a worker. If schedule to close is not
* provided then both this and schedule to start are required.
*
* @deprecated use {@link ActivityOptions.Builder#setStartToCloseTimeout(Duration)} instead.
*/
int startToCloseTimeoutSeconds() default 0;

/**
* Heartbeat interval. Activity must heartbeat before this interval passes after a last heartbeat
* or activity start.
*
* @deprecated use {@link ActivityOptions.Builder#setHeartbeatTimeout(Duration)} instead.
*/
int heartbeatTimeoutSeconds() default 0;

/**
* Task list to use when dispatching activity task to a worker. By default it is the same task
* list name the workflow was started with.
*
* @deprecated use {@link ActivityOptions.Builder#setTaskList(String)} instead.
*/
String taskList() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

package com.uber.cadence.internal.sync;

import com.google.common.base.Strings;
import com.uber.cadence.activity.ActivityMethod;
import com.uber.cadence.activity.ActivityOptions;
import com.uber.cadence.common.MethodRetry;
import com.uber.cadence.workflow.ActivityStub;
import com.uber.cadence.workflow.WorkflowInterceptor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.function.Function;

class ActivityInvocationHandler extends ActivityInvocationHandlerBase {
Expand All @@ -41,12 +44,37 @@ private ActivityInvocationHandler(
init(activityInterface);
}

@SuppressWarnings("deprecation")
@Override
protected Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, String activityName) {
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName) {
Function<Object[], Object> function;
ActivityOptions mergedOptions =
ActivityOptions.newBuilder(options).setMethodRetry(methodRetry).build();
ActivityOptions.Builder optionsBuilder =
ActivityOptions.newBuilder(options).setMethodRetry(methodRetry);
if (activityMethod != null) {
// options always take precedence over activity method annotation.
if (options.getStartToCloseTimeout() == null) {
optionsBuilder.setStartToCloseTimeout(
Duration.ofSeconds(activityMethod.startToCloseTimeoutSeconds()));
}
if (options.getScheduleToStartTimeout() == null) {
optionsBuilder.setScheduleToStartTimeout(
Duration.ofSeconds(activityMethod.scheduleToStartTimeoutSeconds()));
}
if (options.getScheduleToCloseTimeout() == null) {
optionsBuilder.setScheduleToCloseTimeout(
Duration.ofSeconds(activityMethod.scheduleToCloseTimeoutSeconds()));
}
if (options.getHeartbeatTimeout() == null) {
optionsBuilder.setHeartbeatTimeout(
Duration.ofSeconds(activityMethod.heartbeatTimeoutSeconds()));
}
if (Strings.isNullOrEmpty(options.getTaskList())
&& !Strings.isNullOrEmpty(activityMethod.taskList())) {
optionsBuilder.setTaskList(activityMethod.taskList());
}
}
ActivityOptions mergedOptions = optionsBuilder.build();
ActivityStub stub = ActivityStubImpl.newInstance(mergedOptions, activityExecutor);

function =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static <T> T newProxy(Class<T> activityInterface, InvocationHandler invocationHa
invocationHandler);
}

protected void init(Class<?> activityInterface) {
void init(Class<?> activityInterface) {
Set<POJOActivityTaskHandler.MethodInterfacePair> activityMethods =
getAnnotatedInterfaceMethodsFromInterface(activityInterface, ActivityInterface.class);
if (activityMethods.isEmpty()) {
Expand All @@ -66,7 +66,8 @@ protected void init(Class<?> activityInterface) {
}

MethodRetry methodRetry = method.getAnnotation(MethodRetry.class);
Function<Object[], Object> function = getActivityFunc(method, methodRetry, activityType);
Function<Object[], Object> function =
getActivityFunc(method, methodRetry, activityMethod, activityType);
methodFunctions.put(method, function);
}
}
Expand All @@ -81,5 +82,5 @@ public Object invoke(Object proxy, Method method, Object[] args) {
}

protected abstract Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, String activityName);
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package com.uber.cadence.internal.sync;

import com.uber.cadence.activity.ActivityMethod;
import com.uber.cadence.activity.LocalActivityOptions;
import com.uber.cadence.common.MethodRetry;
import com.uber.cadence.workflow.ActivityStub;
import com.uber.cadence.workflow.WorkflowInterceptor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.function.Function;

class LocalActivityInvocationHandler extends ActivityInvocationHandlerBase {
Expand All @@ -45,14 +47,21 @@ private LocalActivityInvocationHandler(
init(activityInterface);
}

@SuppressWarnings("deprecation")
@Override
protected Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, String activityName) {
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName) {
Function<Object[], Object> function;
LocalActivityOptions mergedOptions =
LocalActivityOptions.newBuilder(options)
.setMethodRetry(methodRetry)
.validateAndBuildWithDefaults();
LocalActivityOptions.Builder optionsBuilder =
LocalActivityOptions.newBuilder(options).setMethodRetry(methodRetry);
if (activityMethod != null) {
// options always take precedence over activity method annotation.
if (options.getScheduleToCloseTimeout() == null) {
optionsBuilder.setScheduleToCloseTimeout(
Duration.ofSeconds(activityMethod.scheduleToCloseTimeoutSeconds()));
}
}
LocalActivityOptions mergedOptions = optionsBuilder.validateAndBuildWithDefaults();
ActivityStub stub = LocalActivityStubImpl.newInstance(mergedOptions, activityExecutor);
function =
(a) -> stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void activityAndRetryOptions() {}
@Test
public void testOnlyAnnotationsPresent() throws NoSuchMethodException {
Method method = ActivityOptionsTest.class.getMethod("activityAndRetryOptions");
ActivityMethod a = method.getAnnotation(ActivityMethod.class);
MethodRetry r = method.getAnnotation(MethodRetry.class);
ActivityOptions o = ActivityOptions.newBuilder().build();
ActivityOptions merged = ActivityOptions.newBuilder(o).setMethodRetry(r).build();
Expand Down

0 comments on commit 4a32bb1

Please sign in to comment.