diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java index d3cfd9e86c70..b2ab897bc154 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/events/NodeBulletinProcessingStrategy.java @@ -16,25 +16,34 @@ */ package org.apache.nifi.events; +import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.Set; +import java.util.Map; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.apache.nifi.reporting.Bulletin; /** - * + * Strategy should not hold more than 5 Bulletins in memory. */ public class NodeBulletinProcessingStrategy implements BulletinProcessingStrategy { + protected static final int MAX_ENTRIES = 5; private final Lock lock; private final Set bulletins; public NodeBulletinProcessingStrategy() { lock = new ReentrantLock(); - bulletins = new LinkedHashSet<>(); + bulletins = Collections.newSetFromMap(new LinkedHashMap(){ + private static final long serialVersionUID = 1L; + + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > MAX_ENTRIES; + } + }); } @Override diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java new file mode 100644 index 000000000000..36cf285c1bcf --- /dev/null +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/events/TestNodeBulletinProcessingStrategy.java @@ -0,0 +1,32 @@ +package org.apache.nifi.events; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TestNodeBulletinProcessingStrategy { + + @Test + public void testUpdate() { + + NodeBulletinProcessingStrategy nBulletinProcessingStrategy = new NodeBulletinProcessingStrategy(); + + nBulletinProcessingStrategy.update(new ComponentBulletin(1)); + nBulletinProcessingStrategy.update(new ComponentBulletin(2)); + nBulletinProcessingStrategy.update(new ComponentBulletin(3)); + nBulletinProcessingStrategy.update(new ComponentBulletin(4)); + nBulletinProcessingStrategy.update(new ComponentBulletin(5)); + assertEquals(5, nBulletinProcessingStrategy.getBulletins().size()); + + nBulletinProcessingStrategy.update(new ComponentBulletin(1)); + nBulletinProcessingStrategy.update(new ComponentBulletin(2)); + nBulletinProcessingStrategy.update(new ComponentBulletin(3)); + nBulletinProcessingStrategy.update(new ComponentBulletin(4)); + nBulletinProcessingStrategy.update(new ComponentBulletin(5)); + nBulletinProcessingStrategy.update(new ComponentBulletin(6)); + nBulletinProcessingStrategy.update(new ComponentBulletin(7)); + assertEquals(NodeBulletinProcessingStrategy.MAX_ENTRIES, nBulletinProcessingStrategy.getBulletins().size()); + + } + +}