Search code examples
eventsgrailsrx-java2

Configure the Grails event bus scheduler for RxJava 2.x


We are using Grails 4.x with RxJava 2.x plugin (grails-event-rxjava2:4.0.0). We are trying to change the default Io scheduler to a different one. Documentation shows how to this for the default event bus implementation, but we cannot change it for RxJava 2

grails-app/conf/spring/resources.groovy

import org.grails.events.bus.*
import java.util.concurrent.*

beans = {
    eventBus(ExecutorEventBus, Executors.newFixedThreadPool(5))
}

For RxJava2 we are supposed to use RxJavaPlugins class, but we don't know how to configure the resources.groovy.

Anyone can help? Thanks in advance


Solution

  • RxEventBus is configured with Java's ServiceLoader via a src/main/resources/META-INF/services/grails.events.bus.EventBus file, using its default constructor with a Schedulers.io() scheduler. Since the scheduler attribute is final, the only way I imagine you can achieve what you want is to provide your own implementation:

    1. Make grails-events-rxjava2 a compileOnly dependency, otherwise both RxEventBus and YourEventBus will be found, resulting in an error.

    2. Subclass org.grails.events.rxjava2.RxEventBus:

      class YourEventBus extends RxEventBus {
          YourEventBus() {
              super(Schedulers.computation()) //or whatever
          }
      }
      
    3. Register your custom implementation in a src/main/resources/META-INF/services/grails.events.bus.EventBus file.