sizovs / PipelinR

PipelinR is a lightweight command processing pipeline ❍ ⇢ ❍ ⇢ ❍ for your Java awesome app.
https://github.com/sizovs/PipelinR
MIT License
420 stars 59 forks source link

Get notification handler name inside Notification.Middleware #18

Closed arkadiuszSzast closed 3 years ago

arkadiuszSzast commented 3 years ago

Hi,

I'm trying to get Notification.Handler class name inside Notification.Middleware but I cannot find any way to achieve that. I need it inside my 'LoggableMiddleware' and 'TraceableMiddleware'. Just for logging that name and adding it as a span tag. Is there any option to do it?

data class Ping(val from: String): Notification {

    @Component
    class PongHandler :Notification.Handler<Ping> {
        override fun handle(notification: Ping) {
            TODO("Not yet implemented")
        }
    }

    @Component
    class Pong2Handler :Notification.Handler<Ping> {
        override fun handle(notification: Ping) {
            TODO("Not yet implemented")
        }
    }
}

And from middleware I want to log something like "Handling Ping notification in PongHandler "

sizovs commented 3 years ago

Hi Arkadiusz @arkadiuszSzast

There is no way to achieve this with Middlewares because Middlewares don't know which Handlers will be handling the Notification. This is because a Notification Handler is just another Middleware that runs after all custom Middlewares (and all Middlewares are independent of each other).

If you want to know which Handlers have actually handled your Notification, you can achieve this by creating a base Handler class that performs logging, and then derive your concrete handlers from it.

Does that work for you?