Google Appengine and rx-Java?

2019-02-23 01:16发布

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://grepcode.com/snapshot/repo1.maven.org/maven2/com.netflix.rxjava/rxjava-core/0.9.0

What is not supported?

2条回答
闹够了就滚
2楼-- · 2019-02-23 01:46

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.

查看更多
地球回转人心会变
3楼-- · 2019-02-23 01:50

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.

查看更多
登录 后发表回答