Skip to content

Commit 143902c

Browse files
authored
Samples used during presentations (#5)
* Added moneytransfer and moneybatch samples * Removed excessive comments * Fixed errors after merge * Fixed money samples
1 parent 2bf4224 commit 143902c

18 files changed

+808
-3
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
import io.temporal.activity.ActivityInterface;
23+
24+
@ActivityInterface
25+
public interface Account {
26+
27+
void deposit(String accountId, String referenceId, int amountCents);
28+
29+
void withdraw(String accountId, String referenceId, int amountCents);
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
import io.temporal.client.WorkflowClient;
23+
import io.temporal.serviceclient.WorkflowServiceStubs;
24+
import io.temporal.worker.Worker;
25+
import io.temporal.worker.WorkerFactory;
26+
27+
public class AccountActivityWorker {
28+
29+
static final String TASK_LIST = "Account";
30+
31+
@SuppressWarnings("CatchAndPrintStackTrace")
32+
public static void main(String[] args) {
33+
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
34+
WorkflowClient client = WorkflowClient.newInstance(service);
35+
36+
WorkerFactory factory = WorkerFactory.newInstance(client);
37+
Worker worker = factory.newWorker(TASK_LIST);
38+
39+
Account account = new AccountImpl();
40+
worker.registerActivitiesImplementations(account);
41+
42+
factory.start();
43+
System.out.println("Activity Worker started for task list: " + TASK_LIST);
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
public class AccountImpl implements Account {
23+
@Override
24+
public void deposit(String accountId, String referenceId, int amountCents) {
25+
System.out.printf(
26+
"Deposit to %s of %d cents requested. ReferenceId=%s\n",
27+
accountId, amountCents, referenceId);
28+
// throw new RuntimeException("simulated"); // Uncomment to simulate failure
29+
}
30+
31+
@Override
32+
public void withdraw(String accountId, String referenceId, int amountCents) {
33+
System.out.printf(
34+
"Withdraw to %s of %d cents requested. ReferenceId=%s\n",
35+
accountId, amountCents, referenceId);
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
import io.temporal.client.WorkflowClient;
23+
import io.temporal.serviceclient.WorkflowServiceStubs;
24+
import io.temporal.worker.Worker;
25+
import io.temporal.worker.WorkerFactory;
26+
27+
public class AccountTransferWorker {
28+
29+
@SuppressWarnings("CatchAndPrintStackTrace")
30+
public static void main(String[] args) {
31+
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
32+
WorkflowClient client = WorkflowClient.newInstance(service);
33+
WorkerFactory factory = WorkerFactory.newInstance(client);
34+
35+
Worker worker = factory.newWorker(AccountActivityWorker.TASK_LIST);
36+
worker.registerWorkflowImplementationTypes(AccountTransferWorkflowImpl.class);
37+
38+
factory.start();
39+
System.out.println("Worker started for task list: " + AccountActivityWorker.TASK_LIST);
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
import io.temporal.workflow.QueryMethod;
23+
import io.temporal.workflow.SignalMethod;
24+
import io.temporal.workflow.WorkflowInterface;
25+
import io.temporal.workflow.WorkflowMethod;
26+
27+
@WorkflowInterface
28+
public interface AccountTransferWorkflow {
29+
30+
@WorkflowMethod
31+
void deposit(String toAccountId, int batchSize);
32+
33+
@SignalMethod
34+
void withdraw(String fromAccountId, String referenceId, int amountCents);
35+
36+
@QueryMethod
37+
int getBalance();
38+
39+
@QueryMethod
40+
int getCount();
41+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
import io.temporal.activity.ActivityOptions;
23+
import io.temporal.common.RetryOptions;
24+
import io.temporal.workflow.Workflow;
25+
import java.time.Duration;
26+
import java.util.HashSet;
27+
import java.util.Set;
28+
29+
public class AccountTransferWorkflowImpl implements AccountTransferWorkflow {
30+
31+
private final ActivityOptions options =
32+
ActivityOptions.newBuilder()
33+
.setStartToCloseTimeout(Duration.ofSeconds(5))
34+
.setScheduleToStartTimeout(Duration.ofHours(1))
35+
.setRetryOptions(
36+
RetryOptions.newBuilder()
37+
.setInitialInterval(Duration.ofSeconds(1))
38+
.setMaximumInterval(Duration.ofSeconds(10))
39+
.build())
40+
.build();
41+
42+
private final Account account = Workflow.newActivityStub(Account.class, options);
43+
44+
private Set<String> references = new HashSet<>();
45+
private int balance;
46+
private int count;
47+
48+
@Override
49+
public void deposit(String toAccount, int batchSize) {
50+
Workflow.await(() -> count == batchSize);
51+
String referenceId = Workflow.randomUUID().toString();
52+
account.deposit(toAccount, referenceId, balance);
53+
}
54+
55+
@Override
56+
public void withdraw(String fromAccountId, String referenceId, int amountCents) {
57+
if (!references.add(referenceId)) {
58+
return; // duplicate
59+
}
60+
account.withdraw(fromAccountId, referenceId, amountCents);
61+
balance += amountCents;
62+
count++;
63+
}
64+
65+
@Override
66+
public int getBalance() {
67+
return balance;
68+
}
69+
70+
@Override
71+
public int getCount() {
72+
return count;
73+
}
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Demonstrates Signal Batching
2+
3+
The sample demonstrates a situation when a single deposit should be initiated for multiple
4+
withdrawals. For example a seller might want to be paid once per fixed number of transactions.
5+
The sample can be easily extended to perform a payment based on a more complex criteria like a
6+
specific time or accumulated amount.
7+
8+
The sample also demonstrates _signal with start_ way of starting workflows. If workflow is already
9+
running it just receives a signal. If it is not running then it is started first and then signal is
10+
delivered to it. You can think about _signal with start_ as a lazy way to create workflows when
11+
signalling them.
12+
13+
To run a worker that hosts the workflow code execute:
14+
15+
16+
17+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneybatch;
21+
22+
import io.temporal.client.BatchRequest;
23+
import io.temporal.client.WorkflowClient;
24+
import io.temporal.client.WorkflowOptions;
25+
import io.temporal.serviceclient.WorkflowServiceStubs;
26+
import java.util.Random;
27+
import java.util.UUID;
28+
29+
public class TransferRequester {
30+
31+
/** Number of withdrawals to batch */
32+
public static final int BATCH_SIZE = 3;
33+
34+
@SuppressWarnings("CatchAndPrintStackTrace")
35+
public static void main(String[] args) {
36+
String reference = UUID.randomUUID().toString();
37+
int amountCents = (new Random().nextInt(5) + 1) * 25;
38+
WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
39+
WorkflowClient workflowClient = WorkflowClient.newInstance(service);
40+
41+
String from = "account1";
42+
String to = "account2";
43+
WorkflowOptions options =
44+
WorkflowOptions.newBuilder()
45+
.setTaskList(AccountActivityWorker.TASK_LIST)
46+
.setWorkflowId(to)
47+
.build();
48+
AccountTransferWorkflow transferWorkflow =
49+
workflowClient.newWorkflowStub(AccountTransferWorkflow.class, options);
50+
// Signal with start sends a signal to a workflow starting it if not yet running
51+
BatchRequest request = workflowClient.newSignalWithStartRequest();
52+
request.add(transferWorkflow::deposit, to, BATCH_SIZE);
53+
request.add(transferWorkflow::withdraw, from, reference, amountCents);
54+
workflowClient.signalWithStart(request);
55+
56+
System.out.printf("Transfer of %d cents from %s to %s requested", amountCents, from, to);
57+
System.exit(0);
58+
}
59+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.samples.moneytransfer;
21+
22+
import io.temporal.activity.ActivityInterface;
23+
24+
@ActivityInterface
25+
public interface Account {
26+
27+
void deposit(String accountId, String referenceId, int amountCents);
28+
29+
void withdraw(String accountId, String referenceId, int amountCents);
30+
}

0 commit comments

Comments
 (0)