io.vlingo.actors.Evictable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vlingo-actors Show documentation
Show all versions of vlingo-actors Show documentation
Type safe Actor Model toolkit for reactive concurrency and resiliency using Java and other JVM languages.
package io.vlingo.actors;
final class Evictable {
final Actor actor;
private long activeOn;
Evictable(Actor actor) {
this.actor = actor;
this.activeOn = System.currentTimeMillis();
}
void receivedMessage() {
activeOn(System.currentTimeMillis());
}
void activeOn(long activeOn) {
this.activeOn = activeOn;
}
boolean stop(long thresholdMillis) {
return stop(System.currentTimeMillis(), thresholdMillis);
}
boolean stop(long referenceMillis, long thresholdMillis) {
if (!actor.definition().evictable) { return false; }
final int pendingMessageCount = actor.lifeCycle.environment.mailbox.pendingMessages();
if (isStale(referenceMillis, thresholdMillis)) {
if (pendingMessageCount == 0) {
actor.selfAs(Stoppable.class).stop();
return true;
}
else {
actor.logger().warn(
"Inactive Actor at {} failed to evict because it has {} undelivered messages in its mailbox",
actor.address(), pendingMessageCount);
}
}
return false;
}
boolean isStale(long thresholdMillis) {
return isStale(System.currentTimeMillis(), thresholdMillis);
}
boolean isStale(long referenceMillis, long thresholdMillis) {
return activeOn < referenceMillis - thresholdMillis;
}
}