0

I've got an app written in JavaScript that makes use of the rxjs library to handle asynchronous data. I've wanted to plugin a java back end and I found J2V8 which lets me fire up Node and talk to it from Java.

For synchronous tasks everything is fine, but when I'm mapping an rxjs Observable to an RxJava Observable I get (silently swallowed) "Invalid V8 thread access" exceptions.

I can see why this might be the case. Node is single threaded and probably busy doing it's own thing when I try and push in my asynchronous response.

The wider view of what I'm wanting to achieve is something like :

Subscription event arrives in Java => Subscription passed to NodeJS for processing => NodeJS calls back out to Java for the actual data source => Java pushes data back into NodeJS for processing => NodeJS passes on the results to the original Java subscriber.

Specifically what I'd like is some advice on how I might push asynchronous events from Java into Node.

Edit : To respond to queries about why I'm trying to do this. I've got a NodeJS server that does what I want it to do with some JS Data Sources. I'm wanting to add a Java Data Source that will communicate with the NodeJS server using websockets. There's a bunch of request/response logic that is common for every data source so I was hoping to funnel requests that hit the Java Data Source through a short JS pipeline containing this logic. I could duplicate the logic in Java, but I'm trying to avoid writing and having to maintain the same thing in 2 different languages.

It's entirely possible that what I'm trying to do is daft, but I'm in prototype stage so I'm experimentng.

Programming Guy
  • 7,259
  • 11
  • 50
  • 59
  • Is there some reason you've selected node over nashorn? – Elliott Frisch Jul 15 '17 at 15:11
  • If you want to process things with node.js, why not just have the client talk directly to a node.js server with ajax and get the Java out of the way? It will then be simple to do everything async. I guess I don't understand why you're trying to mix Java and node.js. – jfriend00 Jul 15 '17 at 16:38
  • I would highly recommend you use a unix datagram socket to communicate back and forth, and run the two processes independently. I know nothing about J2V8, but have written a lot of C++ to V8 stuff. The single threaded nature of node really works best if you center the node program around evented IO, otherwise you run the risk of slowing down the node.js main thread, and causing all kind of issues. A unix datagram socket, or regular unix socket (aka. 'pipe' in node.js parlance) will work well, and be the easiest to implement. A JVM, and all its under the cover magic, w/ node sounds dirty. – EdH Jul 15 '17 at 18:12
  • You will get better performance this way, b/c the libuv (the underlying node.js event/IO library) will use extra threads for IO. – EdH Jul 15 '17 at 18:13
  • @Elliot : I went with node because my javascript code is ES6 and also because that's what's running my code elsewhere. – Programming Guy Jul 16 '17 at 22:44

0 Answers0