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; ...@@ -17,7 +17,7 @@ package de.codecentric.boot.admin.journal.store;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.LinkedList;
import java.util.List; import java.util.List;
import de.codecentric.boot.admin.event.ClientApplicationEvent; import de.codecentric.boot.admin.event.ClientApplicationEvent;
...@@ -28,20 +28,28 @@ import de.codecentric.boot.admin.event.ClientApplicationEvent; ...@@ -28,20 +28,28 @@ import de.codecentric.boot.admin.event.ClientApplicationEvent;
* @author Johannes Stelzer * @author Johannes Stelzer
*/ */
public class SimpleJournaledEventStore implements JournaledEventStore { public class SimpleJournaledEventStore implements JournaledEventStore {
private final List<ClientApplicationEvent> store = new LinkedList<ClientApplicationEvent>();
private final List<ClientApplicationEvent> store = Collections private int capacity = 1_000;
.synchronizedList(new ArrayList<ClientApplicationEvent>(1000));
@Override @Override
public Collection<ClientApplicationEvent> findAll() { public Collection<ClientApplicationEvent> findAll() {
ArrayList<ClientApplicationEvent> list = new ArrayList<>(store); synchronized (this.store) {
Collections.reverse(list); return new ArrayList<>(store);
return list; }
} }
@Override @Override
public void store(ClientApplicationEvent event) { 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; ...@@ -18,10 +18,8 @@ package de.codecentric.boot.admin.journal.store;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
...@@ -33,22 +31,39 @@ import de.codecentric.boot.admin.model.Application; ...@@ -33,22 +31,39 @@ import de.codecentric.boot.admin.model.Application;
public class SimpleJournaledEventStoreTest { public class SimpleJournaledEventStoreTest {
private SimpleJournaledEventStore store = new SimpleJournaledEventStore();
@Test @Test
public void test_store() { public void test_store() {
SimpleJournaledEventStore store = new SimpleJournaledEventStore();
Application application = Application.create("foo").withId("bar").build(); Application application = Application.create("foo").withId("bar").build();
List<ClientApplicationEvent> events = Arrays.asList(new ClientApplicationRegisteredEvent( List<ClientApplicationEvent> events = Arrays.asList(
application), new ClientApplicationDeregisteredEvent(application)); new ClientApplicationRegisteredEvent(application),
new ClientApplicationDeregisteredEvent(application));
for (ClientApplicationEvent event : events) { for (ClientApplicationEvent event : events) {
store.store(event); store.store(event);
} }
// Items are stored in reverse order assertThat(store.findAll(), is(
List<ClientApplicationEvent> reversed = new ArrayList<>(events); (Collection<ClientApplicationEvent>) Arrays.asList(events.get(1), events.get(0))));
Collections.reverse(reversed); }
@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