[New] Added last execution to StrolchJob

This commit is contained in:
Robert von Burg 2018-09-10 18:29:19 +02:00
parent 00ade23b4d
commit b0231cdf91
4 changed files with 96 additions and 52 deletions

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -33,6 +33,18 @@ public interface ComponentContainer {
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 PrivilegeHandler getPrivilegeHandler() throws IllegalArgumentException;
@ -42,31 +54,31 @@ public interface ComponentContainer {
public abstract Set<String> getRealmNames();
/**
* Returns the {@link StrolchRealm} with the given name. To get the default realm, use the constant
* {@link StrolchConstants#DEFAULT_REALM}.
*
* Returns the {@link StrolchRealm} with the given name. To get the default realm, use the constant {@link
* StrolchConstants#DEFAULT_REALM}.
*
* @param realm
* the name of the {@link StrolchRealm} to return
*
* the name of the {@link StrolchRealm} to return
*
* @return the {@link StrolchRealm} with the given name
*
*
* @throws StrolchException
* if the {@link StrolchRealm} does not exist with the given name
* if the {@link StrolchRealm} does not exist with the given name
*/
public abstract StrolchRealm getRealm(String realm) throws StrolchException;
/**
* Returns the default {@link StrolchRealm} for the user with the given {@link Certificate}. This is done by
* querying the property {@link StrolchConstants#PROP_REALM} from the certificate.
*
*
* @param certificate
* the {@link Certificate} from which to retrieve the name of the {@link StrolchRealm} to return
*
* the {@link Certificate} from which to retrieve the name of the {@link StrolchRealm} to return
*
* @return the {@link StrolchRealm}
*
*
* @throws StrolchException
* if the user does not have a {@link StrolchConstants#PROP_REALM} property configured, and the default
* realm is not configured, or if the realm does not exist with the found value
* if the user does not have a {@link StrolchConstants#PROP_REALM} property configured, and the default realm is
* not configured, or if the realm does not exist with the found value
*/
public abstract StrolchRealm getRealm(Certificate certificate) throws StrolchException;

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -25,15 +25,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
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.agent.api.*;
import li.strolch.exception.StrolchException;
import li.strolch.privilege.model.Certificate;
import li.strolch.runtime.StrolchConstants;
@ -43,6 +35,8 @@ import li.strolch.runtime.configuration.StrolchConfigurationException;
import li.strolch.runtime.privilege.PrivilegeHandler;
import li.strolch.utils.helper.StringHelper;
import li.strolch.utils.helper.SystemHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ComponentContainerImpl implements ComponentContainer {
@ -118,8 +112,9 @@ public class ComponentContainerImpl implements ComponentContainer {
realmName = StrolchConstants.DEFAULT_REALM;
} else {
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(),
StrolchConstants.PROP_REALM, StrolchConstants.DEFAULT_REALM));
throw new StrolchException(MessageFormat
.format(msg, certificate.getUsername(), StrolchConstants.PROP_REALM,
StrolchConstants.DEFAULT_REALM));
}
}
@ -156,8 +151,8 @@ public class ComponentContainerImpl implements ComponentContainer {
@SuppressWarnings("unchecked")
Class<StrolchComponent> strolchComponentClass = (Class<StrolchComponent>) implClass;
Constructor<StrolchComponent> constructor = strolchComponentClass.getConstructor(ComponentContainer.class,
String.class);
Constructor<StrolchComponent> constructor = strolchComponentClass
.getConstructor(ComponentContainer.class, String.class);
StrolchComponent strolchComponent = constructor.newInstance(this, componentName);
strolchComponent.setup(componentConfiguration);
@ -170,8 +165,7 @@ public class ComponentContainerImpl implements ComponentContainer {
msg = MessageFormat.format(msg, componentName, e.getMessage());
throw new StrolchConfigurationException(msg, e);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SecurityException
| IllegalArgumentException | InvocationTargetException e) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
String msg = "Could not load class for component {0} due to: {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, componentName, e.getMessage());
@ -221,8 +215,8 @@ public class ComponentContainerImpl implements ComponentContainer {
long took = System.nanoTime() - start;
msg = "{0}:{1} Strolch Container setup with {2} components. Took {3}"; //$NON-NLS-1$
logger.info(MessageFormat.format(msg, applicationName, environment, this.componentMap.size(),
formatNanoDuration(took)));
logger.info(MessageFormat
.format(msg, applicationName, environment, this.componentMap.size(), formatNanoDuration(took)));
}
public void initialize(StrolchConfiguration strolchConfiguration) {
@ -243,8 +237,8 @@ public class ComponentContainerImpl implements ComponentContainer {
long took = System.nanoTime() - start;
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(),
formatNanoDuration(took)));
logger.info(MessageFormat
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
}
public void start() {
@ -264,8 +258,8 @@ public class ComponentContainerImpl implements ComponentContainer {
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$
logger.info(MessageFormat.format(msg, applicationName, environment, this.controllerMap.size(),
formatNanoDuration(took)));
logger.info(MessageFormat
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
logger.info(MessageFormat.format("System: {0}", SystemHelper.asString())); //$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;
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(),
formatNanoDuration(took)));
logger.info(MessageFormat
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
}
this.state = ComponentState.STOPPED;
@ -313,8 +307,8 @@ public class ComponentContainerImpl implements ComponentContainer {
long took = System.nanoTime() - start;
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(),
formatNanoDuration(took)));
logger.info(MessageFormat
.format(msg, applicationName, environment, this.controllerMap.size(), formatNanoDuration(took)));
this.controllerMap.clear();
this.componentMap.clear();
}

View File

@ -2,6 +2,7 @@ package li.strolch.job;
import static li.strolch.model.Tags.AGENT;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
@ -14,6 +15,7 @@ import java.util.concurrent.TimeUnit;
import com.google.gson.JsonObject;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.agent.api.StrolchComponent;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.handler.operationslog.LogMessage;
import li.strolch.handler.operationslog.LogSeverity;
@ -50,6 +52,7 @@ public abstract class StrolchJob implements Runnable {
private long nrOfExecutions;
private boolean first;
private ScheduledFuture<?> future;
private ZonedDateTime lastExecution;
private Throwable lastException;
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);
}
/**
* 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
*
@ -151,6 +170,7 @@ public abstract class StrolchJob implements Runnable {
public void runNow() {
synchronized (this) {
runAsAgent(this::execute);
this.lastExecution = ZonedDateTime.now();
this.nrOfExecutions++;
}
}
@ -175,6 +195,7 @@ public abstract class StrolchJob implements Runnable {
}
}
this.lastExecution = ZonedDateTime.now();
this.nrOfExecutions++;
if (this.first) {
@ -245,20 +266,36 @@ public abstract class StrolchJob implements Runnable {
public JsonObject toJson() {
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.REALM, this.realmName);
jsonObject.addProperty("mode", this.mode.name());
jsonObject.addProperty("initialDelay", this.initialDelay);
jsonObject.addProperty("initialDelayTimeUnit", this.initialDelayTimeUnit.name());
jsonObject.addProperty("delay", this.delay);
jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit.name());
jsonObject.addProperty("nextExecution", nextExecution);
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("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;
}

View File

@ -38,7 +38,7 @@ public class StrolchJobsHandler extends StrolchComponent {
super.initialize(configuration);
}
public void registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
public StrolchJob registerAndScheduleJob(Class<? extends StrolchJob> strolchJobClass) {
StrolchJob strolchJob;
try {
@ -54,6 +54,7 @@ public class StrolchJobsHandler extends StrolchComponent {
strolchJob.schedule();
this.jobs.put(strolchJob.getName(), strolchJob);
return strolchJob;
}
public List<StrolchJob> getJobs(Certificate cert) {