1010import dev .dbos .transact .exceptions .*;
1111import dev .dbos .transact .json .JSONUtil ;
1212import dev .dbos .transact .queue .Queue ;
13- import dev .dbos .transact .queue .QueueRegistry ;
1413import dev .dbos .transact .queue .QueueService ;
1514import dev .dbos .transact .workflow .ForkOptions ;
1615import dev .dbos .transact .workflow .WorkflowHandle ;
2524
2625import java .lang .reflect .InvocationTargetException ;
2726import java .lang .reflect .Method ;
28- import java .sql .SQLException ;
2927import java .util .Optional ;
3028import java .util .UUID ;
3129import java .util .concurrent .*;
@@ -150,7 +148,7 @@ public <T> T syncWorkflow(String workflowName,
150148 String targetClassName ,
151149 Object target ,
152150 Object [] args ,
153- WorkflowFunction function ,
151+ WorkflowFunctionReflect function ,
154152 String workflowId ) throws Throwable {
155153
156154 String wfid = workflowId ;
@@ -213,7 +211,7 @@ public <T> T syncWorkflow(String workflowName,
213211 <T > T runAndSaveResult (
214212 Object target ,
215213 Object [] args ,
216- WorkflowFunction function ,
214+ WorkflowFunctionReflect function ,
217215 String workflowId ) throws Throwable {
218216
219217 try {
@@ -249,7 +247,7 @@ public <T> WorkflowHandle<T> submitWorkflow(String workflowName,
249247 String targetClassName ,
250248 Object target ,
251249 Object [] args ,
252- WorkflowFunction function ) throws Throwable {
250+ WorkflowFunctionReflect function ) throws Throwable {
253251
254252 DBOSContext ctx = DBOSContextHolder .get ();
255253 String workflowId = ctx .getWorkflowId () ;
@@ -332,7 +330,6 @@ public void enqueueWorkflow(String workflowName,
332330 ) throws Throwable {
333331
334332
335-
336333 DBOSContext ctx = DBOSContextHolder .get ();
337334 String wfid = ctx .getWorkflowId () ;
338335
@@ -363,7 +360,7 @@ public <T> T runStep(String stepName,
363360 int maxAttempts ,
364361 float backOffRate ,
365362 Object [] args ,
366- DBOSFunction <T > function
363+ WorkflowFunction <T > function
367364 ) throws Throwable {
368365
369366
@@ -435,7 +432,7 @@ public <T> T runStep(String stepName,
435432 * Retrieve the workflowHandle for the workflowId
436433 *
437434 */
438- public WorkflowHandle <? > retrieveWorkflow (String workflowId ) {
435+ public < R > WorkflowHandle <R > retrieveWorkflow (String workflowId ) {
439436 return new WorkflowHandleDBPoll (workflowId , systemDatabase ) ;
440437 }
441438
@@ -488,7 +485,7 @@ public void sleep(float seconds) {
488485
489486 }
490487
491- public WorkflowHandle <? > resumeWorkflow (String workflowId ) {
488+ public < T > WorkflowHandle <T > resumeWorkflow (String workflowId ) {
492489
493490 Supplier <Void > resumeFunction = () -> {
494491 logger .info ("Resuming workflow: " , workflowId );
@@ -512,7 +509,7 @@ public void cancelWorkflow(String workflowId) {
512509
513510 }
514511
515- public WorkflowHandle <? > forkWorkflow (String workflowId , int startStep , ForkOptions options ) {
512+ public < T > WorkflowHandle <T > forkWorkflow (String workflowId , int startStep , ForkOptions options ) {
516513
517514 Supplier <String > forkFunction = () -> {
518515 logger .info (String .format ("Forking workflow:%s from step:%d " , workflowId , startStep ));
@@ -523,4 +520,29 @@ public WorkflowHandle<?> forkWorkflow(String workflowId, int startStep, ForkOpti
523520 String forkedId = systemDatabase .callFunctionAsStep (forkFunction , "DBOS.forkedWorkflow" );
524521 return retrieveWorkflow (forkedId );
525522 }
523+
524+ public <T > WorkflowHandle <T > startWorkflow (WorkflowFunction <T > func ) {
525+ DBOSContext oldctx = DBOSContextHolder .get ();
526+ DBOSContext newCtx = oldctx ;
527+
528+ if (newCtx .getWorkflowId () == null ) {
529+ newCtx = newCtx .copyWithWorkflowId (UUID .randomUUID ().toString ()) ;
530+ }
531+
532+ if (newCtx .getQueue () == null ) {
533+ newCtx = oldctx .copyWithAsync () ;
534+ }
535+
536+ try {
537+ DBOSContextHolder .set (newCtx );
538+ func .execute ();
539+ return retrieveWorkflow (newCtx .getWorkflowId ());
540+ } catch (Throwable t ) {
541+ throw new DBOSException (UNEXPECTED .getCode (), t .getMessage ());
542+ } finally {
543+ DBOSContextHolder .set (oldctx );
544+ }
545+
546+ }
547+
526548}
0 commit comments