The StrolchJobsHandler manages background tasks and scheduled jobs within the Strolch Agent.
A StrolchJob is a background task that can be executed periodically or on demand. It provides methods for:
execute method.StrolchJobsHandler.To create a new job, extend the StrolchJob class and implement the doExecute method.
public class MyJob extends StrolchJob {
public MyJob(StrolchAgent agent, String id, String name, JobMode mode) {
super(agent, id, name, mode);
}
@Override
protected void doExecute(PrivilegeContext ctx) throws Exception {
runAsAgent(tx -> {
// Perform background work in a transaction
tx.commitOnClose();
});
}
}
JobMode.Manual: The job must be triggered manually.JobMode.Recurring: The job runs periodically based on a defined delay.JobMode.Cron: The job runs according to a CRON expression.Jobs can be scheduled programmatically or via configuration.
StrolchJobsHandler jobsHandler = agent.getComponent(StrolchJobsHandler.class);
MyJob job = jobsHandler.register(MyJob.class);
job.setDelay(1, TimeUnit.MINUTES, 5, TimeUnit.MINUTES);
job.schedule();
Jobs can also be configured as Strolch Resource elements in the model. This allows for dynamic job registration and configuration without code changes.
A job resource must have the Type set to StrolchJob.
Example XML Configuration:
<Resource Id="expiredProductsJob" Name="Expired Products Job" Type="StrolchJob">
<ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
<Parameter Id="className" Name="Class Name" Type="String" Value="com.example.MyJob"/>
<Parameter Id="mode" Name="Job Mode" Type="String" Interpretation="Enumeration" Uom="JobMode" Value="Recurring"/>
<Parameter Id="delay" Name="Delay" Type="Integer" Interpretation="TimeUnit" Uom="MINUTES" Value="30"/>
<Parameter Id="initialDelay" Name="Initial Delay" Type="Integer" Interpretation="TimeUnit" Uom="SECONDS" Value="10"/>
</ParameterBag>
</Resource>
The StrolchJobsHandler provides information about job status, last execution time, duration, and any exceptions that occurred.