Skip to content

Commit

Permalink
Simplify quarkus implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasrutishauser committed Nov 22, 2024
1 parent da223a9 commit dbbf4df
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import java.util.List;
import java.util.Optional;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index;

import com.github.jonasrutishauser.transactional.event.api.Configuration;
import com.github.jonasrutishauser.transactional.event.api.MPConfiguration;
import com.github.jonasrutishauser.transactional.event.api.handler.EventHandler;
import com.github.jonasrutishauser.transactional.event.core.concurrent.DefaultEventExecutor;
import com.github.jonasrutishauser.transactional.event.core.defaults.DefaultConcurrencyProvider;
import com.github.jonasrutishauser.transactional.event.core.serialization.JaxbSerialization;
Expand All @@ -36,7 +32,6 @@
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem;
Expand Down Expand Up @@ -81,28 +76,6 @@ void excludeFromBeansXml(Capabilities capabilities, Optional<MetricsCapabilityBu
}
}

@BuildStep
UnremovableBeanBuildItem ensureEventHandlerMethodsAreNotRemoved(ApplicationIndexBuildItem index) {
return new UnremovableBeanBuildItem(
beanInfo -> beanInfo.isClassBean() && hasEventHandlerMethod(beanInfo.getImplClazz(), index.getIndex()));
}

private boolean hasEventHandlerMethod(ClassInfo implClazz, Index index) {
if (implClazz.methods().stream() //
.filter(m -> !m.isBridge()) //
.filter(m -> !m.isSynthetic()) //
.filter(m -> !m.isConstructor()) //
.filter(m -> !m.isStaticInitializer()) //
.anyMatch(m -> m.hasAnnotation(EventHandler.class))) {
return true;
}
if (implClazz.superClassType() == null) {
return false;
}
ClassInfo superClass = index.getClassByName(implClazz.superClassType().name());
return superClass != null && hasEventHandlerMethod(implClazz, index);
}

@BuildStep(onlyIfNot = IsNormal.class)
UnremovableBeanBuildItem ensureDbSchemaIsNotRemoved() {
return UnremovableBeanBuildItem.beanTypes(DbSchema.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
@Typed(Handler.class)
public class TestEventHandler extends AbstractHandler<TestEvent> {

@Inject
private Messages messages;

@Inject
TestEventHandler(Messages messages) {
this.messages = messages;
}

@Override
protected void handle(TestEvent event) {
if (event.getMessage().contains("failure") && messages.addFailure(event.getMessage())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.jonasrutishauser.transactional.event.quarkus.deployment.it;

import com.github.jonasrutishauser.transactional.event.api.handler.EventHandler;

import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

@Dependent
public class TestHandlerMethod {

private final Messages messages;

@Inject
TestHandlerMethod(Messages messages) {
this.messages = messages;
}

@EventHandler
void handle(String event) {
messages.add(event);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collection;

import com.github.jonasrutishauser.transactional.event.api.EventPublisher;
import com.github.jonasrutishauser.transactional.event.api.handler.EventHandler;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.control.ActivateRequestContext;
Expand All @@ -13,11 +12,15 @@
@Dependent
public class TestPublisher {

@Inject
private EventPublisher publisher;
private final EventPublisher publisher;

private final Messages messages;

@Inject
private Messages messages;
TestPublisher(EventPublisher publisher, Messages messages) {
this.publisher = publisher;
this.messages = messages;
}

@Transactional
@ActivateRequestContext
Expand All @@ -35,9 +38,4 @@ public Collection<String> getMessages() {
return messages.get();
}

@EventHandler
void handle(String event) {
messages.add(event);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class TransactionalEventIT {
static final QuarkusUnitTest config = new QuarkusUnitTest() //
.setFlatClassPath(true) // needed for invoker
.withApplicationRoot(archive -> archive //
.addClasses(Messages.class, TestEvent.class, TestEventHandler.class, TestPublisher.class) //
.addClasses(Messages.class, TestEvent.class, TestEventHandler.class, TestPublisher.class,
TestHandlerMethod.class) //
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") //
).overrideRuntimeConfigKey("quarkus.transactional.event.initial-dispatch-interval", "1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class QuarkusEventExecutor implements EventExecutor {
private final ExecutorService executor;
private final ScheduledExecutorService scheduler;

QuarkusEventExecutor() {
this(null, null);
}

@Inject
QuarkusEventExecutor(@Events ExecutorService executor, ScheduledExecutorService scheduler) {
this.executor = executor;
Expand Down

0 comments on commit dbbf4df

Please sign in to comment.