What kind of “EventBus” to use in Spring? Built-in, Reactor, Akka?

后端 未结 3 1986
花落未央
花落未央 2021-01-30 01:58

We\'re going to start a new Spring 4 application in a few weeks. And we\'d like to use some event-driven architecture. This year I read here and there about \"Reactor\" and whil

3条回答
  •  孤城傲影
    2021-01-30 02:21

    Lets ignore the Spring's ApplicationEvent as it really is not designed for what your asking (its more about bean lifecycle management).

    What you need to figure out is if you want do it

    1. the object oriented way (ie actors, dynamic consumers, registered on the fly) OR
    2. the service way (static consumers, registered on startup).

    Using your example of X and Y are they:

    1. ephemeral instances (1) or are they
    2. long lived singletons/service objects (2)?

    If you need to register consumers on the fly than Akka is a good choice (I'm not sure about reactor as I have never used it). If you don't want to do your consuming in ephemeral objects than you can use JMS or AMQP.

    You also need to understand that these kind of libraries are trying to solve two problems:

    1. Concurrency (ie doing things in parallel on the same machine)
    2. Distribution (ie doing things in parallel on multiple machines)

    Reactor and Akka are mainly focused on #1. Akka just recently added cluster support and the actor abstraction makes it easier to do #2. Message Queues (JMS, AMQP) are focused on #2.

    For my own work I do the service route and use a heavily modified Guava EventBus and RabbitMQ. I use annotations similar to the Guava Eventbus but also have annotations for the objects sent on the bus however you can just use Guava's EventBus in Async mode as a POC and then make your own like I did.

    You might think that you need to have dynamic consumers (1) but most problems can be solved with a simple pub/sub. Also managing dynamic consumers can be tricky (hence Akka is a good choice because the actor model has all sort of management for this)

提交回复
热议问题