Skip to content

Commit

Permalink
refactored eventing
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Nov 13, 2024
1 parent eacc91b commit 493a285
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 150 deletions.
123 changes: 123 additions & 0 deletions events/artemis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.serious</groupId>
<artifactId>service-events</artifactId>
<version>0.0.3-SNAPSHOT</version>
</parent>

<artifactId>service-events-artemis</artifactId>
<name>service-events-artemis</name>
<description>events artemis</description>
<packaging>jar</packaging>

<dependencies>
<!-- kotlin -->

<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json</artifactId>
<version>1.5.1</version>
</dependency>

<!-- common -->

<dependency>
<groupId>com.serious</groupId>
<artifactId>service-events-core</artifactId>
<version>${project.version}</version>
</dependency>

<!-- ARTEMIS -->

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-server</artifactId>
<version>2.28.0</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-server</artifactId>
<version>2.28.0</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.29.0</version>
</dependency>

<!-- jackson -->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>

<!-- logging -->

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<!-- spring -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>

<!-- test dependencies -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!-- build -->

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package org.sirius.events.artemis
* All rights reserved
*/

import org.sirius.events.EventManager
import org.sirius.events.Eventing

abstract class ArtemisEventing(eventManager: EventManager) : Eventing(eventManager) {
abstract class ArtemisEventing() : Eventing {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import org.sirius.common.tracer.Tracer
import org.sirius.events.EventDescriptor
import org.sirius.events.EventListenerDescriptor
import org.sirius.events.EventManager
import org.springframework.stereotype.Component
import java.util.concurrent.ConcurrentHashMap

class EmbeddedArtemisEventing(eventManager: EventManager) : ArtemisEventing(eventManager) {
@Component
class EmbeddedArtemisEventing : ArtemisEventing() {
// instance data

private val embedded = EmbeddedActiveMQ()
Expand All @@ -34,7 +35,7 @@ class EmbeddedArtemisEventing(eventManager: EventManager) : ArtemisEventing(even

// private

protected fun createMessage(event: Any) : ClientMessage {
protected fun createMessage(eventManager: EventManager, event: Any) : ClientMessage {
val json = eventManager.objectMapper.writeValueAsString(event)

val message = session.createMessage(true)
Expand All @@ -44,7 +45,7 @@ class EmbeddedArtemisEventing(eventManager: EventManager) : ArtemisEventing(even
return message
}

private fun createEvent(message : ClientMessage, eventClass: Class<*>) : Any {
private fun createEvent(eventManager: EventManager, message : ClientMessage, eventClass: Class<*>) : Any {
val body = message.bodyBuffer.readString()

return eventManager.objectMapper.readValue(body, eventClass)
Expand All @@ -57,11 +58,11 @@ class EmbeddedArtemisEventing(eventManager: EventManager) : ArtemisEventing(even

// implement

override fun registerEvent(eventDescriptor: EventDescriptor) {
override fun registerEvent(eventManager: EventManager, eventDescriptor: EventDescriptor) {
producer[eventDescriptor.clazz] = session.createProducer(eventDescriptor.name)
}

override fun registerEventListener(eventListenerDescriptor: EventListenerDescriptor) {
override fun registerEventListener(eventManager: EventManager, eventListenerDescriptor: EventListenerDescriptor) {
// consumer

val eventName = eventListenerDescriptor.event.name
Expand Down Expand Up @@ -89,15 +90,15 @@ class EmbeddedArtemisEventing(eventManager: EventManager) : ArtemisEventing(even
if ( Tracer.ENABLED)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "handle event %s from address %s", eventClass.name, message.address)

eventManager.dispatch(createEvent(message, eventClass), eventClass)
eventManager.dispatch(createEvent(eventManager, message, eventClass), eventClass)
}
}

override fun send(event: Any) {
override fun send(eventManager: EventManager, event: Any) {
if ( Tracer.ENABLED)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "send event %s", event.javaClass.name)

producer4(event.javaClass).send(createMessage(event))
producer4(event.javaClass).send(createMessage(eventManager, event))
}

// lifecycle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sirius.events
package org.sirius.events.artemis
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
Expand All @@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
import org.sirius.common.tracer.TraceLevel
import org.sirius.common.tracer.Tracer
import org.sirius.common.tracer.trace.ConsoleTrace
import org.sirius.events.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Bean
Expand Down
File renamed without changes.
102 changes: 102 additions & 0 deletions events/core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.serious</groupId>
<artifactId>service-events</artifactId>
<version>0.0.3-SNAPSHOT</version>
</parent>

<artifactId>service-events-core</artifactId>
<name>service-event-core</name>
<description>events core</description>
<packaging>jar</packaging>

<dependencies>
<!-- kotlin -->

<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-json</artifactId>
<version>1.5.1</version>
</dependency>

<!-- common -->

<dependency>
<groupId>com.serious</groupId>
<artifactId>service-common</artifactId>
<version>${project.version}</version>
</dependency>

<!-- jackson -->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>

<!-- logging -->

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<!-- spring -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>

<!-- test dependencies -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!-- build -->

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ package org.sirius.events
import com.fasterxml.jackson.databind.ObjectMapper
import jakarta.annotation.PostConstruct
import jakarta.annotation.PreDestroy
import org.apache.activemq.artemis.api.core.client.*
import org.sirius.common.tracer.TraceLevel
import org.sirius.common.tracer.Tracer
import org.sirius.events.artemis.EmbeddedArtemisEventing
import org.springframework.beans.BeansException
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -66,15 +64,16 @@ class EventManager() : ApplicationContextAware {
val events : MutableMap<Class<*>, EventDescriptor> = ConcurrentHashMap()
val eventListener : MutableMap<Class<out Any>, EventListenerDescriptor> = ConcurrentHashMap()

val eventing = EmbeddedArtemisEventing(this) // TODO
@Autowired
lateinit var eventing : Eventing

// public

fun send(event: Any) {
if ( Tracer.ENABLED)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "send event %s", event.javaClass.name)

eventing.send(event)
eventing.send(this, event)
}

// protected
Expand All @@ -101,7 +100,7 @@ class EventManager() : ApplicationContextAware {
val descriptor = EventDescriptor(name, clazz, broadcast)
events[clazz] = descriptor

eventing.registerEvent(descriptor)
eventing.registerEvent(this, descriptor)
}
}
}
Expand All @@ -125,7 +124,7 @@ class EventManager() : ApplicationContextAware {

eventListener[eventClass] = descriptor

eventing.registerEventListener(descriptor)
eventing.registerEventListener(this, descriptor)
}
}

Expand Down
22 changes: 22 additions & 0 deletions events/core/src/main/kotlin/org/sirius/events/Eventing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sirius.events
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

interface Eventing {
// public

fun registerEvent(eventManager: EventManager, eventDescriptor: EventDescriptor)

fun registerEventListener(eventManager: EventManager, eventListenerDescriptor: EventListenerDescriptor)

fun send(eventManager: EventManager, event: Any)

// lifecycle

fun startup()

fun shutdown()
}
Loading

0 comments on commit 493a285

Please sign in to comment.