Search code examples
javacadence-workflowtemporal-workflowuber-cadence

How to pass the yaml property to Uber Cadence cronSchedule annotation


I am new to uber Cadence and trying to write a cron scheduled task. Cadence provided a cronSchedule annotation (find an example online) which takes a cron expression string for the method to be triggered at specific time. However, I want this cron expression to be loaded according to what we set in the yml file. Is there any way to do it? I currently only found this @cronSchedule annotation way to do it. I also found there is a WorkflowOption that I can set the cronSchedule. However, don't know how to use it to trigger the method. Below is the current code.

public interface CronTask {


    @WorkflowMethod(
            workflowId = CRON_WORKFLOW_ID,
            taskList = TASK_LIST,
            executionStartToCloseTimeoutSeconds = 30,
            workflowIdReusePolicy = WorkflowIdReusePolicy.TerminateIfRunning
    )
    @CronSchedule("*/1 * * * *") 
    void kickOff(String name);
}

If there is no way to achieve this. Does anyone know if we can simply replace this @CronSchedule annotation with spring @Scheduled? Will the annotation WorkflowMethod still work as expected?


Solution

  • In Temporal you can do it through WorkflowOptions:

        WorkflowOptions workflowOptions =
            WorkflowOptions.newBuilder()
                .setWorkflowId(WORKFLOW_ID)
                .setTaskQueue(TASK_QUEUE)
                .setCronSchedule("* * * * *")
                .setWorkflowExecutionTimeout(Duration.ofMinutes(3))
                .setWorkflowRunTimeout(Duration.ofMinutes(1))
                .build();
    
        // Create the workflow client stub. It is used to start our workflow execution.
        GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
    

    I believe Cadence has a similar capability.