processor injection

This commit is contained in:
2025-10-23 18:26:32 +02:00
parent 67ced02756
commit 9d582793f4
5 changed files with 66 additions and 71 deletions

View File

@@ -35,7 +35,7 @@
<dependency> <dependency>
<groupId>de.infinimotion</groupId> <groupId>de.infinimotion</groupId>
<artifactId>model-persistence</artifactId> <artifactId>model-persistence</artifactId>
<version>0.0.38</version> <version>0.0.44</version>
</dependency> </dependency>
<!-- Quarkus --> <!-- Quarkus -->

View File

@@ -0,0 +1,28 @@
package de.infinimotion.persistence;
import de.infinimotion.model.persistence.*;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;
import java.io.IOException;
@ApplicationScoped
public class CommandObserver {
@Incoming("command")
@Outgoing("command-replies")
public CommandWrapper process(CommandWrapper request) throws IOException {
System.out.println(request);
try {
return request.process().serialize().copyIds(request);
} catch (Throwable t) {
t.printStackTrace();
CommandException e = new CommandException();
e.setException(t.getMessage());
return e.serialize().copyIds(request);
}
}
}

View File

@@ -1,64 +0,0 @@
package de.infinimotion.persistence;
import de.infinimotion.model.persistence.*;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ApplicationScoped
public class CommandProcessor {
@Inject
EntityManager em;
private Map<String, ThrowingFunction<CommandWrapper, Command, IOException>> processors
= Map.of(CommandListKinosaal.class.getSimpleName(), this::processListKinosaal,
CommandCreateKinosaal.class.getSimpleName(), this::processCreateKinosaal);
@Incoming("command")
@Outgoing("command-replies")
public CommandWrapper process(CommandWrapper request) throws IOException {
try {
ThrowingFunction<CommandWrapper, Command, IOException> processor = processors.get(request.getType());
if (processor == null) {
CommandException e = new CommandException();
e.setException("unknown processor");
return e.serialize().copyIds(request);
}
return processor.apply(request).serialize().copyIds(request);
} catch (Throwable t) {
CommandException e = new CommandException();
e.setException(t.getMessage());
return e.serialize().copyIds(request);
}
}
@Transactional
Command processListKinosaal(CommandWrapper wrapper) throws IOException {
CommandListKinosaal request = CommandListKinosaal.deserialize(wrapper);
List<Kinosaal> results = em.createQuery("SELECT k FROM Kinosaal k", Kinosaal.class).getResultList();
CommandListKinosaalResponse response = new CommandListKinosaalResponse();
response.setList(results);
return response;
}
@Transactional
Command processCreateKinosaal(CommandWrapper wrapper) throws IOException {
CommandCreateKinosaal request = CommandCreateKinosaal.deserialize(wrapper);
Kinosaal hall = new Kinosaal();
hall.setName(request.getName());
em.persist(hall);
CommandCreateKinosaalResponse response = new CommandCreateKinosaalResponse();
response.setHall(hall);
return response;
}
}

View File

@@ -1,6 +0,0 @@
package de.infinimotion.persistence;
@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Exception> {
R apply(T t) throws E;
}

View File

@@ -0,0 +1,37 @@
package de.infinimotion.persistence.processor;
import de.infinimotion.model.persistence.*;
import io.quarkus.arc.Unremovable;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import java.util.List;
@Unremovable
@ApplicationScoped
public class KinosaalProcessor implements CommandListKinosaalProcessor, CommandCreateKinosaalProcessor {
@Inject
EntityManager em;
@Transactional
public Command processCommandListKinosaal(CommandListKinosaal request) {
List<Kinosaal> results = em.createQuery("SELECT k FROM Kinosaal k", Kinosaal.class).getResultList();
CommandListKinosaalResponse response = new CommandListKinosaalResponse();
response.setList(results);
return response;
}
@Transactional
public Command processCommandCreateKinosaal(CommandCreateKinosaal request) {
Kinosaal hall = new Kinosaal();
hall.setName(request.getName());
em.persist(hall);
CommandCreateKinosaalResponse response = new CommandCreateKinosaalResponse();
response.setHall(hall);
return response;
}
}