Skip to content

Commit

Permalink
integrated eventing
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Nov 13, 2024
1 parent 9cc1442 commit 096d3db
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 81 deletions.
6 changes: 6 additions & 0 deletions administration/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

<!-- internal -->

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

<dependency>
<groupId>com.serious</groupId>
<artifactId>service-administration-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import com.serious.service.ComponentAdministration
import com.serious.service.ServiceConfiguration
import com.serious.service.ServiceManager
import lombok.extern.slf4j.Slf4j
import org.sirius.common.tracer.TraceLevel
import org.sirius.common.tracer.Tracer
import org.sirius.common.tracer.trace.ConsoleTrace
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
Expand Down Expand Up @@ -250,6 +253,12 @@ class ServiceAdministrationApplication {
fun main(args: Array<String>) {
System.setProperty("java.net.preferIPv4Stack" , "true");

val tracer = Tracer(ConsoleTrace(), "%d{yyyy-MM-dd HH:mm:ss,SSS} %l{-6s} [%p{-10s}] %m")

tracer
.setTraceLevel("org.sirius", TraceLevel.FULL)
.setTraceLevel("com.serious", TraceLevel.FULL)

SpringApplication.run(ServiceAdministrationApplication::class.java, *args)
}
}
Expand Down
2 changes: 2 additions & 0 deletions administration/server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ logging:
file: "[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level [%t] [%logger - %line]: %m%n"
level:
root: INFO
org:
hibernate: INFO
file:
name: "log/hello.log"
server:
Expand Down
11 changes: 11 additions & 0 deletions administration/server/src/main/resources/broker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:activemq" xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<core xmlns="urn:activemq:core">
<persistence-enabled>false</persistence-enabled>

<security-enabled>false</security-enabled>

<acceptors>
<acceptor name="in-vm">vm://0</acceptor>
</acceptors>
</core>
</configuration>
10 changes: 10 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<packaging>jar</packaging>

<dependencies>
<!-- local -->

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

<!-- spring -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ package org.sirius.events.artemis

import org.sirius.events.Eventing

abstract class ArtemisEventing() : Eventing {
abstract class ArtemisEventing() : Eventing() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ
import org.sirius.common.tracer.TraceLevel
import org.sirius.common.tracer.Tracer
import org.sirius.events.EventDescriptor
import org.sirius.events.EventError
import org.sirius.events.EventListenerDescriptor
import org.sirius.events.EventManager
import org.springframework.stereotype.Component
Expand All @@ -29,31 +30,30 @@ class EmbeddedArtemisEventing : ArtemisEventing() {
init {
this.embedded.start()
val serverLocator = ActiveMQClient.createServerLocator("vm://0")
val sessionFactory = serverLocator.createSessionFactory()
session = sessionFactory.createSession()

session = serverLocator.createSessionFactory().createSession()
}

// private

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

val message = session.createMessage(true)

message.writeBodyBufferString(json)
message.writeBodyBufferString(asJSON(event))

return message
}

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

return eventManager.objectMapper.readValue(body, eventClass)
private fun createEvent(message : ClientMessage, eventDescriptor: EventDescriptor) : Any {
return asEvent(message.bodyBuffer.readString(), eventDescriptor)
}


private fun producer4(clazz: Class<*>) : ClientProducer {
return this.producer[clazz]!!
val producer = this.producer[clazz]
if ( producer !== null)
return producer
else
throw EventError("unknown producer for class ${clazz.name}")
}

// implement
Expand All @@ -69,7 +69,7 @@ class EmbeddedArtemisEventing : ArtemisEventing() {
val eventClass = eventListenerDescriptor.event.clazz

val address = eventName
val queueName = eventName
val queueName = eventListenerDescriptor.name

// create a queue per listener

Expand All @@ -90,14 +90,11 @@ class EmbeddedArtemisEventing : ArtemisEventing() {
if ( Tracer.ENABLED)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "handle event %s from address %s", eventClass.name, message.address)

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

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(eventManager, event))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TestConfiguration {
}
}

@Event
@Event(broadcast = true)
data class HelloEvent(
val hello : String = ""
)
Expand All @@ -49,6 +49,13 @@ class HelloEventListener : AbstractEventListener<HelloEvent>() {
EventTests.future.complete(event.hello)
}
}
/*
@EventListener(event = HelloEvent::class)
class OtherHelloEventListener : AbstractEventListener<HelloEvent>() {
override fun on(event: HelloEvent) {
EventTests.future1.complete(event.hello)
}
}*/

@SpringBootTest(classes = [TestConfiguration::class, EventConfiguration::class])
internal class EventTests {
Expand All @@ -61,21 +68,21 @@ internal class EventTests {

@Test
fun test() {
Tracer.trace("org.sirius", TraceLevel.FULL, "test" )

eventManager.send(HelloEvent("world"))

val value = future.get()
//val value1 = future1.get()

assertEquals("world", value)
}

companion object {
val future = CompletableFuture<String>()
val future1 = CompletableFuture<String>()

@JvmStatic
@BeforeAll
fun setupTracer(): Unit {
fun setupTracer() {
val tracer = Tracer(ConsoleTrace(), "%d{yyyy-MM-dd HH:mm:ss,SSS} %l{-6s} [%p{-10s}] %m")

tracer.setTraceLevel("org.sirius", TraceLevel.FULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import kotlin.reflect.KClass
@Target(AnnotationTarget.CLASS)
@Component
annotation class EventListener(
//val name: String = "",
val name: String = "",
val event: KClass<out Any>
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.reflect.KClass

data class EventListenerDescriptor(
val beanDefinition : BeanDefinition,
val name: String,
val event: EventDescriptor,
var instance : AbstractEventListener<Any>? = null
)
14 changes: 10 additions & 4 deletions events/core/src/main/kotlin/org/sirius/events/EventManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class EventManager() : ApplicationContextAware {
lateinit var rootPackage: String

@Autowired
lateinit var objectMapper : ObjectMapper
private lateinit var objectMapper : ObjectMapper

lateinit var listenerFactory : ListenerFactory

Expand All @@ -70,8 +70,10 @@ class EventManager() : ApplicationContextAware {
// public

fun send(event: Any) {
val eventDescriptor = this.findEventDescriptor(event.javaClass)

if ( Tracer.ENABLED)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "send event %s", event.javaClass.name)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "%s event %s", eventDescriptor.name, if (eventDescriptor.broadcast) "broadcast" else "send")

eventing.send(this, event)
}
Expand Down Expand Up @@ -113,14 +115,18 @@ class EventManager() : ApplicationContextAware {
.metadata
.getAnnotationAttributes(EventListener::class.java.getCanonicalName())!!

var name = annotations["name"] as String
if ( name.isBlank())
name = bean.beanClassName!!

val eventClass = annotations["event"] as Class<out Any>

val eventDescriptor = findEventDescriptor(eventClass)

if ( Tracer.ENABLED)
Tracer.trace("org.sirius.events", TraceLevel.HIGH, "register event listener %s for event %s", bean.beanClassName!!, eventDescriptor.name)

val descriptor = EventListenerDescriptor(bean, eventDescriptor)
val descriptor = EventListenerDescriptor(bean, name, eventDescriptor)

eventListener[eventClass] = descriptor

Expand All @@ -136,7 +142,7 @@ class EventManager() : ApplicationContextAware {
throw EventError("no registered event for class ${eventClass.name}")
}

public fun dispatch(event: Any, eventClass: Class<out Any>) {
fun dispatch(event: Any, eventClass: Class<out Any>) {
eventListener(eventClass).on(event)
}

Expand Down
40 changes: 34 additions & 6 deletions events/core/src/main/kotlin/org/sirius/events/Eventing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,46 @@ package org.sirius.events
* All rights reserved
*/

interface Eventing {
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired

abstract class Eventing {
// instance data

@Autowired
private lateinit var objectMapper : ObjectMapper

// protected

protected fun asEvent(json: String, eventDescriptor: EventDescriptor) : Any {
try {
return objectMapper.readValue(json, eventDescriptor.clazz)
}
catch(exception: Throwable) {
throw EventError(exception.message!!)
}
}

protected fun asJSON(event: Any) : String {
try {
return objectMapper.writeValueAsString(event)
}
catch(exception: Throwable) {
throw EventError(exception.message!!)
}
}

// public

fun registerEvent(eventManager: EventManager, eventDescriptor: EventDescriptor)
abstract fun registerEvent(eventManager: EventManager, eventDescriptor: EventDescriptor)

fun registerEventListener(eventManager: EventManager, eventListenerDescriptor: EventListenerDescriptor)
abstract fun registerEventListener(eventManager: EventManager, eventListenerDescriptor: EventListenerDescriptor)

fun send(eventManager: EventManager, event: Any)
abstract fun send(eventManager: EventManager, event: Any)

// lifecycle

fun startup()
open fun startup() {}

fun shutdown()
open fun shutdown() {}
}
6 changes: 6 additions & 0 deletions portal/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<version>${project.version}</version>
</dependency>

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

<!-- dependencies -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ import com.serious.portal.version.Version
import com.serious.portal.version.VersionRange
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import org.sirius.events.AbstractEventListener
import org.sirius.events.Event
import org.sirius.events.EventListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.*
import kotlin.collections.HashMap

@Event(broadcast = true)
data class ClearCacheEvent(val name : String = "") // TODO: wohin


// types

data class FilterContext(
Expand All @@ -30,8 +37,9 @@ typealias ManifestFilter = (context: FilterContext, manifest: Manifest) -> Boole

typealias FeatureFilter = (context: FilterContext, feature: Feature) -> Boolean

@EventListener(event = ClearCacheEvent::class)
@Component
class DeploymentManager() {
class DeploymentManager() : AbstractEventListener<ClearCacheEvent>() {
// instance data

private val manifestFilters = mutableListOf<ManifestFilter>()
Expand Down Expand Up @@ -308,4 +316,10 @@ class DeploymentManager() {

return deployment
}

// implement AbstractEventListener

override fun on(event: ClearCacheEvent) {
// TODO
}
}
Loading

0 comments on commit 096d3db

Please sign in to comment.