8000 Added @ActivityInterface by mfateev · Pull Request #56 · temporalio/sdk-java · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
Added ActivityInterface. Removed timeout properties from ActivityMethod
  • Loading branch information
mfateev committed Apr 1, 2020
commit b9346e6222fe8a53b5712aa450302d5fe160f248
38 changes: 38 additions & 0 deletions src/main/java/io/temporal/activity/ActivityInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 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
* io.temporal.workflow.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".
*/
@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 @@ -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 @@ -45,13 +44,10 @@ private ActivityInvocationHandler(

@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 @@ -69,7 +69,7 @@ public Object invoke(Object proxy, Method method, Object[] args) {
activityName = activityMethod.name();
}

function = getActivityFunc(method, methodRetry, activityMethod, activityName);
function = getActivityFunc(method, methodRetry, activityName);
methodFunctions.put(method, function);
} catch (NoSuchMethodException e) {
throw Workflow.wrap(e);
Expand All @@ -79,5 +79,5 @@ public Object invoke(Object proxy, Method method, Object[] args) {
}

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 @@ -45,11 +44,10 @@ private LocalActivityInvocationHandler(

@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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ public void registerActivitiesImplementations(Object... activityImplementations)
@Override
public <T> T newActivityStub(Class<T> activityInterface) {
ActivityOptions options =
ActivityOptions.newBuilder().setScheduleToCloseTimeout(Duration.ofDays(1)).build();
ActivityOptions.newBuilder()
.setScheduleToCloseTimeout(Duration.ofDays(1))
.setHeartbeatTimeout(Duration.ofSeconds(1))
.build();
InvocationHandler invocationHandler =
ActivityInvocationHandler.newInstance(options, new TestActivityExecutor());
invocationHandler = new DeterministicRunnerWrapper(invocationHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@
import io.temporal.proto.workflowservice.RespondQueryTaskCompletedRequest;
import io.temporal.proto.workflowservice.SignalWorkflowExecutionRequest;
import io.temporal.proto.workflowservice.StartWorkflowExecutionRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand All @@ -125,6 +122,8 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.LongSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class TestWorkflowMutableStateImpl implements TestWorkflowMutableState {

Expand Down
103 changes: 1 addition & 102 deletions src/test/java/io/temporal/activity/ActivityOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,6 @@

public class ActivityOptionsTest {

@ActivityMethod
public void defaultActivityOptions() {}

@Test
public void testOnlyOptionsPresent() throws NoSuchMethodException {
ActivityOptions o =
ActivityOptions.newBuilder()
.setTaskList("foo")
.setHeartbeatTimeout(Duration.ofSeconds(123))
.setScheduleToCloseTimeout(Duration.ofSeconds(321))
.setScheduleToStartTimeout(Duration.ofSeconds(333))
.setStartToCloseTimeout(Duration.ofSeconds(345))
.setRetryOptions(
RetryOptions.newBuilder()
.setDoNotRetry(IllegalArgumentException.class)
.setMaximumAttempts(11111)
.setBackoffCoefficient(1.55)
.setMaximumInterval(Duration.ofDays(3))
.setExpiration(Duration.ofDays(365))
.setInitialInterval(Duration.ofMinutes(12))
.build())
.build();
ActivityMethod a =
ActivityOptionsTest.class
.getMethod("defaultActivityOptions")
.getAnnotation(ActivityMethod.class);
Assert.assertEquals(o, ActivityOptions.newBuilder(o).setActivityMethod(a).build());
}

@MethodRetry(initialIntervalSeconds = 3)
@ActivityMethod
public void defaultActivityAndRetryOptions() {}

@Test
public void testOnlyOptionsAndEmptyAnnotationsPresent() throws NoSuchMethodException {
ActivityOptions o =
ActivityOptions.newBuilder()
.setTaskList("foo")
.setHeartbeatTimeout(Duration.ofSeconds(123))
.setScheduleToCloseTimeout(Duration.ofSeconds(321))
.setScheduleToStartTimeout(Duration.ofSeconds(333))
.setStartToCloseTimeout(Duration.ofSeconds(345))
.setRetryOptions(
RetryOptions.newBuilder()
.setDoNotRetry(IllegalArgumentException.class)
.setMaximumAttempts(11111)
.setBackoffCoefficient(1.55)
.setMaximumInterval(Duration.ofDays(3))
.setExpiration(Duration.ofDays(365))
.setInitialInterval(Duration.ofMinutes(12))
.build())
.build();
ActivityMethod a =
ActivityOptionsTest.class
.getMethod("defaultActivityAndRetryOptions")
.getAnnotation(ActivityMethod.class);
Assert.assertEquals(o, ActivityOptions.newBuilder(o).setActivityMethod(a).build());
}

@MethodRetry(
initialIntervalSeconds = 12,
backoffCoefficient = 1.97,
Expand All @@ -96,13 +37,6 @@ public void testOnlyOptionsAndEmptyAnnotationsPresent() throws NoSuchMethodExcep
maximumIntervalSeconds = 22,
doNotRetry = {NullPointerException.class, UnsupportedOperationException.class}
)
@ActivityMethod(
startToCloseTimeoutSeconds = 1135,
taskList = "bar",
heartbeatTimeoutSeconds = 4567,
scheduleToCloseTimeoutSeconds = 2342,
scheduleToStartTimeoutSeconds = 9879
)
public void activityAndRetryOptions() {}

@Test
Expand All @@ -111,16 +45,7 @@ public void testOnlyAnnotationsPresent() throws NoSuchMethodException {
ActivityMethod a = method.getAnnotation(ActivityMethod.class);
MethodRetry r = method.getAnnotation(MethodRetry.class);
ActivityOptions o = ActivityOptions.newBuilder().build();
ActivityOptions merged =
ActivityOptions.newBuilder(o).setActivityMethod(a).setMethodRetry(r).build();
Assert.assertEquals(a.taskList(), merged.getTaskList());
Assert.assertEquals(a.heartbeatTimeoutSeconds(), merged.getHeartbeatTimeout().getSeconds());
Assert.assertEquals(
a.scheduleToCloseTimeoutSeconds(), merged.getScheduleToCloseTimeout().getSeconds());
Assert.assertEquals(
a.scheduleToStartTimeoutSeconds(), merged.getScheduleToStartTimeout().getSeconds());
Assert.assertEquals(
a.startToCloseTimeoutSeconds(), merged.getStartToCloseTimeout().getSeconds());
ActivityOptions merged = ActivityOptions.newBuilder(o).setMethodRetry(r).build();

RetryOptions rMerged = merged.getRetryOptions();
Assert.assertEquals(r.maximumAttempts(), rMerged.getMaximumAttempts());
Expand All @@ -132,30 +57,4 @@ public void testOnlyAnnotationsPresent() throws NoSuchMethodException {
Duration.ofSeconds(r.maximumIntervalSeconds()), rMerged.getMaximumInterval());
Assert.assertEquals(Arrays.asList(r.doNotRetry()), rMerged.getDoNotRetry());
}

@Test
public void testBothPresent() throws NoSuchMethodException {
ActivityOptions o =
ActivityOptions.newBuilder()
.setTaskList("foo")
.setHeartbeatTimeout(Duration.ofSeconds(123))
.setScheduleToCloseTimeout(Duration.ofSeconds(321))
.setScheduleToStartTimeout(Duration.ofSeconds(333))
.setStartToCloseTimeout(Duration.ofSeconds(345))
.setRetryOptions(
RetryOptions.newBuilder()
.setDoNotRetry(IllegalArgumentException.class)
.setMaximumAttempts(11111)
.setBackoffCoefficient(1.55)
.setMaximumInterval(Duration.ofDays(3))
.setExpiration(Duration.ofDays(365))
.setInitialInterval(Duration.ofMinutes(12))
.build())
.build();
Method method = ActivityOptionsTest.class.getMethod("activityAndRetryOptions");
ActivityMethod a = method.getAnnotation(ActivityMethod.class);
MethodRetry r = method.getAnnotation(MethodRetry.class);
Assert.assertEquals(
o, ActivityOptions.newBuilder(o).setActivityMethod(a).setMethodRetry(r).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import io.grpc.Status;
import io.temporal.activity.Activity;
import io.temporal.activity.ActivityMethod;
import io.temporal.client.ActivityCancelledException;
import io.temporal.testing.TestActivityEnvironment;
import io.temporal.workflow.ActivityFailureException;
Expand Down Expand Up @@ -108,8 +107,6 @@ public void testHeartbeat() {
}

public interface InterruptibleTestActivity {

@ActivityMethod(scheduleToCloseTimeoutSeconds = 1000, heartbeatTimeoutSeconds = 1)
void activity1() throws InterruptedException;
}

Expand Down
Loading
0