Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Appengine and rx-Java?

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?

like image 878
Patrick Avatar asked May 16 '14 17:05

Patrick


2 Answers

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.

like image 92
Roman Savin Avatar answered Nov 06 '22 11:11

Roman Savin


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.

like image 41
zsxwing Avatar answered Nov 06 '22 09:11

zsxwing