{"record":{"id":"bbbdb287c88cb8bb","repo":"quarkusio/quarkus","slug":"sync-task-was-already-set","errorCode":null,"errorMessage":"Sync task was already set","messagePattern":"Sync task was already set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/scheduler/common/src/main/java/io/quarkus/scheduler/common/runtime/AbstractJobDefinition.java","lineNumber":138,"sourceCode":"        if (asyncTask != null) {\n            throw new IllegalStateException(\"Async task was already set\");\n        }\n        this.task = Objects.requireNonNull(task);\n        this.runOnVirtualThread = runOnVirtualThread;\n        return self();\n    }\n\n    @Override\n    public THIS setTask(Class<? extends Consumer<ScheduledExecution>> taskClass, boolean runOnVirtualThread) {\n        this.taskClass = Objects.requireNonNull(taskClass);\n        return setTask(SchedulerUtils.instantiateBeanOrClass(taskClass), runOnVirtualThread);\n    }\n\n    @Override\n    public THIS setAsyncTask(Function<ScheduledExecution, Uni<Void>> asyncTask) {\n        checkScheduled();\n        if (task != null) {\n            throw new IllegalStateException(\"Sync task was already set\");\n        }\n        this.asyncTask = Objects.requireNonNull(asyncTask);\n        return self();\n    }\n\n    @Override\n    public THIS setAsyncTask(Class<? extends Function<ScheduledExecution, Uni<Void>>> asyncTaskClass) {\n        this.asyncTaskClass = Objects.requireNonNull(asyncTaskClass);\n        return setAsyncTask(SchedulerUtils.instantiateBeanOrClass(asyncTaskClass));\n    }\n\n    protected void checkScheduled() {\n        if (scheduled) {\n            throw new IllegalStateException(\"Cannot modify a job that was already scheduled\");\n        }\n    }\n\n    @SuppressWarnings(\"unchecked\")","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/scheduler/common/src/main/java/io/quarkus/scheduler/common/runtime/AbstractJobDefinition.java#L120-L156","documentation":"AbstractJobDefinition.setAsyncTask() installs the async Uni-returning task for a job definition. Because a job may define only one task, this IllegalStateException is thrown when a synchronous task (setTask) was already set. It is the mirror guard of 'Async task was already set'.","triggerScenarios":"Calling scheduler.newJob(...).setTask(consumer).setAsyncTask(fn) on the same definition; calling setAsyncTask(Class) overloads after setTask; reuse of a JobDefinition that already carries a sync task.","commonSituations":"Migrating a job from sync to async execution while keeping the old setTask call; framework wrappers that pre-set a sync task; accidental double-configuration in fluent chains.","solutions":["Remove the redundant setTask call so only setAsyncTask remains","Pick one execution style per job definition","Instantiate a new JobDefinition if you need a differently configured job","Search the builder chain for both setters and delete the obsolete one"],"exampleFix":"// before\nscheduler.newJob(\"j\").setTask(c).setAsyncTask(fn);\n// after\nscheduler.newJob(\"j\").setAsyncTask(fn);","handlingStrategy":"validation","validationCode":"if (syncTaskSet) {\n    throw new IllegalStateException(\"task already set; remove setTask before setAsyncTask\");\n}\ndefinition.setAsyncTask(fn);","typeGuard":null,"tryCatchPattern":"try {\n    definition.setAsyncTask(fn);\n} catch (IllegalStateException e) {\n    LOG.error(\"Job {} already has a sync task\", name, e);\n}","preventionTips":["Pick one execution style (sync Consumer vs async Uni function) per job","When migrating sync->async, delete the old setTask call","Keep job definitions short-lived and single-purpose","Add a code-review checklist item for duplicate builder setters"],"tags":["scheduler","job-definition","builder","api-misuse"],"backgroundTag":"builder-state-conflict","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}