Commit 44c839af by Johannes Edmeier

Limit the number of stored events in journal

parent 82c8d819
......@@ -17,7 +17,7 @@ package de.codecentric.boot.admin.journal.store;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
......@@ -28,20 +28,28 @@ import de.codecentric.boot.admin.event.ClientApplicationEvent;
* @author Johannes Stelzer
*/
public class SimpleJournaledEventStore implements JournaledEventStore {
private final List<ClientApplicationEvent> store = new LinkedList<ClientApplicationEvent>();
private final List<ClientApplicationEvent> store = Collections
.synchronizedList(new ArrayList<ClientApplicationEvent>(1000));
private int capacity = 1_000;
@Override
public Collection<ClientApplicationEvent> findAll() {
ArrayList<ClientApplicationEvent> list = new ArrayList<>(store);
Collections.reverse(list);
return list;
synchronized (this.store) {
return new ArrayList<>(store);
}
}
@Override
public void store(ClientApplicationEvent event) {
store.add(event);
synchronized (this.store) {
while (store.size() >= capacity) {
store.remove(capacity - 1);
}
store.add(0, event);
}
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
......@@ -18,10 +18,8 @@ package de.codecentric.boot.admin.journal.store;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
......@@ -33,22 +31,39 @@ import de.codecentric.boot.admin.model.Application;
public class SimpleJournaledEventStoreTest {
private SimpleJournaledEventStore store = new SimpleJournaledEventStore();
@Test
public void test_store() {
SimpleJournaledEventStore store = new SimpleJournaledEventStore();
Application application = Application.create("foo").withId("bar").build();
List<ClientApplicationEvent> events = Arrays.asList(new ClientApplicationRegisteredEvent(
application), new ClientApplicationDeregisteredEvent(application));
List<ClientApplicationEvent> events = Arrays.asList(
new ClientApplicationRegisteredEvent(application),
new ClientApplicationDeregisteredEvent(application));
for (ClientApplicationEvent event : events) {
store.store(event);
}
// Items are stored in reverse order
List<ClientApplicationEvent> reversed = new ArrayList<>(events);
Collections.reverse(reversed);
assertThat(store.findAll(), is(
(Collection<ClientApplicationEvent>) Arrays.asList(events.get(1), events.get(0))));
}
@Test
public void test_store_capacity() {
SimpleJournaledEventStore store = new SimpleJournaledEventStore();
store.setCapacity(2);
Application application = Application.create("foo").withId("bar").build();
List<ClientApplicationEvent> events = Arrays.asList(
new ClientApplicationRegisteredEvent(application),
new ClientApplicationDeregisteredEvent(application),
new ClientApplicationDeregisteredEvent(application));
assertThat(store.findAll(), is((Collection<ClientApplicationEvent>) reversed));
for (ClientApplicationEvent event : events) {
store.store(event);
}
assertThat(store.findAll(), is(
(Collection<ClientApplicationEvent>) Arrays.asList(events.get(2), events.get(1))));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment