Skip to content

Commit

Permalink
25.0 Release (#18)
Browse files Browse the repository at this point in the history
* Error handling and other proto changes in Java SDK (#17)

* Updated temporal-sdk version to 0.25.0
  • Loading branch information
mfateev authored Jun 27, 2020
1 parent 143902c commit 61bf7a9
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 71 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ repositories {
}

dependencies {
implementation group: 'io.temporal', name: 'temporal-sdk', version: '0.23.1'
implementation group: 'io.temporal', name: 'temporal-sdk', version: '0.25.0'
implementation group: 'commons-configuration', name: 'commons-configuration', version: '1.9'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import io.temporal.activity.ActivityOptions;
import io.temporal.common.RetryOptions;
import io.temporal.workflow.ActivityException;
import io.temporal.failure.ActivityFailure;
import io.temporal.workflow.Saga;
import io.temporal.workflow.Workflow;
import java.time.Duration;
Expand Down Expand Up @@ -51,7 +51,7 @@ public void bookTrip(String name) {

String flightReservationID = activities.bookFlight(name);
saga.addCompensation(activities::cancelFlight, flightReservationID, name);
} catch (ActivityException e) {
} catch (ActivityFailure e) {
saga.compensate();
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowStub;
import io.temporal.proto.common.WorkflowExecution;
import io.temporal.common.v1.WorkflowExecution;
import io.temporal.serviceclient.WorkflowServiceStubs;
import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
public interface StoreActivities {

final class TaskListFileNamePair {
private final String hostTaskList;
private final String fileName;
private String hostTaskList;
private String fileName;

public TaskListFileNamePair(String hostTaskList, String fileName) {
this.hostTaskList = hostTaskList;
this.fileName = fileName;
}

/** Jackson needs it */
public TaskListFileNamePair() {}

public String getHostTaskList() {
return hostTaskList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static class GreetingWorkflowImpl implements GreetingWorkflow {
.setRetryOptions(
RetryOptions.newBuilder()
.setInitialInterval(Duration.ofSeconds(1))
.setDoNotRetry(IllegalArgumentException.class)
.setDoNotRetry(IllegalArgumentException.class.getName())
.build())
.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package io.temporal.samples.hello;

import io.temporal.activity.Activity;
import io.temporal.activity.ActivityExecutionContext;
import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityOptions;
import io.temporal.client.ActivityCompletionClient;
Expand Down Expand Up @@ -86,17 +87,18 @@ static class GreetingActivitiesImpl implements GreetingActivities {

/**
* Demonstrates how to implement an activity asynchronously. When {@link
* Activity#doNotCompleteOnReturn()} is called the activity implementation function returning
* doesn't complete the activity.
* io.temporal.activity.ActivityExecutionContext#doNotCompleteOnReturn()} is called the activity
* implementation function returning doesn't complete the activity.
*/
@Override
public String composeGreeting(String greeting, String name) {
// TaskToken is a correlation token used to match an activity task with its completion
byte[] taskToken = Activity.getTaskToken();
ActivityExecutionContext context = Activity.getExecutionContext();
byte[] taskToken = context.getTaskToken();
// In real life this request can be executed anywhere. By a separate service for
// example.
ForkJoinPool.commonPool().execute(() -> composeGreetingAsync(taskToken, greeting, name));
Activity.doNotCompleteOnReturn();
context.doNotCompleteOnReturn();
// When doNotCompleteOnReturn() is invoked the return value is ignored.
return "ignored";
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/io/temporal/samples/hello/HelloCron.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import io.temporal.activity.Activity;
import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityOptions;
import io.temporal.client.DuplicateWorkflowException;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowExecutionAlreadyStarted;
import io.temporal.client.WorkflowOptions;
import io.temporal.proto.common.WorkflowExecution;
import io.temporal.common.v1.WorkflowExecution;
import io.temporal.serviceclient.WorkflowServiceStubs;
import io.temporal.worker.Worker;
import io.temporal.worker.WorkerFactory;
Expand Down Expand Up @@ -82,7 +82,8 @@ public void greet(String name) {
static class GreetingActivitiesImpl implements GreetingActivities {
@Override
public void greet(String greeting) {
System.out.println("From " + Activity.getWorkflowExecution() + ": " + greeting);
System.out.println(
"From " + Activity.getExecutionContext().getInfo().getWorkflowId() + ": " + greeting);
}
}

Expand Down Expand Up @@ -114,13 +115,17 @@ public static void main(String[] args) throws InterruptedException {
.setWorkflowId(CRON_WORKFLOW_ID)
.setTaskList(TASK_LIST)
.setCronSchedule("* * * * *")
// Execution timeout limits total time. Cron will stop executing after this timeout.
.setWorkflowExecutionTimeout(Duration.ofMinutes(10))
// Run timeout limits duration of a single workflow invocation.
.setWorkflowRunTimeout(Duration.ofMinutes(1))
.build();
// WorkflowOptions.newBuilder().setCronSchedule("@every 2s").build();
GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
try {
WorkflowExecution execution = WorkflowClient.start(workflow::greet, "World");
System.out.println("Started " + execution);
} catch (DuplicateWorkflowException e) {
} catch (WorkflowExecutionAlreadyStarted e) {
System.out.println("Already running as " + e.getExecution());
} catch (Throwable e) {
e.printStackTrace();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/io/temporal/samples/hello/HelloPeriodic.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import io.temporal.activity.Activity;
import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityOptions;
import io.temporal.client.DuplicateWorkflowException;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowException;
import io.temporal.client.WorkflowExecutionAlreadyStarted;
import io.temporal.client.WorkflowOptions;
import io.temporal.client.WorkflowStub;
import io.temporal.proto.common.WorkflowExecution;
import io.temporal.common.v1.WorkflowExecution;
import io.temporal.serviceclient.WorkflowServiceStubs;
import io.temporal.worker.Worker;
import io.temporal.worker.WorkerFactory;
Expand Down Expand Up @@ -112,7 +112,8 @@ public void greetPeriodically(String name) {
static class GreetingActivitiesImpl implements GreetingActivities {
@Override
public void greet(String greeting) {
System.out.println("From " + Activity.getWorkflowExecution() + ": " + greeting);
System.out.println(
"From " + Activity.getExecutionContext().getInfo().getWorkflowId() + ": " + greeting);
}
}

Expand Down Expand Up @@ -160,7 +161,7 @@ public static void main(String[] args) throws InterruptedException {
try {
execution = WorkflowClient.start(workflow::greetPeriodically, "World");
System.out.println("Started " + execution);
} catch (DuplicateWorkflowException e) {
} catch (WorkflowExecutionAlreadyStarted e) {
System.out.println("Still running as " + e.getExecution());
} catch (Throwable e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.converter.GsonJsonDataConverter;
import io.temporal.proto.common.Payload;
import io.temporal.proto.common.SearchAttributes;
import io.temporal.proto.common.WorkflowExecution;
import io.temporal.proto.workflowservice.DescribeWorkflowExecutionRequest;
import io.temporal.proto.workflowservice.DescribeWorkflowExecutionResponse;
import io.temporal.common.v1.Payload;
import io.temporal.common.v1.SearchAttributes;
import io.temporal.common.v1.WorkflowExecution;
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 io.temporal.workflowservice.v1.DescribeWorkflowExecutionRequest;
import io.temporal.workflowservice.v1.DescribeWorkflowExecutionResponse;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
Expand Down Expand Up @@ -168,7 +167,7 @@ private static String generateDateTimeFieldValue() {
// example for extract value from search attributes
private static String getKeywordFromSearchAttribute(SearchAttributes searchAttributes) {
Payload field = searchAttributes.getIndexedFieldsOrThrow("CustomKeywordField");
DataConverter dataConverter = GsonJsonDataConverter.getInstance();
return dataConverter.getPayloadConverter().fromData(field, String.class, String.class);
DataConverter dataConverter = DataConverter.getDefaultInstance();
return dataConverter.fromPayload(field, String.class, String.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public void withdraw(String accountId, String referenceId, int amountCents) {
System.out.printf(
"Withdraw to %s of %d cents requested. ReferenceId=%s\n",
accountId, amountCents, referenceId);
// throw new RuntimeException("simulated");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
public class AccountTransferWorkflowImpl implements AccountTransferWorkflow {

private final ActivityOptions options =
ActivityOptions.newBuilder()
.setStartToCloseTimeout(Duration.ofSeconds(5))
.setScheduleToStartTimeout(Duration.ofMinutes(10))
.build();
ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(5)).build();
private final Account account = Workflow.newActivityStub(Account.class, options);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import static io.temporal.samples.updatabletimer.DynamicSleepWorkflowWorker.DYNAMIC_SLEEP_WORKFLOW_ID;
import static io.temporal.samples.updatabletimer.DynamicSleepWorkflowWorker.TASK_LIST;

import io.temporal.client.DuplicateWorkflowException;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowExecutionAlreadyStarted;
import io.temporal.client.WorkflowOptions;
import io.temporal.proto.common.WorkflowExecution;
import io.temporal.proto.common.WorkflowIdReusePolicy;
import io.temporal.common.v1.WorkflowExecution;
import io.temporal.enums.v1.WorkflowIdReusePolicy;
import io.temporal.serviceclient.WorkflowServiceStubs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -45,15 +45,16 @@ public static void main(String[] args) {
WorkflowOptions.newBuilder()
.setTaskList(TASK_LIST)
.setWorkflowId(DYNAMIC_SLEEP_WORKFLOW_ID)
.setWorkflowIdReusePolicy(WorkflowIdReusePolicy.AllowDuplicate)
.setWorkflowIdReusePolicy(
WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE)
.build());

try {
// Start asynchronously
WorkflowExecution execution =
WorkflowClient.start(workflow::execute, System.currentTimeMillis() + 60000);
logger.info("Workflow started: " + execution);
} catch (DuplicateWorkflowException e) {
} catch (WorkflowExecutionAlreadyStarted e) {
logger.info("Workflow already running: " + e.getExecution());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</encoder>
</appender>
<logger name="io.grpc.netty" level="INFO"/>
<root level="INFO">
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowException;
import io.temporal.client.WorkflowOptions;
import io.temporal.failure.ApplicationFailure;
import io.temporal.testing.TestWorkflowEnvironment;
import io.temporal.worker.Worker;
import org.junit.After;
Expand Down Expand Up @@ -70,7 +71,9 @@ public void testTripBookingFails() {
workflow.bookTrip("trip1");
fail("unreachable");
} catch (WorkflowException e) {
assertEquals("Flight booking did not work", e.getCause().getCause().getMessage());
assertEquals(
"Flight booking did not work",
((ApplicationFailure) e.getCause().getCause()).getOriginalMessage());
}
}

Expand All @@ -93,7 +96,9 @@ public void testSAGA() {
workflow.bookTrip("trip1");
fail("unreachable");
} catch (WorkflowException e) {
assertEquals("Flight booking did not work", e.getCause().getCause().getMessage());
assertEquals(
"Flight booking did not work",
((ApplicationFailure) e.getCause().getCause()).getOriginalMessage());
}

verify(activities).cancelHotel(eq("HotelBookingID1"), eq("trip1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.proto.common.TimeoutType;
import io.temporal.enums.v1.TimeoutType;
import io.temporal.failure.TimeoutFailure;
import io.temporal.samples.fileprocessing.StoreActivities.TaskListFileNamePair;
import io.temporal.testing.SimulatedTimeoutException;
import io.temporal.testing.TestWorkflowEnvironment;
import io.temporal.worker.Worker;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -142,7 +142,8 @@ public void testHostFailover() {

StoreActivities activitiesHost1 = mock(StoreActivities.class);
when(activitiesHost1.process(FILE_NAME_UNPROCESSED))
.thenThrow(new SimulatedTimeoutException(TimeoutType.ScheduleToStart));
.thenThrow(
new TimeoutFailure("simulated", null, TimeoutType.TIMEOUT_TYPE_SCHEDULE_TO_START));
workerHost1.registerActivitiesImplementations(activitiesHost1);

StoreActivities activitiesHost2 = mock(StoreActivities.class);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/temporal/samples/hello/HelloCronTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.proto.common.WorkflowExecution;
import io.temporal.common.v1.WorkflowExecution;
import io.temporal.samples.hello.HelloCron.GreetingActivities;
import io.temporal.samples.hello.HelloCron.GreetingWorkflow;
import io.temporal.samples.hello.HelloCron.GreetingWorkflowImpl;
Expand Down
Loading

0 comments on commit 61bf7a9

Please sign in to comment.