Scheduled Job Tutorial
Broadleaf's ScheduledJob
s represent a process that you want to execute on one or more Nodes in a Broadleaf Cluster at a specific date or on a recurring schedule.
In this tutorial, we will create an EchoSystemEventConsumer
which will receive a scheduled job message and output it to the console.
Step 1 - Create the Event Consumer
The event consumer represents the code you want to run when the event is received. In this tutorial, we just want to output a message that the event was received to the console.
package com.mycompany.core.tutorial.event;
@Component("blEchoSystemEventConsumer")
public class EchoSystemEventConsumer implements SystemEventConsumer {
public void consumeEvent(SystemEvent event) {
System.out.println("Received event " + event.getType());
for (ScheduledDetail detail : event.getEventDetails().values()) {
System.out.println(" " + detail.getName() + ": " + detail.getValue());
}
}
public String getEventType() {
// This must match the type set on the event itself in the next step.
return "ECHO_EVENT";
}
@Override
public int getOrder() {
return 1;
}
}
Note: Make sure this class is placed in a package that will be component scanned.
If there is a need for persistence usage, the Consumer
class can be made to extend AbstractSystemEventConsumer
instead of just implementing SystemEventConsumer
. This will bring an instance of javax.persistence.EntityManagerFactory
to be used and will also provide a default order of 10000
(which can easily changed by overriding the getOrder()
method if needed).
Step 2 - Create an Event Factory
When the Job Manager determines that a Job needs to run, it creates an event. In order to create the event, it requires that an Event Factory be configured that matches the job type. Let's create that now.
package com.mycompany.core.tutorial.event;
@Service("blEchoEventFactory")
public class EchoSystemEventFactory implements SystemEventFactory {
@Override
public SystemEvent create(ScheduledJob job) {
SystemEvent event = new SystemEventImpl();
event.setType(getEventType());
event.setScopeType(EventScopeType.VM);
event.setWorkerType(EventWorkerType.ANY);
event.setEnabled(true);
SystemEventDetail detail = new SystemEventDetailImpl();
detail.setEvent(event);
detail.setFriendlyName("Current Time");
detail.setName("current_time");
detail.setValue(String.valueOf(new Date()));
event.getEventDetails().put(detail.getName(), detail);
return event;
}
@Override
public String getEventType() {
return "ECHO_EVENT";
}
}
Note: Make sure this class is placed in a package that will be component scanned and that the
EventType
on both classes match.
Step 3 - Insert SQL for the Job
The actual job scheduling details are controlled by a row in the BLC_SCHED_JOB
table. Here is the SQL required to run our job every 30 seconds:
INSERT INTO BLC_SCHED_JOB (SCHED_JOB_ID, TYPE, NAME, ENABLED, DATE_UPDATED, CRON_EXPRESSION) VALUES (-1000, 'ECHO_EVENT', 'Echo Event Job', TRUE, CURRENT_TIMESTAMP, '0/30 * * * * ?');
Step 4 - Run the Tutorial
- Build and Start Site
- Watch the console where you should see the output approximately every 30 seconds