Skip to content

Commit

Permalink
Using builders. Added support for annotation to mark events.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Jun 13, 2024
1 parent 6e9d693 commit 5377917
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 51 deletions.
9 changes: 9 additions & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
config.stopBubbling = true
# This will instruct jacoco not to calculate coverage for lombok generated code
lombok.addLombokGeneratedAnnotation = true
lombok.builder.className = Builder
#lombok.accessors.chain=true

lombok.addNullAnnotations = checkerframework
lombok.addSuppressWarnings = false

8 changes: 8 additions & 0 deletions src/main/java/nl/vpro/esper/EsperEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package nl.vpro.esper;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface EsperEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.time.Duration;
import java.time.Instant;
import java.util.Set;
import java.util.concurrent.*;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

import com.espertech.esper.compiler.client.EPCompileException;
import com.espertech.esper.runtime.client.EPDeployException;

@Slf4j
public class AsyncEventServiceProviderImpl extends EventServiceProviderImpl implements AsyncEventServiceProvider {

Expand All @@ -30,30 +29,13 @@ public class AsyncEventServiceProviderImpl extends EventServiceProviderImpl impl
private Duration defaultTimeout = Duration.ofSeconds(10);
private boolean running = true;


public AsyncEventServiceProviderImpl(int queueCapacity) throws EPDeployException, EPCompileException {
super();
queue = new ArrayBlockingQueue<>(queueCapacity);
}

public AsyncEventServiceProviderImpl(String name) {
this(name, 200);
}

public AsyncEventServiceProviderImpl(String name, int queueCapacity) {
this(name, new String[] {}, queueCapacity);
}

public AsyncEventServiceProviderImpl(String name, String... eventPackage) {
this(name, eventPackage, 200);
}

public AsyncEventServiceProviderImpl(String name, String eventPackage, int queueCapacity) {
this(name, new String[] {eventPackage}, queueCapacity);
}

public AsyncEventServiceProviderImpl(String name, String[] eventPackage, int queueCapacity) {
super(name, eventPackage);
@lombok.Builder(builderMethodName = "asyncBuilder")
private AsyncEventServiceProviderImpl(
String name,
Set<String> eventPackages,
Set<Class<? extends Annotation>> eventAnnotations,
int queueCapacity) {
super(name, eventPackages, eventAnnotations);
queue = new ArrayBlockingQueue<>(queueCapacity);
}

Expand Down
48 changes: 32 additions & 16 deletions src/main/java/nl/vpro/esper/service/EventServiceProviderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.util.*;

import jakarta.annotation.PostConstruct;
Expand All @@ -30,22 +31,41 @@ public class EventServiceProviderImpl implements EventServiceProvider {
Configuration config = new Configuration();


public EventServiceProviderImpl() {
this(null);
}

public EventServiceProviderImpl(String name) {
this(name, "nl.vpro.esper.event");
}

@SneakyThrows
public EventServiceProviderImpl(String name, String... eventPackages) {
Set<String> eventPackagesSet = Set.of(eventPackages);
@lombok.Builder
public EventServiceProviderImpl(
String name,
Set<String> eventPackages,
Set<Class<? extends Annotation>> eventAnnotations) {
final Set<String> finalEventPackages;
if (eventPackages == null) {
finalEventPackages = Set.of("nl");
} else {
finalEventPackages = eventPackages;
}
ClassPath.from(getClass().getClassLoader())
.getAllClasses()
.stream()
.filter(c -> eventPackagesSet.contains(c.getPackageName()))
.map(ClassPath.ClassInfo::load)
.filter(c -> ! c.getResourceName().equals("module-info.class"))
.filter(c ->
finalEventPackages.stream()
.anyMatch(p ->
c.getPackageName().equals(p) || c.getPackageName().startsWith(p + ".")
)
)
.map(ci -> {
try {
return ci.load();
} catch (Exception e) {
return null;
}
})
.filter(Objects::nonNull)
.filter(c ->
eventAnnotations == null ||
Arrays.stream(c.getAnnotations())
.anyMatch(a -> eventAnnotations.stream().anyMatch(ac -> ac.isInstance(a)))
)
.forEach((found) -> {
log.info("Found event type {}", found);
config.getCommon().addEventType(found);
Expand All @@ -54,10 +74,6 @@ public EventServiceProviderImpl(String name, String... eventPackages) {
init();
}

public EventServiceProviderImpl(String name, Package... eventPackages) {
this(name, Arrays.stream(eventPackages).map(Package::getName).toArray(String[]::new));
}

@SneakyThrows
@PostConstruct
private void init() {
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/nl/vpro/esper/event/TestEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import lombok.Getter;
import lombok.Setter;

import nl.vpro.esper.EsperEvent;

@Setter
@Getter
@EsperEvent
public class TestEvent {
private String name;

Expand Down
7 changes: 6 additions & 1 deletion src/test/java/nl/vpro/esper/listener/TopListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package nl.vpro.esper.listener;


import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -26,7 +28,10 @@ public class TopListTest {
EventServiceProvider provider;
@BeforeEach
public void setup() {
provider = new EventServiceProviderImpl("test", TestEvent.class.getPackage());
provider = EventServiceProviderImpl.builder()
.name("test")
.eventPackages(Set.of(TestEvent.class.getPackageName()))
.build();
Statement statement = new Statement("select name, count(*) from TestEvent.win:time(1 min) group by name");
listener = new TopList("name", 5, true);
statement.addListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*/
package nl.vpro.esper.service;

import nl.vpro.esper.event.TestEvent;
import nl.vpro.esper.listener.Counter;
import java.time.Duration;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import nl.vpro.esper.event.TestEvent;
import nl.vpro.esper.listener.Counter;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -21,7 +23,9 @@ public class AsyncEventServiceProviderTest {
@BeforeEach
public void setup() {
Statement testStatement = new Statement("select count(*) from TestEvent where name like '%6'");
provider = new AsyncEventServiceProviderImpl("200", "nl.vpro.esper.event");
provider = AsyncEventServiceProviderImpl.asyncBuilder().name("200")
.eventPackages(Set.of("nl.vpro.esper.event"))
.build();

Check failure on line 28 in src/test/java/nl/vpro/esper/service/AsyncEventServiceProviderTest.java

View workflow job for this annotation

GitHub Actions / Test Report

AsyncEventServiceProviderTest.testService

java.lang.IllegalArgumentException at java.base/java.util.concurrent.ArrayBlockingQueue.<init>(ArrayBlockingQueue.java:272)
Raw output
java.lang.IllegalArgumentException
	at java.base/java.util.concurrent.ArrayBlockingQueue.<init>(ArrayBlockingQueue.java:272)
	at java.base/java.util.concurrent.ArrayBlockingQueue.<init>(ArrayBlockingQueue.java:257)
	at nl.vpro.esper.service.AsyncEventServiceProviderImpl.<init>(AsyncEventServiceProviderImpl.java:39)
	at nl.vpro.esper.service.AsyncEventServiceProviderImpl$Builder.build(AsyncEventServiceProviderImpl.java:32)
	at nl.vpro.esper.service.AsyncEventServiceProviderTest.setup(AsyncEventServiceProviderTest.java:28)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
provider.init();
provider.addStatement(testStatement);
listener = new Counter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
*/
package nl.vpro.esper.service;

import nl.vpro.esper.event.TestEvent;
import nl.vpro.esper.listener.Counter;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import nl.vpro.esper.EsperEvent;
import nl.vpro.esper.event.TestEvent;
import nl.vpro.esper.listener.Counter;

import static org.assertj.core.api.Assertions.assertThat;


Expand All @@ -20,7 +24,9 @@ public class EventServiceProviderTest {
@BeforeEach
public void setup() {
Statement testStatement = new Statement("select count(*) from TestEvent where name like '%6'");
provider = new EventServiceProviderImpl();
provider = EventServiceProviderImpl.builder()
.eventAnnotations(Set.of(EsperEvent.class))
.build();
provider.addStatement(testStatement);
listener = new Counter();
testStatement.addListener(listener);
Expand Down

0 comments on commit 5377917

Please sign in to comment.