Skip to content

Added @ActivityInterface #56

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

Merged
merged 5 commits into from
Apr 4, 2020
Merged
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
78 changes: 78 additions & 0 deletions src/main/java/io/temporal/activity/ActivityInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.activity;

import io.temporal.workflow.Workflow;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that the interface is an activity interface. Only interfaces annotated with this
* annotation can be used as parameters to {@link Workflow#newActivityStub(Class)} methods.
*
* <p>Each method of the interface annotated with <code>ActivityInterface</code> including inherited
* from interfaces is a separate activity. By default the name of an activity type is "short
* interface name"_"method name".
*
* <p>Example:
*
* <pre><code>
* public interface A {
* a();
* }
*
* {@literal @}ActivityInterface
* public interface B extends A {
* b();
* }
*
* {@literal @}ActivityInterface
* public interface C extends B {
* c();
* }
*
* public class CImpl implements C {
* public void a() {}
* public void b() {}
* public void c() {}
* }
* </code></pre>
*
* When <code>CImpl</code> instance is registered with the {@link io.temporal.worker.Worker} the
* following activities are registered:
*
* <p>
*
* <ul>
* <li>B_a
* <li>B_b
* <li>C_c
* </ul>
*
* Note that method <code>a()</code> is registered as "B_a" because interface <code>A</code> lacks
* ActivityInterface annotation. The workflow code can call activities through stubs to <code>B
* </code> and <code>C</code> interfaces. A call to crate stub to <code>A</code> interface will fail
* as <code>A</code> is not annotated with ActivityInterface.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ActivityInterface {}
36 changes: 1 addition & 35 deletions src/main/java/io/temporal/activity/ActivityMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,12 @@

/**
* Indicates that the method is an activity method. This annotation applies only to activity
* interface methods. Not required. Use it to override default activity type name or other options.
* When both {@link ActivityOptions} and {@link ActivityMethod} have non default value for some
* parameter the {@link ActivityOptions} one takes precedence.
* interface methods. Not required. Use it to override default activity type name.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ActivityMethod {

/** 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.
*/
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.
*/
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.
*/
int startToCloseTimeoutSeconds() default 0;

/**
* Heartbeat interval. Activity must heartbeat before this interval passes after a last heartbeat
* or activity start.
*/
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.
*/
String taskList() default "";
}
17 changes: 0 additions & 17 deletions src/main/java/io/temporal/activity/ActivityOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,6 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
return this;
}

/**
* Properties that are set on this builder take precedence over ones found in the annotation.
*/
public Builder setActivityMethod(ActivityMethod a) {
if (a == null) {
return this;
}
scheduleToCloseTimeout =
mergeDuration(a.scheduleToCloseTimeoutSeconds(), scheduleToCloseTimeout);
scheduleToStartTimeout =
mergeDuration(a.scheduleToStartTimeoutSeconds(), scheduleToStartTimeout);
startToCloseTimeout = mergeDuration(a.startToCloseTimeoutSeconds(), startToCloseTimeout);
heartbeatTimeout = mergeDuration(a.heartbeatTimeoutSeconds(), heartbeatTimeout);
taskList = taskList != null ? taskList : (a.taskList().isEmpty() ? null : a.taskList());
return this;
}

/**
* Properties that are set on this builder take precedence over ones found in the annotation.
*/
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/io/temporal/activity/LocalActivityOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,6 @@ public Builder setRetryOptions(RetryOptions retryOptions) {
return this;
}

/**
* Merges ActivityMethod annotation. The values of this builder take precedence over annotation
* ones.
*/
public Builder setActivityMethod(ActivityMethod a) {
if (a != null) {
this.scheduleToCloseTimeout =
ActivityOptions.mergeDuration(
a.scheduleToCloseTimeoutSeconds(), scheduleToCloseTimeout);
}
return this;
}

/**
* Merges MethodRetry annotation. The values of this builder take precedence over annotation
* ones.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public final class InternalUtils {
* @return "Simple class name"_"methodName"
*/
public static String getSimpleName(Method method) {
return method.getDeclaringClass().getSimpleName() + "_" + method.getName();
return getSimpleName(method.getDeclaringClass(), method);
}

public static String getSimpleName(Class<?> type, Method method) {
return type.getSimpleName() + "_" + method.getName();
}

public static String getWorkflowType(Method method, WorkflowMethod workflowMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package io.temporal.internal.sync;

import io.temporal.activity.ActivityMethod;
import io.temporal.activity.ActivityOptions;
import io.temporal.common.MethodRetry;
import io.temporal.common.interceptors.WorkflowCallsInterceptor;
Expand All @@ -33,25 +32,27 @@ class ActivityInvocationHandler extends ActivityInvocationHandlerBase {
private final WorkflowCallsInterceptor activityExecutor;

static InvocationHandler newInstance(
ActivityOptions options, WorkflowCallsInterceptor activityExecutor) {
return new ActivityInvocationHandler(options, activityExecutor);
Class<?> activityInterface,
ActivityOptions options,
WorkflowCallsInterceptor activityExecutor) {
return new ActivityInvocationHandler(activityInterface, activityExecutor, options);
}

private ActivityInvocationHandler(
ActivityOptions options, WorkflowCallsInterceptor activityExecutor) {
Class<?> activityInterface,
WorkflowCallsInterceptor activityExecutor,
ActivityOptions options) {
this.options = options;
this.activityExecutor = activityExecutor;
init(activityInterface);
}

@Override
protected Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName) {
Method method, MethodRetry methodRetry, String activityName) {
Function<Object[], Object> function;
ActivityOptions mergedOptions =
ActivityOptions.newBuilder(options)
.setActivityMethod(activityMethod)
.setMethodRetry(methodRetry)
.build();
ActivityOptions.newBuilder(options).setMethodRetry(methodRetry).build();
ActivityStub stub = ActivityStubImpl.newInstance(mergedOptions, activityExecutor);

function =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
package io.temporal.internal.sync;

import static io.temporal.internal.common.InternalUtils.getValueOrDefault;
import static io.temporal.internal.sync.POJOActivityTaskHandler.getAnnotatedInterfaceMethodsFromInterface;

import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityMethod;
import io.temporal.common.MethodRetry;
import io.temporal.internal.common.InternalUtils;
import io.temporal.internal.sync.AsyncInternal.AsyncMarker;
import io.temporal.workflow.Workflow;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/** Dynamic implementation of a strongly typed activity interface. */
Expand All @@ -47,37 +49,39 @@ static <T> T newProxy(Class<T> activityInterface, InvocationHandler invocationHa
invocationHandler);
}

protected void init(Class<?> activityInterface) {
Set<POJOActivityTaskHandler.MethodInterfacePair> activityMethods =
getAnnotatedInterfaceMethodsFromInterface(activityInterface, ActivityInterface.class);
if (activityMethods.isEmpty()) {
throw new IllegalArgumentException(
"Class doesn't implement any non empty interface annotated with @ActivityInterface: "
+ activityInterface.getName());
}
for (POJOActivityTaskHandler.MethodInterfacePair pair : activityMethods) {
Method method = pair.getMethod();
ActivityMethod activityMethod = method.getAnnotation(ActivityMethod.class);
String activityType;
if (activityMethod != null && !activityMethod.name().isEmpty()) {
activityType = activityMethod.name();
} else {
activityType = InternalUtils.getSimpleName(pair.getType(), method);
}

MethodRetry methodRetry = method.getAnnotation(MethodRetry.class);
Function<Object[], Object> function = getActivityFunc(method, methodRetry, activityType);
methodFunctions.put(method, function);
}
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
Function<Object[], Object> function = methodFunctions.get(method);
if (function == null) {
try {
if (method.equals(Object.class.getMethod("toString"))) {
// TODO: activity info
return "ActivityInvocationHandlerBase";
}
if (!method.getDeclaringClass().isInterface()) {
throw new IllegalArgumentException(
"Interface type is expected: " + method.getDeclaringClass());
}
MethodRetry methodRetry = method.getAnnotation(MethodRetry.class);
ActivityMethod activityMethod = method.getAnnotation(ActivityMethod.class);
String activityName;
if (activityMethod == null || activityMethod.name().isEmpty()) {
activityName = InternalUtils.getSimpleName(method);
} else {
activityName = activityMethod.name();
}

function = getActivityFunc(method, methodRetry, activityMethod, activityName);
methodFunctions.put(method, function);
} catch (NoSuchMethodException e) {
throw Workflow.wrap(e);
}
throw new IllegalArgumentException("Unexpected method: " + method);
}
return getValueOrDefault(function.apply(args), method.getReturnType());
}

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

package io.temporal.internal.sync;

import io.temporal.activity.ActivityMethod;
import io.temporal.activity.LocalActivityOptions;
import io.temporal.common.MethodRetry;
import io.temporal.common.interceptors.WorkflowCallsInterceptor;
Expand All @@ -33,23 +32,27 @@ class LocalActivityInvocationHandler extends ActivityInvocationHandlerBase {
private final WorkflowCallsInterceptor activityExecutor;

static InvocationHandler newInstance(
LocalActivityOptions options, WorkflowCallsInterceptor activityExecutor) {
return new LocalActivityInvocationHandler(options, activityExecutor);
Class<?> activityInterface,
LocalActivityOptions options,
WorkflowCallsInterceptor activityExecutor) {
return new LocalActivityInvocationHandler(activityInterface, activityExecutor, options);
}

private LocalActivityInvocationHandler(
LocalActivityOptions options, WorkflowCallsInterceptor activityExecutor) {
Class<?> activityInterface,
WorkflowCallsInterceptor activityExecutor,
LocalActivityOptions options) {
this.options = options;
this.activityExecutor = activityExecutor;
init(activityInterface);
}

@Override
protected Function<Object[], Object> getActivityFunc(
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName) {
Method method, MethodRetry methodRetry, String activityName) {
Function<Object[], Object> function;
LocalActivityOptions mergedOptions =
LocalActivityOptions.newBuilder(options)
.setActivityMethod(activityMethod)
.setMethodRetry(methodRetry)
.validateAndBuildWithDefaults();
ActivityStub stub = LocalActivityStubImpl.newInstance(mergedOptions, activityExecutor);
Expand Down
Loading