[New] StrolchJobs now have an ID as well as a name, and model can also specify a delay:

<Resource Id="reloadPolicies" Name="Reload Policies" Type="StrolchJob">
       <ParameterBag Id="parameters" Name="Parameters" Type="Parameters">
           <Parameter Id="className" Name="Class Name" Type="String"
                   Value="li.strolch.policy.ReloadPoliciesJob"/>
           <Parameter Id="mode" Name="Job Mode" Type="String"
                   Interpretation="Enumeration" Uom="JobMode" Value="Recurring"/>
           <Parameter Id="startDate" Name="Job StartDate" Type="Date" Value="-"/>
           <Parameter Id="initialDelay" Name="Initial Delay" Type="Integer"
                   Interpretation="TimeUnit" Uom="MINUTES" Value="1"/>
           <Parameter Id="delay" Name="Delay" Type="Integer"
                   Interpretation="TimeUnit" Uom="HOURS" Value="1"/>
        </ParameterBag>
    </Resource>
This commit is contained in:
Robert von Burg 2020-05-02 23:33:57 +02:00
parent 8f0d264d8b
commit 1e3e97737b
9 changed files with 93 additions and 24 deletions

View File

@ -5,8 +5,8 @@ import li.strolch.privilege.model.PrivilegeContext;
public class ReloadJobsJob extends StrolchJob {
public ReloadJobsJob(StrolchAgent agent, String name, JobMode mode) {
super(agent, name, mode);
public ReloadJobsJob(StrolchAgent agent, String id, String name, JobMode mode) {
super(agent, id, name, mode);
}
@Override

View File

@ -46,6 +46,7 @@ public abstract class StrolchJob implements Runnable, Restrictable {
protected static final Logger logger = LoggerFactory.getLogger(StrolchJob.class);
private final StrolchAgent agent;
private final String id;
private final String name;
private JobMode mode;
@ -71,8 +72,9 @@ public abstract class StrolchJob implements Runnable, Restrictable {
private ConfigureMethod configureMethod;
private ZonedDateTime cronStartDate;
public StrolchJob(StrolchAgent agent, String name, JobMode jobMode) {
public StrolchJob(StrolchAgent agent, String id, String name, JobMode jobMode) {
this.agent = agent;
this.id = id;
this.name = name;
this.mode = jobMode;
this.first = true;
@ -125,6 +127,10 @@ public abstract class StrolchJob implements Runnable, Restrictable {
return this;
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
@ -398,7 +404,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
public JsonObject toJson() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(Tags.Json.NAME, getName());
jsonObject.addProperty(Tags.Json.ID, this.id);
jsonObject.addProperty(Tags.Json.NAME, this.name);
jsonObject.addProperty(Tags.Json.REALM, this.realmName);
jsonObject.addProperty("mode", this.mode.name());
jsonObject.addProperty("configureMethod", this.configureMethod == null ? "-" : this.configureMethod.name());

View File

@ -8,11 +8,13 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.agent.api.StrolchComponent;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.IntegerParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
@ -46,9 +48,13 @@ public class StrolchJobsHandler extends StrolchComponent {
List<StrolchJob> jobs = new ArrayList<>();
// copy any already existing programmatically added jobs
if (this.jobs != null) {
this.jobs.values().forEach(value -> {
// cancel any schedule jobs
value.cancel(false);
// copy any already existing programmatically added jobs
if (value.getConfigureMethod() == ConfigureMethod.Programmatic)
jobs.add(value);
});
@ -61,13 +67,26 @@ public class StrolchJobsHandler extends StrolchComponent {
tx.streamResources(TYPE_STROLCH_JOB).forEach(jobRes -> {
String className = jobRes.getParameter(PARAM_CLASS_NAME, true).getValue();
String cron = jobRes.getParameter(PARAM_CRON, true).getValue();
DateParameter startDateP = jobRes.getParameter(PARAM_START_DATE, true);
JobMode mode = JobMode.valueOf(jobRes.getParameter(PARAM_MODE, true).getValue());
StrolchJob job = instantiateJob(className, jobRes.getName(), mode);
job.setConfigureMethod(ConfigureMethod.Model).setMode(mode)
.setCronExpression(cron, startDateP.toZonedDateTime());
StrolchJob job = instantiateJob(className, jobRes.getId(), jobRes.getName(), mode);
job.setConfigureMethod(ConfigureMethod.Model).setMode(mode);
if (jobRes.hasParameter(PARAM_CRON)) {
String cron = jobRes.getParameter(PARAM_CRON, true).getValue();
job.setCronExpression(cron, startDateP.toZonedDateTime());
} else if (jobRes.hasParameter(PARAM_INITIAL_DELAY) && jobRes.hasParameter(PARAM_DELAY)) {
IntegerParameter initialDelayP = jobRes.getParameter(PARAM_INITIAL_DELAY, true);
IntegerParameter delayP = jobRes.getParameter(PARAM_DELAY, true);
TimeUnit initialDelayUnit = TimeUnit.valueOf(initialDelayP.getUom());
TimeUnit delayUnit = TimeUnit.valueOf(delayP.getUom());
job.setDelay(initialDelayP.getValue(), initialDelayUnit, delayP.getValue(), delayUnit);
} else {
logger.error("Job " + jobRes.getId()
+ " is inconsistent, as either cron, or initialDelay/delay is missing!");
return;
}
jobs.add(job);
logger.info("Added job " + job.getName() + ": " + job.getCron() + " from model.");
@ -77,7 +96,8 @@ public class StrolchJobsHandler extends StrolchComponent {
});
StrolchAgent agent = getContainer().getAgent();
ReloadJobsJob reloadJobsJob = new ReloadJobsJob(agent, ReloadJobsJob.class.getSimpleName(), JobMode.Manual);
ReloadJobsJob reloadJobsJob = new ReloadJobsJob(agent, ReloadJobsJob.class.getSimpleName(),
ReloadJobsJob.class.getSimpleName(), JobMode.Manual);
reloadJobsJob.setConfigureMethod(ConfigureMethod.Programmatic);
jobs.add(reloadJobsJob);
@ -86,7 +106,7 @@ public class StrolchJobsHandler extends StrolchComponent {
}
@SuppressWarnings("unchecked")
private StrolchJob instantiateJob(String className, String name, JobMode mode) {
private StrolchJob instantiateJob(String className, String id, String name, JobMode mode) {
Class<StrolchJob> clazz;
try {
clazz = (Class<StrolchJob>) Class.forName(className);
@ -94,15 +114,15 @@ public class StrolchJobsHandler extends StrolchComponent {
throw new IllegalArgumentException("StrolchJob class " + className + " does not exist!");
}
return instantiateJob(clazz, name, mode);
return instantiateJob(clazz, id, name, mode);
}
private StrolchJob instantiateJob(Class<? extends StrolchJob> clazz, String name, JobMode mode) {
private StrolchJob instantiateJob(Class<? extends StrolchJob> clazz, String id, String name, JobMode mode) {
StrolchJob strolchJob;
try {
Constructor<? extends StrolchJob> constructor = clazz
.getConstructor(StrolchAgent.class, String.class, JobMode.class);
strolchJob = constructor.newInstance(getContainer().getAgent(), name, mode);
.getConstructor(StrolchAgent.class, String.class, String.class, JobMode.class);
strolchJob = constructor.newInstance(getContainer().getAgent(), id, name, mode);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to instantiate job " + clazz.getName(), e);
}
@ -110,13 +130,15 @@ public class StrolchJobsHandler extends StrolchComponent {
}
public StrolchJob registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(), JobMode.Manual);
StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(),
strolchJobClass.getSimpleName(), JobMode.Manual);
job.setConfigureMethod(ConfigureMethod.Programmatic);
return register(job).schedule();
}
public StrolchJob register(Class<? extends StrolchJob> strolchJobClass) {
StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(), JobMode.Manual);
StrolchJob job = instantiateJob(strolchJobClass, strolchJobClass.getSimpleName(),
strolchJobClass.getSimpleName(), JobMode.Manual);
job.setConfigureMethod(ConfigureMethod.Programmatic);
return register(job);
}

View File

@ -7,8 +7,8 @@ import li.strolch.privilege.model.PrivilegeContext;
public class ReloadPoliciesJob extends StrolchJob {
public ReloadPoliciesJob(StrolchAgent agent, String name, JobMode mode) {
super(agent, name, mode);
public ReloadPoliciesJob(StrolchAgent agent, String id, String name, JobMode mode) {
super(agent, id, name, mode);
}
@Override

View File

@ -7,8 +7,8 @@ import li.strolch.privilege.model.PrivilegeContext;
public class ReloadPrivilegeHandlerJob extends StrolchJob {
public ReloadPrivilegeHandlerJob(StrolchAgent agent, String name, JobMode jobMode) {
super(agent, name, jobMode);
public ReloadPrivilegeHandlerJob(StrolchAgent agent, String id, String name, JobMode jobMode) {
super(agent, id, name, jobMode);
}
@Override

View File

@ -75,6 +75,8 @@ public class StrolchConstants extends StrolchModelConstants {
public static final String PARAM_CLASS_NAME = "className";
public static final String PARAM_CRON = "cron";
public static final String PARAM_INITIAL_DELAY = "initialDelay";
public static final String PARAM_DELAY = "delay";
public static final String PARAM_START_DATE = "startDate";
public static final String PARAM_MODE = "mode";

View File

@ -596,6 +596,30 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
return bag.hasParameter(paramKey);
}
/**
* Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the id
* {@link StrolchModelConstants#BAG_RELATIONS}
*
* @param paramKey
* the key of the {@link Parameter} to be found
*
* @return true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given
* bagKey. False is returned if the {@link ParameterBag} does not exist, or the {@link Parameter} does not exist on
* the {@link ParameterBag}
*/
@Override
public boolean hasRelation(String paramKey) {
if (this.parameterBagMap == null) {
return false;
}
ParameterBag bag = this.parameterBagMap.get(BAG_RELATIONS);
if (bag == null) {
return false;
}
return bag.hasParameter(paramKey);
}
/**
* Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given
* bagKey

View File

@ -331,6 +331,19 @@ public interface ParameterBagContainer extends StrolchElement {
*/
boolean hasParameter(String paramKey);
/**
* Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the id
* {@link StrolchModelConstants#BAG_RELATIONS}
*
* @param paramKey
* the key of the {@link Parameter} to be found
*
* @return true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given
* bagKey. False is returned if the {@link ParameterBag} does not exist, or the {@link Parameter} does not exist on
* the {@link ParameterBag}
*/
boolean hasRelation(String paramKey);
/**
* Returns true if the {@link Parameter} with the given paramKey exists on the {@link ParameterBag} with the given
* bagKey

View File

@ -12,13 +12,14 @@ import li.strolch.privilege.model.PrivilegeContext;
public class ArchiveExecutedActivitiesJob extends StrolchJob {
public ArchiveExecutedActivitiesJob(StrolchAgent agent, String name, JobMode jobMode) {
super(agent, name, jobMode);
public ArchiveExecutedActivitiesJob(StrolchAgent agent, String id, String name, JobMode jobMode) {
super(agent, id, name, jobMode);
}
public ArchiveExecutedActivitiesJob(StrolchAgent agent, JobMode jobMode, long initialDelay,
TimeUnit initialDelayTimeUnit, long delay, TimeUnit delayTimeUnit) {
super(agent, ArchiveExecutedActivitiesJob.class.getSimpleName(), jobMode);
super(agent, ArchiveExecutedActivitiesJob.class.getSimpleName(),
ArchiveExecutedActivitiesJob.class.getSimpleName(), jobMode);
setDelay(initialDelay, initialDelayTimeUnit, delay, delayTimeUnit);
}