21
21
package io .temporal .internal .common ;
22
22
23
23
import com .google .common .base .Defaults ;
24
+ import io .nexusrpc .Header ;
24
25
import io .temporal .api .common .v1 .Callback ;
25
26
import io .temporal .api .enums .v1 .TaskQueueKind ;
26
27
import io .temporal .api .taskqueue .v1 .TaskQueue ;
27
28
import io .temporal .client .WorkflowOptions ;
28
29
import io .temporal .client .WorkflowStub ;
29
30
import io .temporal .internal .client .NexusStartWorkflowRequest ;
30
31
import java .util .Arrays ;
32
+ import java .util .Map ;
33
+ import java .util .TreeMap ;
31
34
import java .util .stream .Collectors ;
32
35
import org .slf4j .Logger ;
33
36
import org .slf4j .LoggerFactory ;
@@ -78,6 +81,20 @@ public static WorkflowStub createNexusBoundStub(
78
81
throw new IllegalArgumentException (
79
82
"WorkflowId is expected to be set on WorkflowOptions when used with Nexus" );
80
83
}
84
+ // Add the Nexus operation ID to the headers if it is not already present to support fabricating
85
+ // a NexusOperationStarted event if the completion is received before the response to a
86
+ // StartOperation request.
87
+ Map <String , String > headers =
88
+ request .getCallbackHeaders ().entrySet ().stream ()
89
+ .collect (
90
+ Collectors .toMap (
91
+ (k ) -> k .getKey ().toLowerCase (),
92
+ Map .Entry ::getValue ,
93
+ (a , b ) -> a ,
94
+ () -> new TreeMap <>(String .CASE_INSENSITIVE_ORDER )));
95
+ if (!headers .containsKey (Header .OPERATION_ID )) {
96
+ headers .put (Header .OPERATION_ID .toLowerCase (), options .getWorkflowId ());
97
+ }
81
98
WorkflowOptions .Builder nexusWorkflowOptions =
82
99
WorkflowOptions .newBuilder (options )
83
100
.setRequestId (request .getRequestId ())
@@ -87,7 +104,7 @@ public static WorkflowStub createNexusBoundStub(
87
104
.setNexus (
88
105
Callback .Nexus .newBuilder ()
89
106
.setUrl (request .getCallbackUrl ())
90
- .putAllHeader (request . getCallbackHeaders () )
107
+ .putAllHeader (headers )
91
108
.build ())
92
109
.build ()));
93
110
if (options .getTaskQueue () == null ) {
0 commit comments