@@ -238,6 +238,25 @@ private <T> DurableFuture<T> runInChildContextAsync(
238238 Function <DurableContext , T > func ,
239239 RunInChildContextConfig config ,
240240 OperationSubType subType ) {
241+ return runInChildContextAsync (name , resultType , func , config , subType , false );
242+ }
243+
244+ private <T > DurableFuture <T > runInVirtualChildContextAsync (
245+ String name ,
246+ TypeToken <T > resultType ,
247+ Function <DurableContext , T > func ,
248+ RunInChildContextConfig config ,
249+ OperationSubType subType ) {
250+ return runInChildContextAsync (name , resultType , func , config , subType , true );
251+ }
252+
253+ private <T > DurableFuture <T > runInChildContextAsync (
254+ String name ,
255+ TypeToken <T > resultType ,
256+ Function <DurableContext , T > func ,
257+ RunInChildContextConfig config ,
258+ OperationSubType subType ,
259+ boolean isVirtual ) {
241260 Objects .requireNonNull (resultType , "resultType cannot be null" );
242261 Objects .requireNonNull (config , "RunInChildContextConfig cannot be null" );
243262 ParameterValidator .validateOperationName (name );
@@ -253,7 +272,9 @@ private <T> DurableFuture<T> runInChildContextAsync(
253272 func ,
254273 resultType ,
255274 config ,
256- this );
275+ this ,
276+ isVirtual ,
277+ null );
257278
258279 operation .execute ();
259280 return operation ;
@@ -386,10 +407,21 @@ public <T> T withRetry(String name, WithRetry<T> operation, WithRetryConfig conf
386407 Objects .requireNonNull (config , "config cannot be null" );
387408
388409 if (config .wrapInChildContext ()) {
389- return (T ) runInChildContext (
390- name , new TypeToken <Object >() {}, childCtx -> executeRetryLoop (childCtx , name , operation , config ));
410+ return (T ) runInChildContextAsync (
411+ name ,
412+ new TypeToken <Object >() {},
413+ childCtx -> executeRetryLoop (childCtx , name , operation , config ),
414+ RunInChildContextConfig .builder ().build (),
415+ OperationSubType .WITH_RETRY )
416+ .get ();
391417 }
392- return executeRetryLoop (this , name , operation , config );
418+ return (T ) runInVirtualChildContextAsync (
419+ name ,
420+ new TypeToken <Object >() {},
421+ childCtx -> executeRetryLoop (childCtx , name , operation , config ),
422+ RunInChildContextConfig .builder ().build (),
423+ OperationSubType .WITH_RETRY )
424+ .get ();
393425 }
394426
395427 @ Override
@@ -399,12 +431,21 @@ public <T> T withRetry(WithRetry<T> operation, WithRetryConfig config) {
399431 Objects .requireNonNull (config , "config cannot be null" );
400432
401433 if (config .wrapInChildContext ()) {
402- return (T ) runInChildContext (
403- ANONYMOUS_CHILD_CONTEXT_NAME ,
404- new TypeToken <Object >() {},
405- childCtx -> executeRetryLoop (childCtx , null , operation , config ));
434+ return (T ) runInChildContextAsync (
435+ ANONYMOUS_CHILD_CONTEXT_NAME ,
436+ new TypeToken <Object >() {},
437+ childCtx -> executeRetryLoop (childCtx , null , operation , config ),
438+ RunInChildContextConfig .builder ().build (),
439+ OperationSubType .WITH_RETRY )
440+ .get ();
406441 }
407- return executeRetryLoop (this , null , operation , config );
442+ return (T ) runInVirtualChildContextAsync (
443+ ANONYMOUS_CHILD_CONTEXT_NAME ,
444+ new TypeToken <Object >() {},
445+ childCtx -> executeRetryLoop (childCtx , null , operation , config ),
446+ RunInChildContextConfig .builder ().build (),
447+ OperationSubType .WITH_RETRY )
448+ .get ();
408449 }
409450
410451 @ Override
@@ -414,8 +455,20 @@ public <T> DurableFuture<T> withRetryAsync(String name, WithRetry<T> operation,
414455 Objects .requireNonNull (operation , "operation cannot be null" );
415456 Objects .requireNonNull (config , "config cannot be null" );
416457
417- return (DurableFuture <T >) runInChildContextAsync (
418- name , new TypeToken <Object >() {}, childCtx -> executeRetryLoop (childCtx , name , operation , config ));
458+ if (config .wrapInChildContext ()) {
459+ return (DurableFuture <T >) runInChildContextAsync (
460+ name ,
461+ new TypeToken <Object >() {},
462+ childCtx -> executeRetryLoop (childCtx , name , operation , config ),
463+ RunInChildContextConfig .builder ().build (),
464+ OperationSubType .WITH_RETRY );
465+ }
466+ return (DurableFuture <T >) runInVirtualChildContextAsync (
467+ name ,
468+ new TypeToken <Object >() {},
469+ childCtx -> executeRetryLoop (childCtx , name , operation , config ),
470+ RunInChildContextConfig .builder ().build (),
471+ OperationSubType .WITH_RETRY );
419472 }
420473
421474 @ Override
@@ -424,10 +477,20 @@ public <T> DurableFuture<T> withRetryAsync(WithRetry<T> operation, WithRetryConf
424477 Objects .requireNonNull (operation , "operation cannot be null" );
425478 Objects .requireNonNull (config , "config cannot be null" );
426479
427- return (DurableFuture <T >) runInChildContextAsync (
480+ if (config .wrapInChildContext ()) {
481+ return (DurableFuture <T >) runInChildContextAsync (
482+ ANONYMOUS_CHILD_CONTEXT_NAME ,
483+ new TypeToken <Object >() {},
484+ childCtx -> executeRetryLoop (childCtx , null , operation , config ),
485+ RunInChildContextConfig .builder ().build (),
486+ OperationSubType .WITH_RETRY );
487+ }
488+ return (DurableFuture <T >) runInVirtualChildContextAsync (
428489 ANONYMOUS_CHILD_CONTEXT_NAME ,
429490 new TypeToken <Object >() {},
430- childCtx -> executeRetryLoop (childCtx , null , operation , config ));
491+ childCtx -> executeRetryLoop (childCtx , null , operation , config ),
492+ RunInChildContextConfig .builder ().build (),
493+ OperationSubType .WITH_RETRY );
431494 }
432495
433496 /**
0 commit comments