views:

38

answers:

2

Currently, I am using Rhino engine to execute some big blocks of javascript code on the server side. However, sometimes, it takes so long(more than 10 minutes) and eat up all CPU usage (at least on my local development env, it is doing this right now).

So I am wondering that what I can do to improve the current system or I can try a new one there.

Any thoughts would be appreciated.

Thanks!

PLUS: It throws the exception while the code is too big: java.lang.Exception: No Context associated with current Thread

A: 

You can compile the script before running it and cache the compiled version see http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/script/CompiledScript.html

(edit: ops... i don't know if that would work on sdk 1.4)

Julio Faerman
I seriously doubt that'll make much difference.
Pointy
@Julio Faerman Thank you. The fact is that the javascript code are dynamically generated from database, and not guaranteed to be the same every time. Besides, I donot want to sacrifice the compiling time either. Sorry that I didnot give the assumption at the beginning.
Jay
A: 

We first have to know how big are the Javascript blocks that you are executing? 10 lines? 50 lines? 1000 lines?

If its getting 50+, then perhaps compiling would help. Since you are using 1.4, you must be using the official Rhino distribution and not javax.script. In that case just use the rhino compiler.

First though, have you profiled your program? Perhaps its something else thats causing the issue.

Are you sure your algorithm is as efficient as possible?

Please consider these before blaming Rhino

TheLQ