[Minor] Automated Code cleanup: field accessed in both synchronized and unsynchronized contexts
This commit is contained in:
parent
beaf2f1eb4
commit
2c362b52c9
|
@ -1,13 +1,13 @@
|
||||||
package li.strolch.job;
|
package li.strolch.job;
|
||||||
|
|
||||||
|
import static java.time.ZoneId.systemDefault;
|
||||||
|
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||||
import static li.strolch.model.Tags.AGENT;
|
import static li.strolch.model.Tags.AGENT;
|
||||||
import static li.strolch.runtime.StrolchConstants.SYSTEM_USER_AGENT;
|
import static li.strolch.runtime.StrolchConstants.SYSTEM_USER_AGENT;
|
||||||
import static li.strolch.utils.helper.StringHelper.formatMillisecondsDuration;
|
import static li.strolch.utils.helper.StringHelper.formatMillisecondsDuration;
|
||||||
import static li.strolch.utils.helper.StringHelper.isEmpty;
|
import static li.strolch.utils.helper.StringHelper.isEmpty;
|
||||||
|
|
||||||
import java.time.ZoneId;
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
@ -96,7 +96,7 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
return this.cron;
|
return this.cron;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StrolchJob setCronExpression(String cron, ZonedDateTime startDate) {
|
public void setCronExpression(String cron, ZonedDateTime startDate) {
|
||||||
this.cronExpression = CronExpression.createWithoutSeconds(cron);
|
this.cronExpression = CronExpression.createWithoutSeconds(cron);
|
||||||
this.cron = cron;
|
this.cron = cron;
|
||||||
this.cronStartDate = startDate.isBefore(ZonedDateTime.now()) ? ZonedDateTime.now() : startDate;
|
this.cronStartDate = startDate.isBefore(ZonedDateTime.now()) ? ZonedDateTime.now() : startDate;
|
||||||
|
@ -105,7 +105,6 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
this.initialDelayTimeUnit = null;
|
this.initialDelayTimeUnit = null;
|
||||||
this.delay = 0;
|
this.delay = 0;
|
||||||
this.delayTimeUnit = null;
|
this.delayTimeUnit = null;
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public StrolchJob setDelay(long initialDelay, TimeUnit initialDelayTimeUnit, long delay, TimeUnit delayTimeUnit) {
|
public StrolchJob setDelay(long initialDelay, TimeUnit initialDelayTimeUnit, long delay, TimeUnit delayTimeUnit) {
|
||||||
|
@ -174,6 +173,34 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
this.delayTimeUnit = delayTimeUnit;
|
this.delayTimeUnit = delayTimeUnit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized long getLastDuration() {
|
||||||
|
return lastDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized long getTotalDuration() {
|
||||||
|
return totalDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized long getNrOfExecutions() {
|
||||||
|
return nrOfExecutions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean isRunning() {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized String getRealmName() {
|
||||||
|
return realmName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZonedDateTime getLastExecution() {
|
||||||
|
return lastExecution;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized Exception getLastException() {
|
||||||
|
return lastException;
|
||||||
|
}
|
||||||
|
|
||||||
protected ComponentContainer getContainer() {
|
protected ComponentContainer getContainer() {
|
||||||
return getAgent().getContainer();
|
return getAgent().getContainer();
|
||||||
}
|
}
|
||||||
|
@ -282,7 +309,7 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
new LogMessage(this.realmName == null ? StrolchConstants.DEFAULT_REALM : this.realmName,
|
new LogMessage(this.realmName == null ? StrolchConstants.DEFAULT_REALM : this.realmName,
|
||||||
SYSTEM_USER_AGENT, Locator.valueOf(AGENT, "strolch-agent", StrolchAgent.getUniqueId()),
|
SYSTEM_USER_AGENT, Locator.valueOf(AGENT, "strolch-agent", StrolchAgent.getUniqueId()),
|
||||||
LogSeverity.Exception, LogMessageState.Information,
|
LogSeverity.Exception, LogMessageState.Information,
|
||||||
ResourceBundle.getBundle("strolch-agent"), "strolchjob.failed").withException(e)
|
ResourceBundle.getBundle("strolch-agent"), "job.failed").withException(e)
|
||||||
.value("jobName", getClass().getName())
|
.value("jobName", getClass().getName())
|
||||||
.value("reason", e));
|
.value("reason", e));
|
||||||
}
|
}
|
||||||
|
@ -361,7 +388,7 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("First execution of " + getName() + " will be at " + executionTime.format(
|
logger.info("First execution of " + getName() + " will be at " + executionTime.format(
|
||||||
DateTimeFormatter.ISO_OFFSET_DATE_TIME));
|
ISO_OFFSET_DATE_TIME));
|
||||||
|
|
||||||
long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis();
|
long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis();
|
||||||
this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS);
|
this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS);
|
||||||
|
@ -371,7 +398,7 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
long millis = this.initialDelayTimeUnit.toMillis(this.initialDelay);
|
long millis = this.initialDelayTimeUnit.toMillis(this.initialDelay);
|
||||||
logger.info("First execution of " + getName() + " will be at " + ZonedDateTime.now()
|
logger.info("First execution of " + getName() + " will be at " + ZonedDateTime.now()
|
||||||
.plus(millis, ChronoUnit.MILLIS)
|
.plus(millis, ChronoUnit.MILLIS)
|
||||||
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
|
.format(ISO_OFFSET_DATE_TIME));
|
||||||
|
|
||||||
this.future = getScheduledExecutor().schedule(this, this.initialDelay, this.initialDelayTimeUnit);
|
this.future = getScheduledExecutor().schedule(this, this.initialDelay, this.initialDelayTimeUnit);
|
||||||
}
|
}
|
||||||
|
@ -388,8 +415,8 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Next execution of " + getName() + " will be at " + executionTime.format(
|
logger.info(
|
||||||
DateTimeFormatter.ISO_OFFSET_DATE_TIME));
|
"Next execution of " + getName() + " will be at " + executionTime.format(ISO_OFFSET_DATE_TIME));
|
||||||
|
|
||||||
long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis();
|
long delay = PeriodDuration.between(ZonedDateTime.now(), executionTime).toMillis();
|
||||||
this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS);
|
this.future = getScheduledExecutor().schedule(this, delay, TimeUnit.MILLISECONDS);
|
||||||
|
@ -399,7 +426,7 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
long millis = this.delayTimeUnit.toMillis(this.delay);
|
long millis = this.delayTimeUnit.toMillis(this.delay);
|
||||||
logger.info("Next execution of " + getName() + " will be at " + ZonedDateTime.now()
|
logger.info("Next execution of " + getName() + " will be at " + ZonedDateTime.now()
|
||||||
.plus(millis, ChronoUnit.MILLIS)
|
.plus(millis, ChronoUnit.MILLIS)
|
||||||
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
|
.format(ISO_OFFSET_DATE_TIME));
|
||||||
|
|
||||||
this.future = getScheduledExecutor().schedule(this, this.delay, this.delayTimeUnit);
|
this.future = getScheduledExecutor().schedule(this, this.delay, this.delayTimeUnit);
|
||||||
}
|
}
|
||||||
|
@ -436,15 +463,16 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit == null ? "-" : this.delayTimeUnit.name());
|
jsonObject.addProperty("delayTimeUnit", this.delayTimeUnit == null ? "-" : this.delayTimeUnit.name());
|
||||||
|
|
||||||
jsonObject.addProperty("running", this.running);
|
jsonObject.addProperty("running", this.running);
|
||||||
jsonObject.addProperty("totalDuration", formatMillisecondsDuration(this.totalDuration));
|
jsonObject.addProperty("totalDuration", formatMillisecondsDuration(getTotalDuration()));
|
||||||
jsonObject.addProperty("lastDuration", formatMillisecondsDuration(this.lastDuration));
|
jsonObject.addProperty("lastDuration", formatMillisecondsDuration(getLastDuration()));
|
||||||
|
|
||||||
if (this.lastExecution == null) {
|
ZonedDateTime lastExecution = getLastExecution();
|
||||||
|
|
||||||
|
if (lastExecution == null) {
|
||||||
jsonObject.addProperty("lastExecution", "-");
|
jsonObject.addProperty("lastExecution", "-");
|
||||||
} else {
|
} else {
|
||||||
String lastExecution = this.lastExecution.format(
|
jsonObject.addProperty("lastExecution",
|
||||||
DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
|
lastExecution.format(ISO_OFFSET_DATE_TIME.withZone(systemDefault())));
|
||||||
jsonObject.addProperty("lastExecution", lastExecution);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.future == null) {
|
if (this.future == null) {
|
||||||
|
@ -454,14 +482,15 @@ public abstract class StrolchJob implements Runnable, Restrictable {
|
||||||
long delay = this.future.getDelay(TimeUnit.MILLISECONDS);
|
long delay = this.future.getDelay(TimeUnit.MILLISECONDS);
|
||||||
String nextExecution = ZonedDateTime.now()
|
String nextExecution = ZonedDateTime.now()
|
||||||
.plus(delay, ChronoUnit.MILLIS)
|
.plus(delay, ChronoUnit.MILLIS)
|
||||||
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
|
.format(ISO_OFFSET_DATE_TIME.withZone(systemDefault()));
|
||||||
jsonObject.addProperty("nextExecution", nextExecution);
|
jsonObject.addProperty("nextExecution", nextExecution);
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonObject.addProperty("nrOfExecutions", this.nrOfExecutions);
|
jsonObject.addProperty("nrOfExecutions", getNrOfExecutions());
|
||||||
|
|
||||||
if (this.lastException != null)
|
Exception lastException = getLastException();
|
||||||
jsonObject.addProperty("lastException", ExceptionHelper.formatExceptionMessage(this.lastException));
|
if (lastException != null)
|
||||||
|
jsonObject.addProperty("lastException", ExceptionHelper.formatExceptionMessage(lastException));
|
||||||
|
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue