Skip to main content

Work Scheduling

While configuring the work schedule in the model is a powerful tool, you may encounter situations where this is not sufficient. You may for example want to read the configuration from a database or some other form of configuration.

To allow for this, process automation provides an extension point to configure your own work schedules at runtime.

If you haven't already, read up on work schedules on the managing executions page first.

Contributing to a WorkSchedule

Work schedules live inside the process automation component and are implemented as a Singleton Enterprise Java Bean (EJB). To contribute custom schedules, you will need to inject this bean as a dependency into another EJB. This newly created bean can be positioned in your own component as long as it depends on processAutomation.

The following is an example of such bean.

package my.component;

import net.democritus.processautomation.WorkSchedule;
import net.democritus.processautomation.WorkScheduleDetails;

@Startup
@Singleton
public class DefaultWorkScheduleConfiguration {

@EJB(beanName="DefaultWorkSchedule")
WorkSchedule defaultWorkSchedule;

@PostConstruct
public void initializeSchedule() {
// interact with defaultWorkSchedule to apply configuration
}

}

Register a schedule

The WorkSchedule interface provides the Handle register(WorkScheduleDetails) method to register a new schedule. This can be done at startup (inside @PostConstruct) or at any other time during application runtime.

In the WorkScheduleDetails you provide the startAt and until through the constructor. You can also specify the timezone and week day just like you would in the model.

@PostConstruct
public void initializeSchedule() {
defaultWorkSchedule.register(new WorkScheduleDetails("09:00", "17:00")
.dayOfWeek(DayOfWeek.MONDAY)
.zoneId("Europe/Brussels")
);
}

Removing a schedule

When you need even more control it is also possible to remove schedules you registered. This can for example be used to apply changes made to a database. The WorkSchedule interface also provides the void unregister(Handle) method to remove a schedule registered earlier. The example below is simple and only deals with one Handle, but you may scale this to as many as you need.

import net.democritus.processautomation.WorkSchedule.Handle;

private Handle schedule;

@Timeout
public void reconfigureSchedule() {
WorkScheduleDetails details = computeSchedule();
if (schedule != null) {
defaultWorkSchedule.unregister(schedule);
}
schedule = defaultWorkSchedule.register(details);
}

Querying the schedule

If at any point in time you want to know if there is an active schedule, the boolean isActiveAt(Instant) method will provide you with the answer.

public void checkSchedule() {
Instant now = Instant.now();
if (defaultWorkSchedule.isActiveAt(now)) {
// it is active
} else {
// no it is not
}
}