{"record":{"id":"3d1102cb4b8afcb6","repo":"quarkusio/quarkus","slug":"async-task-was-already-set","errorCode":null,"errorMessage":"Async task was already set","messagePattern":"Async 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":121,"sourceCode":"    @Override\n    public THIS setExecutionMaxDelay(String maxDelay) {\n        checkScheduled();\n        this.executionMaxDelay = maxDelay;\n        return self();\n    }\n\n    @Override\n    public THIS setDescription(String description) {\n        checkScheduled();\n        this.description = Objects.requireNonNull(description);\n        return self();\n    }\n\n    @Override\n    public THIS setTask(Consumer<ScheduledExecution> task, boolean runOnVirtualThread) {\n        checkScheduled();\n        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        }","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/scheduler/common/src/main/java/io/quarkus/scheduler/common/runtime/AbstractJobDefinition.java#L103-L139","documentation":"AbstractJobDefinition.setTask() installs the synchronous task Consumer for a programmatically defined job. A job definition accepts either a sync task or an async task, but not both; if an async task (setAsyncTask) was already set, this IllegalStateException is thrown. It enforces mutual exclusivity of the two task styles.","triggerScenarios":"Calling scheduler.newJob(...).setAsyncTask(fn).setTask(consumer) (or setTask after any setAsyncTask variant including Class-based overloads) on the same JobDefinition before scheduling.","commonSituations":"Copy-pasted builder chains where both task styles were configured; a shared base builder that sets an async task and calling code adding a sync one; refactoring from sync to async without removing the other call.","solutions":["Configure exactly one task style: keep setTask OR setAsyncTask, not both","Remove the earlier setAsyncTask call from the builder chain","If both behaviors are needed, combine them inside a single task implementation","Create a fresh JobDefinition via scheduler.newJob(...) instead of reusing a partially configured one"],"exampleFix":"// before\nJobDefinition job = scheduler.newJob(\"j\").setAsyncTask(fn).setTask(c);\n// after\nJobDefinition job = scheduler.newJob(\"j\").setTask(c);","handlingStrategy":"validation","validationCode":"if (asyncTaskSet) {\n    throw new IllegalStateException(\"choose either sync or async task\");\n}\ndefinition.setTask(task);","typeGuard":null,"tryCatchPattern":"try {\n    definition.setTask(task);\n} catch (IllegalStateException e) {\n    LOG.error(\"Job {} already has an async task; drop the duplicate setter\", name, e);\n}","preventionTips":["Configure only one of setTask/setAsyncTask per JobDefinition","Review builder chains for duplicate task setters","Create a new JobDefinition for differently-configured jobs","Centralize job creation in one factory method that enforces a single task style"],"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"}