Google Appengine and rx-Java?

前端 未结 2 1553
臣服心动
臣服心动 2021-01-19 03:59

Is rxJava library compatible with Google Appengine? If so are there any limitations? The only info I have found is mention of \'partial support\' on grepcode. http://grep

相关标签:
2条回答
  • 2021-01-19 04:31

    You should create a child of RxJavaSchedulersHook and override its methods using your scheduler which use com.google.appengine.api.ThreadManager: I've done it like this :

    public class MyThreadSchedulersHook extends RxJavaSchedulersHook {
    
    private final Executor executor = new ScheduledThreadPoolExecutor(10, ThreadManager.backgroundThreadFactory());
    
    @Override
    public Scheduler getComputationScheduler() {
        return Schedulers.from(executor);
    }
    
    @Override
    public Scheduler getIOScheduler() {
        return Schedulers.from(executor);
    }
    
    @Override
    public Scheduler getNewThreadScheduler() {
        return Schedulers.from(executor);
    }
    }
    

    Then you should register this hook. Better to do this in an ServletContextListener implementation:

    public class ContextListener implements ServletContextListener {
    
    @Override
    public void contextInitialized(final ServletContextEvent servletContextEvent) {
        RxJavaPlugins.getInstance().registerSchedulersHook(new RxMainThreadSchedulersHook());
    }
    
    @Override
    public void contextDestroyed(final ServletContextEvent servletContextEvent) {
        // App Engine does not currently invoke this method.
    }
    }
    

    It works for me.

    0 讨论(0)
  • 2021-01-19 04:36

    The problem is the limitation of Java Threads in Google Appengine: https://developers.google.com/appengine/docs/java/#Java_The_sandbox

    RxJava uses Java Thread and Executor in the Scheduler implementations. So the codes which get involved some concurrent Schedulers can not run in Google Java Sandbox.

    If you want to use Scheduler in Google Appengine, you need to implement a special Scheduler by yourself. Besides, some operators may use Scheduler by default, such as delay use Schedulers.computation() by default. Remember to use their Scheduler overload methods.

    0 讨论(0)
提交回复
热议问题