-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pool can have a companion ticker thread (#236)
* Pool can have a companion ticker thread * checkstyle * checkstyle * fine grain ticker with duration.lowest * ticker error handling
- Loading branch information
1 parent
ec8d9f8
commit a892cb4
Showing
7 changed files
with
391 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/org/openstreetmap/atlas/utilities/threads/LogTicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.openstreetmap.atlas.utilities.threads; | ||
|
||
import org.openstreetmap.atlas.utilities.scalars.Duration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author matthieun | ||
*/ | ||
public class LogTicker extends Ticker | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(LogTicker.class); | ||
|
||
public LogTicker(final String name, final Duration tickerTime) | ||
{ | ||
super(name, tickerTime); | ||
} | ||
|
||
@Override | ||
protected void tickAction(final Duration sinceStart) | ||
{ | ||
logger.info("{}: {}", getName(), sinceStart); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/org/openstreetmap/atlas/utilities/threads/Ticker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.openstreetmap.atlas.utilities.threads; | ||
|
||
import java.io.Closeable; | ||
|
||
import org.openstreetmap.atlas.utilities.scalars.Duration; | ||
import org.openstreetmap.atlas.utilities.time.Time; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Ticker companion. | ||
* | ||
* @author matthieun | ||
*/ | ||
public abstract class Ticker implements Runnable, Closeable | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(Ticker.class); | ||
private static final Duration CHECK_TIME = Duration.milliseconds(500); | ||
|
||
private final String name; | ||
private final Duration tickerTime; | ||
|
||
// This tells the ticker it is time to stop ticking. | ||
private volatile boolean stop; | ||
|
||
/** | ||
* @param name | ||
* The name of the ticker companion | ||
* @param tickerTime | ||
* The duration between each tick. It is indicative only. | ||
*/ | ||
public Ticker(final String name, final Duration tickerTime) | ||
{ | ||
this.name = name; | ||
this.tickerTime = tickerTime; | ||
this.stop = false; | ||
} | ||
|
||
@Override | ||
public void close() | ||
{ | ||
this.stop = true; | ||
} | ||
|
||
public String getName() | ||
{ | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
final Time start = Time.now(); | ||
Time lastCheck = Time.now(); | ||
while (!this.stop) | ||
{ | ||
// Sleep small, to check regularly. If the thread is "closed" it will then die fairly | ||
// soon even if the ticker time is really long. | ||
CHECK_TIME.lowest(this.tickerTime).sleep(); | ||
if (lastCheck.elapsedSince().isMoreThan(this.tickerTime)) | ||
{ | ||
try | ||
{ | ||
tickAction(start.elapsedSince()); | ||
} | ||
catch (final Exception e) | ||
{ | ||
// In case of any error in tickAction, kill the ticker silently, with a nice | ||
// error message. | ||
logger.error("{} tick action failed! Associated job should not be affected.", | ||
getName(), e); | ||
} | ||
lastCheck = Time.now(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "Ticker [name=" + this.name + ", tickerTime=" + this.tickerTime + "]"; | ||
} | ||
|
||
/** | ||
* Act upon a tick event. | ||
* | ||
* @param sinceStart | ||
* The duration elapsed since the start of the ticker. | ||
*/ | ||
protected abstract void tickAction(Duration sinceStart); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.