Skip to content

Commit 966d2ea

Browse files
tomekl007pedja4
authored andcommitted
Bael 850 (eugenp#1732)
* BAEL-786 Updated custom test reports * BAEL-850 ConcurentskipLIst * BAEL-850 formatting * BAEL-856 code for long adder and accumulator (eugenp#1723) * BAEL-856 code for long adder and accumulator * BAEL-856 rearange packages * BAEL-856 Formatting * BAEL-742 Create README for Stripe API article (eugenp#1731) * Add files via upload * Update pom.xml * Update RunGuice.java * Update Communication.java * Update CommunicationMode.java * Update DefaultCommunicator.java * Update EmailCommunicationMode.java * Update IMCommunicationMode.java * Update SMSCommunicationMode.java * Update MessageLogger.java * Update MessageSentLoggable.java * Update AOPModule.java * Update BasicModule.java * Update CommunicationModel.java * Update Communicator.java * Update BasicModule.java * Update RunGuice.java * Update MessageLogger.java * Update Communicator.java * Update pom.xml * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * Update pom.xml * Update pom.xml * Update pom.xml * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom * BAEL-9: Updated README.md * BAEL-157: README.md updated * Changed project name * Update RunGuice.java Removed references to message logging and output * Update Communication.java Removed message logging-related code * BAEL-566: Updated README.md * New project name * BAEL-393: removing guice-intro directory * BAEL-393: renamed module guice-intro to guice in root pom.xml * BAEL-393 and BAEL-541 README.md files * BAEL-731: Updated README.md * BAEL-680: renamed test methods * BAEL-714: Updated README.md * BAEL-737: Updated README.md * BAEL-680 and BAEL-756 README.md updates * BAEL-666: Updated README * BAEL-415: Custom Scope * BAEL-415: Custom Scope - renamed classes to reflect TenantScope * README file updates for BAEL-723, BAEL-763, and BAEL-415 * BAEL-735: README * BAEL-567: README * BAEL-736: README * BAEL-766: Update README * BAEL-555: README update * BAEL-761: README update * BAEL-742: Stripe API for Java README file * Merge aspectj -> spring-aop (eugenp#1737) * BAEL-88 Integration tests fixed. (eugenp#1739) * BAEL-88 Testing in Spring Boot * BAEL-88 Integration tests fixed. * Merge assertj -> testing (eugenp#1738) * Guide to JDO (eugenp#1736) * Guide to JDO * Guide to JDO * Guide to JDO * Guide To JDO * BAEL-86: Corrected URL in README.md (eugenp#1740) * Add files via upload * Update pom.xml * Update RunGuice.java * Update Communication.java * Update CommunicationMode.java * Update DefaultCommunicator.java * Update EmailCommunicationMode.java * Update IMCommunicationMode.java * Update SMSCommunicationMode.java * Update MessageLogger.java * Update MessageSentLoggable.java * Update AOPModule.java * Update BasicModule.java * Update CommunicationModel.java * Update Communicator.java * Update BasicModule.java * Update RunGuice.java * Update MessageLogger.java * Update Communicator.java * Update pom.xml * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * Update pom.xml * Update pom.xml * Update pom.xml * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom * BAEL-9: Updated README.md * BAEL-157: README.md updated * Changed project name * Update RunGuice.java Removed references to message logging and output * Update Communication.java Removed message logging-related code * BAEL-566: Updated README.md * New project name * BAEL-393: removing guice-intro directory * BAEL-393: renamed module guice-intro to guice in root pom.xml * BAEL-393 and BAEL-541 README.md files * BAEL-731: Updated README.md * BAEL-680: renamed test methods * BAEL-714: Updated README.md * BAEL-737: Updated README.md * BAEL-680 and BAEL-756 README.md updates * BAEL-666: Updated README * BAEL-415: Custom Scope * BAEL-415: Custom Scope - renamed classes to reflect TenantScope * README file updates for BAEL-723, BAEL-763, and BAEL-415 * BAEL-735: README * BAEL-567: README * BAEL-736: README * BAEL-766: Update README * BAEL-555: README update * BAEL-761: README update * BAEL-742: Stripe API for Java README file * BAEL-86: Correction to README file * BAEL-850 Formatting
1 parent 9e2577b commit 966d2ea

3 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.concurrent.skiplist;
2+
3+
import java.time.ZonedDateTime;
4+
5+
public class Event {
6+
private final ZonedDateTime eventTime;
7+
private final String content;
8+
9+
public Event(ZonedDateTime eventTime, String content) {
10+
this.eventTime = eventTime;
11+
this.content = content;
12+
}
13+
14+
public ZonedDateTime getEventTime() {
15+
return eventTime;
16+
}
17+
18+
public String getContent() {
19+
return content;
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.concurrent.skiplist;
2+
3+
import java.time.ZonedDateTime;
4+
import java.util.Comparator;
5+
import java.util.concurrent.ConcurrentNavigableMap;
6+
import java.util.concurrent.ConcurrentSkipListMap;
7+
import java.util.function.ToLongFunction;
8+
9+
public class EventWindowSort {
10+
private final ConcurrentSkipListMap<ZonedDateTime, String> events = new ConcurrentSkipListMap<>(Comparator.comparingLong(new ToLongFunction<ZonedDateTime>() {
11+
@Override
12+
public long applyAsLong(ZonedDateTime value) {
13+
return value
14+
.toInstant()
15+
.toEpochMilli();
16+
}
17+
}));
18+
19+
public void acceptEvent(Event event) {
20+
events.put(event.getEventTime(), event.getContent());
21+
}
22+
23+
public ConcurrentNavigableMap<ZonedDateTime, String> getEventsFromLastMinute() {
24+
return events.tailMap(ZonedDateTime
25+
.now()
26+
.minusMinutes(1));
27+
}
28+
29+
public ConcurrentNavigableMap<ZonedDateTime, String> getEventsOlderThatOneMinute() {
30+
return events.headMap(ZonedDateTime
31+
.now()
32+
.minusMinutes(1));
33+
}
34+
35+
}
36+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.baeldung.concurrent.skiplist;
2+
3+
import org.junit.Test;
4+
5+
import java.time.ZonedDateTime;
6+
import java.util.UUID;
7+
import java.util.concurrent.ConcurrentNavigableMap;
8+
import java.util.concurrent.ExecutorService;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.TimeUnit;
11+
import java.util.stream.IntStream;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertTrue;
15+
16+
public class ConcurrentSkipListSetTest {
17+
18+
@Test
19+
public void givenThreadsProducingEvents_whenGetForEventsFromLastMinute_thenReturnThoseEventsInTheLockFreeWay() throws InterruptedException {
20+
//given
21+
ExecutorService executorService = Executors.newFixedThreadPool(3);
22+
EventWindowSort eventWindowSort = new EventWindowSort();
23+
int numberOfThreads = 2;
24+
//when
25+
Runnable producer = () -> IntStream
26+
.rangeClosed(0, 100)
27+
.forEach(index -> eventWindowSort.acceptEvent(new Event(ZonedDateTime
28+
.now()
29+
.minusSeconds(index), UUID
30+
.randomUUID()
31+
.toString())));
32+
33+
for (int i = 0; i < numberOfThreads; i++) {
34+
executorService.execute(producer);
35+
}
36+
37+
Thread.sleep(500);
38+
39+
ConcurrentNavigableMap<ZonedDateTime, String> eventsFromLastMinute = eventWindowSort.getEventsFromLastMinute();
40+
41+
long eventsOlderThanOneMinute = eventsFromLastMinute
42+
.entrySet()
43+
.stream()
44+
.filter(e -> e
45+
.getKey()
46+
.isBefore(ZonedDateTime
47+
.now()
48+
.minusMinutes(1)))
49+
.count();
50+
assertEquals(eventsOlderThanOneMinute, 0);
51+
52+
long eventYoungerThanOneMinute = eventsFromLastMinute
53+
.entrySet()
54+
.stream()
55+
.filter(e -> e
56+
.getKey()
57+
.isAfter(ZonedDateTime
58+
.now()
59+
.minusMinutes(1)))
60+
.count();
61+
62+
//then
63+
assertTrue(eventYoungerThanOneMinute > 0);
64+
65+
executorService.awaitTermination(1, TimeUnit.SECONDS);
66+
executorService.shutdown();
67+
}
68+
69+
@Test
70+
public void givenThreadsProducingEvents_whenGetForEventsOlderThanOneMinute_thenReturnThoseEventsInTheLockFreeWay() throws InterruptedException {
71+
//given
72+
ExecutorService executorService = Executors.newFixedThreadPool(3);
73+
EventWindowSort eventWindowSort = new EventWindowSort();
74+
int numberOfThreads = 2;
75+
//when
76+
Runnable producer = () -> IntStream
77+
.rangeClosed(0, 100)
78+
.forEach(index -> eventWindowSort.acceptEvent(new Event(ZonedDateTime
79+
.now()
80+
.minusSeconds(index), UUID
81+
.randomUUID()
82+
.toString())));
83+
84+
for (int i = 0; i < numberOfThreads; i++) {
85+
executorService.execute(producer);
86+
}
87+
88+
Thread.sleep(500);
89+
90+
ConcurrentNavigableMap<ZonedDateTime, String> eventsFromLastMinute = eventWindowSort.getEventsOlderThatOneMinute();
91+
92+
long eventsOlderThanOneMinute = eventsFromLastMinute
93+
.entrySet()
94+
.stream()
95+
.filter(e -> e
96+
.getKey()
97+
.isBefore(ZonedDateTime
98+
.now()
99+
.minusMinutes(1)))
100+
.count();
101+
assertTrue(eventsOlderThanOneMinute > 0);
102+
103+
long eventYoungerThanOneMinute = eventsFromLastMinute
104+
.entrySet()
105+
.stream()
106+
.filter(e -> e
107+
.getKey()
108+
.isAfter(ZonedDateTime
109+
.now()
110+
.minusMinutes(1)))
111+
.count();
112+
113+
//then
114+
assertEquals(eventYoungerThanOneMinute, 0);
115+
116+
executorService.awaitTermination(1, TimeUnit.SECONDS);
117+
executorService.shutdown();
118+
}
119+
120+
}

0 commit comments

Comments
 (0)