r/microservices • • 3d ago

Tool/Product Event-driven (choreography) java framework for monolith/microservices

I am working on the framework that helps building event-driven java application. I am still working on edge cases. Here is the gist: https://gist.github.com/mananvpanchal/25aeb37b04da7399adbcde2b251e0bc5

I will appreciate if you spare some time to read it. Please, let me know if I am on right path or wrong.

Thanks

3 Upvotes

6 comments sorted by

1

u/bzBetty 3d ago

the waiting on multiple events is rather interesting, although I can't understand how you wouldn't need an eventual timeout mechanism on that.

1

u/mananvpanchal 3d ago edited 3d ago

Thanks for reading!

Yes, we need timeout that is for sure. As, nothing is actually waiting for anything, I think we have two ways to do it.

  1. We just have "PENDING" state in a table row suggests at least one event is not arrived yet. A time out scheduler simply update the status to "COMPLETED".
  2. If we really need to execute some logic or need to emit next event based on timeout occurrence. We can do something like this

Suppose, we are waiting for three events

public void on(OrderPlaced order, StockReserved stock, PaymentAuthorized payment, InProps inProps) {
  if(payemnt.timedOut()) {
  // logic on payment timeout
  // new event may be emitted here
  }
  if(stock.timedOut()) {
  // logic on stock timeout
  // new event may be emitted here
  }
}

If payment service is stuck or dead for some reason, we will not receive PaymentAuthorized event for quite sometime in future. In that case, a time out scheduler can emit the same event with timedOut = true flag. The status become COMPLETED. Our event processor should be implemented to handle the timeout scenario.

Now, in both situation, the processor state is COMPLETED. If that stuck event is arrived now, but it will not be considered and will not put any processor into waiting state.

The docs is bit poor here but I will update it: https://gist.github.com/mananvpanchal/25aeb37b04da7399adbcde2b251e0bc5#65-an-event-that-never-arrives

1

u/mananvpanchal 3d ago

Do you prefer something like this?

public void on(OrderPlaced order, 
              @AnyOf("stock_events") StockReserved stock, 
              @AnyOf("stock_events") @TimeOut("5m") StockTimedOut timedOut, 
              PaymentAuthorized payment, InProps inProps) {

That way, any event from the "stock_events" group arrives first I will execute "on" method. A timeout scheduler run behind the scene and look for timeout.

Up until now, I am just handling "AND" case where I act when all events are arrived, but with the above feature I can handle "OR" case too.

The above setup looks like (order && (stock || timedOut) && payment)

2

u/bzBetty 2d ago

Think I preferred the original. This way I think it's ambiguous around whether I require a timeout or it cancels things.

I've also always liked the idea of sagas, where maybe the error timeout method is a separate method on same class somehow

2

u/bzBetty 2d ago

Other thoughts is you could call the method when any of the events arrive and pass null as the ones that haven't yet

1

u/mananvpanchal 2d ago

Sure, I will do that way