Context
Resources.use inside handlers and the middleware TaskContext.
Resources.use inside handlers and the middleware TaskContext.
A handler's first argument is always its deserialized payload — a lambda
handler takes nothing else, while a @TaskHandler method may declare further
@Resource(...) parameters that the generated companion injects. The
execution context around it is reached two ways: resource resolution inside
the handler, and the TaskContext handed to middleware.
Resources.useimport org.byteveda.flexiq.resources.Resources;
flexiq.worker().handle(sendEmail, payload -> {
SmtpClient smtp = Resources.use("smtp");
return smtp.deliver(payload);
});Resolves a registered resource by name against
the current task's scope. Valid only while a task runs on this worker — the
worker binds the scope around the handler call; outside one it throws a
ResourceException. Prefer the declarative @Resource("smtp") parameter on a
@TaskHandler method, which the generated companion resolves the same way.
JobContext.current()JobContext job = JobContext.current();This returns the job running on this thread, so a handler can reach its own
identity without threading it through every signature. It is a static
accessor, reached through the type rather than an instance, because a handler
has no JobContext until this call hands it one.
Outside a task body it throws IllegalStateException rather than returning
null — there is no job to describe, and any answer would be a guess. That also
means it is thread-bound: work you hand to your own executor sees no context
unless you carry the value across yourself.
| Member | Description |
|---|---|
jobId() / taskName() | The running job's identity. |
setProgress(int) | Record 0–100 for inspection and the dashboard. |
log(message) / log(level, message) / log(level, message, extra) | Append a structured task-log row against this job. |
publish(Object) | Emit a streaming partial — a task log at level result. |
step() → StepContext | The durable step recorder bound to this attempt's session. |
flexiq.supportsSteps() answers whether the backend has a step store at all,
which is what decides whether step() can commit anything. It is a capability
probe rather than the gate — a step session refuses on its own when the store is
missing — so it exists for an application that wants the answer before
dispatching a job. It reports false for a backend without one, including any
implementation of FlexiQ that predates durable steps.
TaskContext (middleware)The job's identity travels through middleware hooks rather than the handler signature:
flexiq.use(new Middleware() {
@Override
public void before(TaskContext context) {
MDC.put("jobId", context.jobId);
}
@Override
public void onError(TaskContext context, Throwable error) {
log.warn("{} {} failed", context.taskName, context.jobId, error);
}
});| Member | Description |
|---|---|
jobId / taskName | The executing job's identity. |
attributes() | Mutable per-execution scratch map shared across before/after/onError for the same job (e.g. a timer or a span). |
job() | The executing job, including its lazily loaded metadata. |
Progress and cooperative cancellation go through the client by job id:
flexiq.setProgress(jobId, percent) records 0–100 for inspection and the
dashboard, and flexiq.isCancelRequested(jobId) lets a long-running handler
poll for a requestCancel from another process. Thread the job id into the
payload (or capture it in middleware attributes()) when a handler needs
either.