Skip to content

Commit

Permalink
remove deprecated observer from the roller framework (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-mlb authored Oct 2, 2023
1 parent 2e60896 commit a46d553
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
16 changes: 8 additions & 8 deletions src/main/java/emissary/roll/RollManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
* RollManager handles all incremental rolls for configured objects within the framework
*/
public class RollManager implements Observer {
public class RollManager implements PropertyChangeListener {
static final Logger log = LoggerFactory.getLogger(RollManager.class);
public static final String CFG_ROLL_MANAGER_THREADS = "ROLL_MANAGER_THREADS";
int executorThreadCount = 10;
Expand Down Expand Up @@ -82,7 +82,7 @@ public final void addRoller(Roller r) {
exec.scheduleAtFixedRate(r, r.getPeriod(), r.getPeriod(), r.getTimeUnit());
}
if (progress) {
r.addObserver(this);
r.addPropertyChangeListener(this);
}
if (time || progress) {
rollers.add(r);
Expand All @@ -93,12 +93,12 @@ public final void addRoller(Roller r) {
}

@Override
public void update(Observable o, Object arg) {
if (rollers.contains(o)) {
Roller r = (Roller) o;
public void propertyChange(PropertyChangeEvent evt) {
if (rollers.contains((Roller) evt.getNewValue())) {
Roller r = (Roller) evt.getNewValue();
// only schedule one time when we're notified
if (r.setProgressScheduled()) {
exec.execute((Roller) o);
exec.execute((Roller) evt.getNewValue());
}

}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/emissary/roll/Roller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Observable;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;

/**
* Stateful object for the RollManager to track progress of a provided Rollable.
*/
public class Roller extends Observable implements Runnable {
public class Roller implements Runnable {

static final Logger log = LoggerFactory.getLogger(Roller.class);

/** Constant to refer to roll interval in config files **/
public static final String CFG_ROLL_INTERVAL = "ROLL_INTERVAL";

private final PropertyChangeSupport support;

private final long max;
private volatile long progress;
private final TimeUnit t;
Expand All @@ -32,12 +35,21 @@ public Roller(long max, TimeUnit t, long period, Rollable r) {
this.t = t;
this.period = period;
this.r = r;
this.support = new PropertyChangeSupport(this);
}

public Roller(TimeUnit t, long period, Rollable r) {
this(0, t, period, r);
}

public void addPropertyChangeListener(PropertyChangeListener pcl) {
support.addPropertyChangeListener(pcl);
}

public void removePropertyChangeListener(PropertyChangeListener pcl) {
support.removePropertyChangeListener(pcl);
}

public final long incrementProgress() {
return incrementProgress(1L);
}
Expand All @@ -47,8 +59,7 @@ public final long incrementProgress(long val) {
lock.lock();
progress += val;
if (progress >= max) {
setChanged();
notifyObservers();
support.firePropertyChange("roll", null, this);
}
return progress;
} finally {
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/emissary/roll/RollManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -34,7 +34,7 @@ void testObserve() throws IOException {
RollManager rm = new RollManager(ConfigUtil.getConfigInfo(this.getClass()));
Roller r = new Roller(1, TimeUnit.DAYS, 1, new RollableTest());
RollTestObserver o = new RollTestObserver();
r.addObserver(o);
r.addPropertyChangeListener(o);
rm.addRoller(r);
r.incrementProgress();
Assertions.assertNotNull(o.o, "Roller notified");
Expand Down Expand Up @@ -85,14 +85,14 @@ public void close() {
assertFalse(rm.exec.getQueue().contains(r));
}

static class RollTestObserver implements Observer {
Observable o;
Object arg;
static class RollTestObserver implements PropertyChangeListener {
Object o;
String prop;

@Override
public void update(Observable o, Object arg) {
this.o = o;
this.arg = arg;
public void propertyChange(PropertyChangeEvent evt) {
this.prop = evt.getPropertyName();
this.o = evt.getNewValue();
}

}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/emissary/roll/RollableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import emissary.test.core.junit5.UnitTest;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class RollableTest extends UnitTest implements Rollable, Observer {
public class RollableTest extends UnitTest implements Rollable, PropertyChangeListener {
boolean wasRolled;
long updateCount;
ArrayBlockingQueue<Object> x = new ArrayBlockingQueue<>(1);
Expand Down Expand Up @@ -36,9 +36,9 @@ public void close() throws IOException {}

@SuppressWarnings("unused")
@Override
public void update(Observable o, Object arg) {
if (o instanceof Roller) {
Roller r = (Roller) o;
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() instanceof Roller) {
Roller r = (Roller) evt.getNewValue();

updateCount++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/emissary/roll/RollerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RollerTest extends UnitTest {
void testRoller() {
final RollableTest tr = new RollableTest();
final Roller r = new Roller(1, TimeUnit.DAYS, 1, tr);
r.addObserver(tr);
r.addPropertyChangeListener(tr);
r.incrementProgress();

r.run();
Expand All @@ -28,7 +28,7 @@ void testRoller() {
void testShouldRoll() {
RollableTest tr = new RollableTest();
Roller r = new Roller(1, TimeUnit.MILLISECONDS, 1, tr);
r.addObserver(tr);
r.addPropertyChangeListener(tr);
r.incrementProgress();

r.run();
Expand Down

0 comments on commit a46d553

Please sign in to comment.