[New] Added last execution to StrolchJob
This commit is contained in:
parent
00ade23b4d
commit
b0231cdf91
|
@ -33,6 +33,18 @@ public interface ComponentContainer {
|
||||||
|
|
||||||
public abstract boolean hasComponent(Class<?> clazz);
|
public abstract boolean hasComponent(Class<?> clazz);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the reference to the {@link StrolchComponent} with the given name, if it exists. If it does not exist, an
|
||||||
|
* {@link IllegalArgumentException} is thrown
|
||||||
|
*
|
||||||
|
* @param clazz
|
||||||
|
* the type of component to return
|
||||||
|
*
|
||||||
|
* @return the component with the given name
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
* if the component does not exist
|
||||||
|
*/
|
||||||
public abstract <T> T getComponent(Class<T> clazz) throws IllegalArgumentException;
|
public abstract <T> T getComponent(Class<T> clazz) throws IllegalArgumentException;
|
||||||
|
|
||||||
public abstract PrivilegeHandler getPrivilegeHandler() throws IllegalArgumentException;
|
public abstract PrivilegeHandler getPrivilegeHandler() throws IllegalArgumentException;
|
||||||
|
@ -42,8 +54,8 @@ public interface ComponentContainer {
|
||||||
public abstract Set<String> getRealmNames();
|
public abstract Set<String> getRealmNames();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the {@link StrolchRealm} with the given name. To get the default realm, use the constant
|
* Returns the {@link StrolchRealm} with the given name. To get the default realm, use the constant {@link
|
||||||
* {@link StrolchConstants#DEFAULT_REALM}.
|
* StrolchConstants#DEFAULT_REALM}.
|
||||||
*
|
*
|
||||||
* @param realm
|
* @param realm
|
||||||
* the name of the {@link StrolchRealm} to return
|
* the name of the {@link StrolchRealm} to return
|
||||||
|
@ -65,8 +77,8 @@ public interface ComponentContainer {
|
||||||
* @return the {@link StrolchRealm}
|
* @return the {@link StrolchRealm}
|
||||||
*
|
*
|
||||||
* @throws StrolchException
|
* @throws StrolchException
|
||||||
* if the user does not have a {@link StrolchConstants#PROP_REALM} property configured, and the default
|
* if the user does not have a {@link StrolchConstants#PROP_REALM} property configured, and the default realm is
|
||||||
* realm is not configured, or if the realm does not exist with the found value
|
* not configured, or if the realm does not exist with the found value
|
||||||
*/
|
*/
|
||||||
public abstract StrolchRealm getRealm(Certificate certificate) throws StrolchException;
|
public abstract StrolchRealm getRealm(Certificate certificate) throws StrolchException;
|
||||||
|
|
||||||
|
|
|
@ -25,15 +25,7 @@ import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import li.strolch.agent.api.*;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
|
||||||
import li.strolch.agent.api.ComponentState;
|
|
||||||
import li.strolch.agent.api.RealmHandler;
|
|
||||||
import li.strolch.agent.api.StrolchAgent;
|
|
||||||
import li.strolch.agent.api.StrolchComponent;
|
|
||||||
import li.strolch.agent.api.StrolchRealm;
|
|
||||||
import li.strolch.exception.StrolchException;
|
import li.strolch.exception.StrolchException;
|
||||||
import li.strolch.privilege.model.Certificate;
|
import li.strolch.privilege.model.Certificate;
|
||||||
import li.strolch.runtime.StrolchConstants;
|
import li.strolch.runtime.StrolchConstants;
|
||||||
|
@ -43,6 +35,8 @@ import li.strolch.runtime.configuration.StrolchConfigurationException;
|
||||||
import li.strolch.runtime.privilege.PrivilegeHandler;
|
import li.strolch.runtime.privilege.PrivilegeHandler;
|
||||||
import li.strolch.utils.helper.StringHelper;
|
import li.strolch.utils.helper.StringHelper;
|
||||||
import li.strolch.utils.helper.SystemHelper;
|
import li.strolch.utils.helper.SystemHelper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class ComponentContainerImpl implements ComponentContainer {
|
public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
|
@ -118,8 +112,9 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
realmName = StrolchConstants.DEFAULT_REALM;
|
realmName = StrolchConstants.DEFAULT_REALM;
|
||||||
} else {
|
} else {
|
||||||
String msg = "The User {0} is missing the property {1} and the Realm {2} can not be used as it does not exist!";
|
String msg = "The User {0} is missing the property {1} and the Realm {2} can not be used as it does not exist!";
|
||||||
throw new StrolchException(MessageFormat.format(msg, certificate.getUsername(),
|
throw new StrolchException(MessageFormat
|
||||||
StrolchConstants.PROP_REALM, StrolchConstants.DEFAULT_REALM));
|
.format(msg, certificate.getUsername(), StrolchConstants.PROP_REALM,
|
||||||
|
StrolchConstants.DEFAULT_REALM));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,8 +151,8 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Class<StrolchComponent> strolchComponentClass = (Class<StrolchComponent>) implClass;
|
Class<StrolchComponent> strolchComponentClass = (Class<StrolchComponent>) implClass;
|
||||||
Constructor<StrolchComponent> constructor = strolchComponentClass.getConstructor(ComponentContainer.class,
|
Constructor<StrolchComponent> constructor = strolchComponentClass
|
||||||
String.class);
|
.getConstructor(ComponentContainer.class, String.class);
|
||||||
StrolchComponent strolchComponent = constructor.newInstance(this, componentName);
|
StrolchComponent strolchComponent = constructor.newInstance(this, componentName);
|
||||||
strolchComponent.setup(componentConfiguration);
|
strolchComponent.setup(componentConfiguration);
|
||||||
|
|
||||||
|
@ -170,8 +165,7 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
msg = MessageFormat.format(msg, componentName, e.getMessage());
|
msg = MessageFormat.format(msg, componentName, e.getMessage());
|
||||||
throw new StrolchConfigurationException(msg, e);
|
throw new StrolchConfigurationException(msg, e);
|
||||||
|
|
||||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SecurityException
|
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
|
||||||
| IllegalArgumentException | InvocationTargetException e) {
|
|
||||||
|
|
||||||
String msg = "Could not load class for component {0} due to: {1}"; //$NON-NLS-1$
|
String msg = "Could not load class for component {0} due to: {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, componentName, e.getMessage());
|
msg = MessageFormat.format(msg, componentName, e.getMessage());
|
||||||
|
@ -221,8 +215,8 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
long took = System.nanoTime() - start;
|
long took = System.nanoTime() - start;
|
||||||
msg = "{0}:{1} Strolch Container setup with {2} components. Took {3}"; //$NON-NLS-1$
|
msg = "{0}:{1} Strolch Container setup with {2} components. Took {3}"; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, applicationName, environment, this.componentMap.size(),
|
logger.info(MessageFormat
|
||||||
formatNanoDuration(took)));
|
.format(msg, applicationName, environment, this.componentMap.size(), formatNanoDuration(took)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(StrolchConfiguration strolchConfiguration) {
|
public void initialize(StrolchConfiguration strolchConfiguration) {
|
||||||
|
@ -243,8 +237,8 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
long took = System.nanoTime() - start;
|
long took = System.nanoTime() - start;
|
||||||
msg = "{0}:{1} All {2} Strolch Components have been initialized. Took {3}"; //$NON-NLS-1$
|
msg = "{0}:{1} All {2} Strolch Components have been initialized. Took {3}"; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, applicationName, environment, this.controllerMap.size(),
|
logger.info(MessageFormat
|
||||||
formatNanoDuration(took)));
|
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
@ -264,8 +258,8 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
long took = System.nanoTime() - start;
|
long took = System.nanoTime() - start;
|
||||||
msg = "{0}:{1} All {2} Strolch Components started. Took {3}. Strolch is now ready to be used. Have fun =))"; //$NON-NLS-1$
|
msg = "{0}:{1} All {2} Strolch Components started. Took {3}. Strolch is now ready to be used. Have fun =))"; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, applicationName, environment, this.controllerMap.size(),
|
logger.info(MessageFormat
|
||||||
formatNanoDuration(took)));
|
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
|
||||||
logger.info(MessageFormat.format("System: {0}", SystemHelper.asString())); //$NON-NLS-1$
|
logger.info(MessageFormat.format("System: {0}", SystemHelper.asString())); //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format("Memory: {0}", SystemHelper.getMemorySummary())); //$NON-NLS-1$
|
logger.info(MessageFormat.format("Memory: {0}", SystemHelper.getMemorySummary())); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
@ -288,8 +282,8 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
long took = System.nanoTime() - start;
|
long took = System.nanoTime() - start;
|
||||||
msg = "{0}:{1} All {2} Strolch Components have been stopped. Took {3}"; //$NON-NLS-1$
|
msg = "{0}:{1} All {2} Strolch Components have been stopped. Took {3}"; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, applicationName, environment, this.controllerMap.size(),
|
logger.info(MessageFormat
|
||||||
formatNanoDuration(took)));
|
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.state = ComponentState.STOPPED;
|
this.state = ComponentState.STOPPED;
|
||||||
|
@ -313,8 +307,8 @@ public class ComponentContainerImpl implements ComponentContainer {
|
||||||
|
|
||||||
long took = System.nanoTime() - start;
|
long took = System.nanoTime() - start;
|
||||||
msg = "{0}:{1} All {2} Strolch Components have been destroyed! Took {3}"; //$NON-NLS-1$
|
msg = "{0}:{1} All {2} Strolch Components have been destroyed! Took {3}"; //$NON-NLS-1$
|
||||||
logger.info(MessageFormat.format(msg, applicationName, environment, this.controllerMap.size(),
|
logger.info(MessageFormat
|
||||||
formatNanoDuration(took)));
|
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
|
||||||
this.controllerMap.clear();
|
this.controllerMap.clear();
|
||||||
this.componentMap.clear();
|
this.componentMap.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package li.strolch.job;
|
||||||
|
|
||||||
import static li.strolch.model.Tags.AGENT;
|
import static li.strolch.model.Tags.AGENT;
|
||||||
|
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
@ -14,6 +15,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.agent.api.StrolchAgent;
|
import li.strolch.agent.api.StrolchAgent;
|
||||||
|
import li.strolch.agent.api.StrolchComponent;
|
||||||
import li.strolch.agent.api.StrolchRealm;
|
import li.strolch.agent.api.StrolchRealm;
|
||||||
import li.strolch.handler.operationslog.LogMessage;
|
import li.strolch.handler.operationslog.LogMessage;
|
||||||
import li.strolch.handler.operationslog.LogSeverity;
|
import li.strolch.handler.operationslog.LogSeverity;
|
||||||
|
@ -50,6 +52,7 @@ public abstract class StrolchJob implements Runnable {
|
||||||
private long nrOfExecutions;
|
private long nrOfExecutions;
|
||||||
private boolean first;
|
private boolean first;
|
||||||
private ScheduledFuture<?> future;
|
private ScheduledFuture<?> future;
|
||||||
|
private ZonedDateTime lastExecution;
|
||||||
private Throwable lastException;
|
private Throwable lastException;
|
||||||
|
|
||||||
public StrolchJob(StrolchAgent agent, JobMode jobMode, long initialDelay, TimeUnit initialDelayTimeUnit, long delay,
|
public StrolchJob(StrolchAgent agent, JobMode jobMode, long initialDelay, TimeUnit initialDelayTimeUnit, long delay,
|
||||||
|
@ -131,6 +134,22 @@ public abstract class StrolchJob implements Runnable {
|
||||||
getContainer().getPrivilegeHandler().runAsAgent(runnable);
|
getContainer().getPrivilegeHandler().runAsAgent(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the reference to the {@link StrolchComponent} with the given name, if it exists. If it does not exist, an
|
||||||
|
* {@link IllegalArgumentException} is thrown
|
||||||
|
*
|
||||||
|
* @param clazz
|
||||||
|
* the type of component to return
|
||||||
|
*
|
||||||
|
* @return the component with the given name
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
* if the component does not exist
|
||||||
|
*/
|
||||||
|
public <T> T getComponent(Class<T> clazz) throws IllegalArgumentException {
|
||||||
|
return getContainer().getComponent(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a {@link StrolchTransaction} for the default realm and certificate
|
* Opens a {@link StrolchTransaction} for the default realm and certificate
|
||||||
*
|
*
|
||||||
|
@ -151,6 +170,7 @@ public abstract class StrolchJob implements Runnable {
|
||||||
public void runNow() {
|
public void runNow() {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
runAsAgent(this::execute);
|
runAsAgent(this::execute);
|
||||||
|
this.lastExecution = ZonedDateTime.now();
|
||||||
this.nrOfExecutions++;
|
this.nrOfExecutions++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,6 +195,7 @@ public abstract class StrolchJob implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.lastExecution = ZonedDateTime.now();
|
||||||
this.nrOfExecutions++;
|
this.nrOfExecutions++;
|
||||||
|
|
||||||
if (this.first) {
|
if (this.first) {
|
||||||
|
@ -245,20 +266,36 @@ public abstract class StrolchJob implements Runnable {
|
||||||
public JsonObject toJson() {
|
public JsonObject toJson() {
|
||||||
JsonObject jsonObject = new JsonObject();
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
|
||||||
long delay = this.future.getDelay(TimeUnit.MILLISECONDS);
|
|
||||||
String nextExecution = ZonedDateTime.now().plus(delay, ChronoUnit.MILLIS)
|
|
||||||
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
|
||||||
|
|
||||||
jsonObject.addProperty(Tags.Json.NAME, getName());
|
jsonObject.addProperty(Tags.Json.NAME, getName());
|
||||||
jsonObject.addProperty(Tags.Json.REALM, this.realmName);
|
jsonObject.addProperty(Tags.Json.REALM, this.realmName);
|
||||||
|
jsonObject.addProperty("mode", this.mode.name());
|
||||||
jsonObject.addProperty("initialDelay", this.initialDelay);
|
jsonObject.addProperty("initialDelay", this.initialDelay);
|
||||||
jsonObject.addProperty("initialDelayTimeUnit", this.initialDelayTimeUnit.name());
|
jsonObject.addProperty("initialDelayTimeUnit", this.initialDelayTimeUnit.name());
|
||||||
jsonObject.addProperty("delay", this.delay);
|
jsonObject.addProperty("delay", this.delay);
|
||||||
jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit.name());
|
jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit.name());
|
||||||
|
|
||||||
|
if (this.lastExecution == null) {
|
||||||
|
jsonObject.addProperty("lastExecution", "-");
|
||||||
|
} else {
|
||||||
|
String lastExecution = this.lastExecution
|
||||||
|
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
|
||||||
|
jsonObject.addProperty("lastExecution", lastExecution);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.future == null) {
|
||||||
|
jsonObject.addProperty("nextExecution", "-");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
long delay = this.future.getDelay(TimeUnit.MILLISECONDS);
|
||||||
|
String nextExecution = ZonedDateTime.now().plus(delay, ChronoUnit.MILLIS)
|
||||||
|
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
|
||||||
jsonObject.addProperty("nextExecution", nextExecution);
|
jsonObject.addProperty("nextExecution", nextExecution);
|
||||||
|
}
|
||||||
|
|
||||||
jsonObject.addProperty("nrOfExecutions", this.nrOfExecutions);
|
jsonObject.addProperty("nrOfExecutions", this.nrOfExecutions);
|
||||||
jsonObject.addProperty("lastException",
|
|
||||||
this.lastException == null ? null : ExceptionHelper.formatExceptionMessage(this.lastException));
|
if (this.lastException != null)
|
||||||
|
jsonObject.addProperty("lastException", ExceptionHelper.formatExceptionMessage(this.lastException));
|
||||||
|
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class StrolchJobsHandler extends StrolchComponent {
|
||||||
super.initialize(configuration);
|
super.initialize(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
|
public StrolchJob registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
|
||||||
|
|
||||||
StrolchJob strolchJob;
|
StrolchJob strolchJob;
|
||||||
try {
|
try {
|
||||||
|
@ -54,6 +54,7 @@ public class StrolchJobsHandler extends StrolchComponent {
|
||||||
strolchJob.schedule();
|
strolchJob.schedule();
|
||||||
|
|
||||||
this.jobs.put(strolchJob.getName(), strolchJob);
|
this.jobs.put(strolchJob.getName(), strolchJob);
|
||||||
|
return strolchJob;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<StrolchJob> getJobs(Certificate cert) {
|
public List<StrolchJob> getJobs(Certificate cert) {
|
||||||
|
|
Loading…
Reference in New Issue