|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.uber.cadence.samples.hello; |
| 19 | + |
| 20 | +import static com.uber.cadence.samples.common.SampleConstants.DOMAIN; |
| 21 | + |
| 22 | +import com.uber.cadence.QueryConsistencyLevel; |
| 23 | +import com.uber.cadence.WorkflowExecution; |
| 24 | +import com.uber.cadence.client.QueryOptions; |
| 25 | +import com.uber.cadence.client.WorkflowClient; |
| 26 | +import com.uber.cadence.client.WorkflowClientOptions; |
| 27 | +import com.uber.cadence.client.WorkflowOptions; |
| 28 | +import com.uber.cadence.client.WorkflowStub; |
| 29 | +import com.uber.cadence.serviceclient.ClientOptions; |
| 30 | +import com.uber.cadence.serviceclient.WorkflowServiceTChannel; |
| 31 | +import com.uber.cadence.worker.Worker; |
| 32 | +import com.uber.cadence.worker.WorkerFactory; |
| 33 | +import com.uber.cadence.workflow.QueryMethod; |
| 34 | +import com.uber.cadence.workflow.SignalMethod; |
| 35 | +import com.uber.cadence.workflow.Workflow; |
| 36 | +import com.uber.cadence.workflow.WorkflowMethod; |
| 37 | +import java.time.Duration; |
| 38 | + |
| 39 | +/** |
| 40 | + * Demonstrates consistent query capability. Requires a local instance of Cadence server of version |
| 41 | + * >= 0.22.0 to be running. |
| 42 | + */ |
| 43 | +public class HelloConsistentQuery { |
| 44 | + |
| 45 | + static final String TASK_LIST = "HelloQuery"; |
| 46 | + |
| 47 | + public interface GreetingWorkflow { |
| 48 | + |
| 49 | + @WorkflowMethod |
| 50 | + void createGreeting(String name); |
| 51 | + |
| 52 | + @SignalMethod |
| 53 | + void increase(); |
| 54 | + |
| 55 | + /** Returns greeting as a query value. */ |
| 56 | + @QueryMethod |
| 57 | + int getCounter(); |
| 58 | + } |
| 59 | + |
| 60 | + /** GreetingWorkflow implementation that updates greeting after sleeping for 5 seconds. */ |
| 61 | + public static class GreetingWorkflowImpl implements GreetingWorkflow { |
| 62 | + |
| 63 | + private int counter; |
| 64 | + |
| 65 | + @Override |
| 66 | + public void createGreeting(String name) { |
| 67 | + // Workflow code always uses WorkflowThread.sleep |
| 68 | + // and Workflow.currentTimeMillis instead of standard Java ones. |
| 69 | + Workflow.sleep(Duration.ofDays(2)); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void increase() { |
| 74 | + this.counter++; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int getCounter() { |
| 79 | + return counter; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) throws InterruptedException { |
| 84 | + // Get a new client |
| 85 | + // NOTE: to set a different options, you can do like this: |
| 86 | + // ClientOptions.newBuilder().setRpcTimeout(5 * 1000).build(); |
| 87 | + WorkflowClient workflowClient = |
| 88 | + WorkflowClient.newInstance( |
| 89 | + new WorkflowServiceTChannel(ClientOptions.defaultInstance()), |
| 90 | + WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build()); |
| 91 | + // Get worker to poll the task list. |
| 92 | + WorkerFactory factory = WorkerFactory.newInstance(workflowClient); |
| 93 | + Worker worker = factory.newWorker(TASK_LIST); |
| 94 | + worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class); |
| 95 | + factory.start(); |
| 96 | + |
| 97 | + // Start a workflow execution. Usually this is done from another program. |
| 98 | + // Get a workflow stub using the same task list the worker uses. |
| 99 | + WorkflowOptions workflowOptions = |
| 100 | + new WorkflowOptions.Builder() |
| 101 | + .setTaskList(TASK_LIST) |
| 102 | + .setExecutionStartToCloseTimeout(Duration.ofSeconds(30)) |
| 103 | + .build(); |
| 104 | + final WorkflowStub workflow = |
| 105 | + workflowClient.newUntypedWorkflowStub("GreetingWorkflow::createGreeting", workflowOptions); |
| 106 | + |
| 107 | + // Start workflow asynchronously to not use another thread to query. |
| 108 | + final WorkflowExecution wf = workflow.start("World"); |
| 109 | + System.out.println("started workflow " + wf.getWorkflowId() + ", " + wf.getRunId()); |
| 110 | + System.out.println("initial value after started"); |
| 111 | + System.out.println( |
| 112 | + workflow.queryWithOptions( |
| 113 | + "GreetingWorkflow::getCounter", |
| 114 | + new QueryOptions.Builder() |
| 115 | + .setQueryConsistencyLevel(QueryConsistencyLevel.STRONG) |
| 116 | + .build(), |
| 117 | + Integer.class, |
| 118 | + Integer.class)); // Should print 0 |
| 119 | + |
| 120 | + // Now we can send a signal to it using workflow stub. |
| 121 | + workflow.signal("GreetingWorkflow::increase"); |
| 122 | + System.out.println("after increase 1 time"); |
| 123 | + System.out.println( |
| 124 | + workflow.queryWithOptions( |
| 125 | + "GreetingWorkflow::getCounter", |
| 126 | + new QueryOptions.Builder() |
| 127 | + .setQueryConsistencyLevel(QueryConsistencyLevel.STRONG) |
| 128 | + .build(), |
| 129 | + Integer.class, |
| 130 | + Integer.class)); // Should print 1 |
| 131 | + |
| 132 | + workflow.signal("GreetingWorkflow::increase"); |
| 133 | + workflow.signal("GreetingWorkflow::increase"); |
| 134 | + workflow.signal("GreetingWorkflow::increase"); |
| 135 | + workflow.signal("GreetingWorkflow::increase"); |
| 136 | + System.out.println("after increase 1+4 times"); |
| 137 | + System.out.println( |
| 138 | + workflow.queryWithOptions( |
| 139 | + "GreetingWorkflow::getCounter", |
| 140 | + new QueryOptions.Builder() |
| 141 | + .setQueryConsistencyLevel(QueryConsistencyLevel.STRONG) |
| 142 | + .build(), |
| 143 | + Integer.class, |
| 144 | + Integer.class)); // Should print 5 |
| 145 | + System.exit(0); |
| 146 | + } |
| 147 | +} |
0 commit comments