26
26
import com .uber .cadence .activity .ActivityMethod ;
27
27
import com .uber .cadence .client .WorkflowClient ;
28
28
import com .uber .cadence .client .WorkflowOptions ;
29
- import com .uber .cadence .converter .DataConverter ;
30
- import com .uber .cadence .converter .JsonDataConverter ;
31
29
import com .uber .cadence .serviceclient .IWorkflowService ;
32
30
import com .uber .cadence .serviceclient .WorkflowServiceTChannel ;
33
31
import com .uber .cadence .worker .Worker ;
34
32
import com .uber .cadence .workflow .Workflow ;
35
33
import com .uber .cadence .workflow .WorkflowMethod ;
36
- import java . nio . ByteBuffer ;
34
+ import com . uber . cadence . workflow . WorkflowUtils ;
37
35
import java .time .LocalDateTime ;
38
36
import java .time .format .DateTimeFormatter ;
39
37
import java .util .HashMap ;
@@ -70,9 +68,54 @@ public static class GreetingWorkflowImpl implements HelloActivity.GreetingWorkfl
70
68
71
69
@ Override
72
70
public String getGreeting (String name ) {
71
+ SearchAttributes currentSearchAttributes = Workflow .getWorkflowInfo ().getSearchAttributes ();
72
+ // Use System.out just for demo, please use Workflow.getLogger in production.
73
+ System .out .println ("Search Attributes on start: " );
74
+ printSearchAttributes (currentSearchAttributes );
75
+
76
+ // update some of the search attributes
77
+ Map <String , Object > upsertedMap = new HashMap <>();
78
+ upsertedMap .put ("CustomKeywordField" , name );
79
+ Workflow .upsertSearchAttributes (upsertedMap );
80
+
81
+ currentSearchAttributes = Workflow .getWorkflowInfo ().getSearchAttributes ();
82
+ System .out .println ("Search Attributes after upsert: " );
83
+ printSearchAttributes (currentSearchAttributes );
84
+
73
85
// This is a blocking call that returns only after the activity has completed.
74
86
return activities .composeGreeting ("Hello" , name );
75
87
}
88
+
89
+ private void printSearchAttributes (SearchAttributes searchAttributes ) {
90
+ if (searchAttributes == null ) {
91
+ return ;
92
+ }
93
+ searchAttributes
94
+ .getIndexedFields ()
95
+ .forEach (
96
+ (k , v ) -> {
97
+ System .out .printf ("%s: %s\n " , k , getValueForKey (k , searchAttributes ));
98
+ });
99
+ }
100
+
101
+ private String getValueForKey (String key , SearchAttributes searchAttributes ) {
102
+ switch (key ) {
103
+ case "CustomKeywordField" :
104
+ case "CustomDatetimeField" :
105
+ case "CustomStringField" :
106
+ return WorkflowUtils .getValueFromSearchAttributes (searchAttributes , key , String .class );
107
+ case "CustomIntField" :
108
+ return WorkflowUtils .getValueFromSearchAttributes (searchAttributes , key , Integer .class )
109
+ .toString ();
110
+ case "CustomDoubleField" :
111
+ return WorkflowUtils .getValueFromSearchAttributes (searchAttributes , key , Double .class )
112
+ .toString ();
113
+ case "CustomBoolField" :
114
+ return WorkflowUtils .getValueFromSearchAttributes (searchAttributes , key , Boolean .class )
115
+ .toString ();
116
+ }
117
+ return "Unknown key" ;
118
+ }
76
119
}
77
120
78
121
static class GreetingActivitiesImpl implements HelloActivity .GreetingActivities {
@@ -109,8 +152,10 @@ public static void main(String[] args) {
109
152
workflowClient .newWorkflowStub (
110
153
HelloSearchAttributes .GreetingWorkflow .class , workflowOptions );
111
154
// Execute a workflow waiting for it to complete.
112
- String greeting = workflow .getGreeting ("SearchAttributes " );
155
+ String greeting = workflow .getGreeting ("World " );
113
156
157
+ // Bellow shows how to read search attributes using DescribeWorkflowExecution API
158
+ // You can do similar things using ListWorkflowExecutions
114
159
IWorkflowService cadenceService = new WorkflowServiceTChannel ();
115
160
WorkflowExecution execution = new WorkflowExecution ();
116
161
execution .setWorkflowId (workflowID );
@@ -121,7 +166,9 @@ public static void main(String[] args) {
121
166
try {
122
167
DescribeWorkflowExecutionResponse resp = cadenceService .DescribeWorkflowExecution (request );
123
168
SearchAttributes searchAttributes = resp .workflowExecutionInfo .getSearchAttributes ();
124
- String keyword = getKeywordFromSearchAttribute (searchAttributes );
169
+ String keyword =
170
+ WorkflowUtils .getValueFromSearchAttributes (
171
+ searchAttributes , "CustomKeywordField" , String .class );
125
172
System .out .printf ("In workflow we get CustomKeywordField is: %s\n " , keyword );
126
173
} catch (Exception e ) {
127
174
System .out .println (e );
@@ -135,7 +182,7 @@ private static Map<String, Object> generateSearchAttributes() {
135
182
Map <String , Object > searchAttributes = new HashMap <>();
136
183
searchAttributes .put (
137
184
"CustomKeywordField" ,
138
- "keys " ); // each field can also be array such as: String[] keys = {"k1", "k2"};
185
+ "old world " ); // each field can also be array such as: String[] keys = {"k1", "k2"};
139
186
searchAttributes .put ("CustomIntField" , 1 );
140
187
searchAttributes .put ("CustomDoubleField" , 0.1 );
141
188
searchAttributes .put ("CustomBoolField" , true );
@@ -153,14 +200,4 @@ private static String generateDateTimeFieldValue() {
153
200
DateTimeFormatter formatter = DateTimeFormatter .ISO_DATE_TIME ;
154
201
return currentDateTime .format (formatter );
155
202
}
156
-
157
- // example for extract value from search attributes
158
- private static String getKeywordFromSearchAttribute (SearchAttributes searchAttributes ) {
159
- Map <String , ByteBuffer > map = searchAttributes .getIndexedFields ();
160
- ByteBuffer byteBuffer = map .get ("CustomKeywordField" );
161
- DataConverter dataConverter = JsonDataConverter .getInstance ();
162
- final byte [] valueBytes = new byte [byteBuffer .limit () - byteBuffer .position ()];
163
- byteBuffer .get (valueBytes , 0 , valueBytes .length );
164
- return dataConverter .fromData (valueBytes , String .class , String .class );
165
- }
166
203
}
0 commit comments