Skip to content

Commit

Permalink
#624 : do not launch cleaningTask from registrationStore several time.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Dec 3, 2018
1 parent 18f0888 commit 5807f49
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class InMemoryRegistrationStore implements CaliforniumRegistrationStore,
private ExpirationListener expirationListener;

private final ScheduledExecutorService schedExecutor;
private boolean started = false;
private final long cleanPeriod; // in seconds

public InMemoryRegistrationStore() {
Expand Down Expand Up @@ -438,20 +439,26 @@ public void setExpirationListener(ExpirationListener listener) {
* start the registration store, will start regular cleanup of dead registrations.
*/
@Override
public void start() {
schedExecutor.scheduleAtFixedRate(new Cleaner(), cleanPeriod, cleanPeriod, TimeUnit.SECONDS);
public synchronized void start() {
if (!started) {
started = true;
schedExecutor.scheduleAtFixedRate(new Cleaner(), cleanPeriod, cleanPeriod, TimeUnit.SECONDS);
}
}

/**
* Stop the underlying cleanup of the registrations.
*/
@Override
public void stop() {
schedExecutor.shutdownNow();
try {
schedExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.warn("Clean up registration thread was interrupted.", e);
if (started) {
started = false;
schedExecutor.shutdownNow();
try {
schedExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.warn("Clean up registration thread was interrupted.", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class RedisRegistrationStore implements CaliforniumRegistrationStore, Sta
private ExpirationListener expirationListener;

private final ScheduledExecutorService schedExecutor;
private boolean started = false;

private final long cleanPeriod; // in seconds
private final int cleanLimit; // maximum number to clean in a clean period
private final long gracePeriod; // in seconds
Expand Down Expand Up @@ -691,20 +693,26 @@ private Observation build(org.eclipse.californium.core.observe.Observation cfObs
* Start regular cleanup of dead registrations.
*/
@Override
public void start() {
schedExecutor.scheduleAtFixedRate(new Cleaner(), cleanPeriod, cleanPeriod, TimeUnit.SECONDS);
public synchronized void start() {
if (!started) {
started = true;
schedExecutor.scheduleAtFixedRate(new Cleaner(), cleanPeriod, cleanPeriod, TimeUnit.SECONDS);
}
}

/**
* Stop the underlying cleanup of the registrations.
*/
@Override
public void stop() {
schedExecutor.shutdownNow();
try {
schedExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.warn("Clean up registration thread was interrupted.", e);
public synchronized void stop() {
if (started) {
started = false;
schedExecutor.shutdownNow();
try {
schedExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.warn("Clean up registration thread was interrupted.", e);
}
}
}

Expand Down

0 comments on commit 5807f49

Please sign in to comment.