com.xlrit.gears.base.schedule.ScheduledRunnable Maven / Gradle / Ivy
package com.xlrit.gears.base.schedule;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.util.Objects.requireNonNull;
public abstract class ScheduledRunnable implements Runnable {
public static final String EFFECTIVELY_NEVER = "0 0 0 1 1 ? 2099";
private static final Logger LOG = LoggerFactory.getLogger(ScheduledRunnable.class);
@Override
@Transactional
public void run() {
if (!isEnabled())
return;
try {
runScheduled();
}
catch (RuntimeException e) {
LOG.error("An error occurred during a scheduled process", e);
}
}
@SuppressWarnings("unused")
public String getEffectiveCronExpression() {
boolean enabled = isEnabled();
String result = enabled ? requireNonNull(getCronExpression()) : EFFECTIVELY_NEVER;
LOG.info("Effective cron expression for {}: {}", getClass().getSimpleName(), result);
return result;
}
protected abstract boolean isEnabled();
protected abstract String getCronExpression();
protected abstract void runScheduled();
}