Here at Carfey, we’ve used Java’s native scripting API in our upcoming job scheduler to allow our clients to deploy new scripts at any time they wish, without redeploying their applications. Combined with our advanced configuration support, we think we’ve found a killer combination of flexibility and power.
What’s in the API?
hello world Result is: null
Really pretty simple. We just grab a script engine from the factory and evaluate our script.
You'll notice that the last value in the script is returned to our Java caller. This is a bit messy in a case like this. It would be preferable to invoke the desired method directly. Fortunately, this is possible in a generic fashion as well using Invocable:
If you wish to invoke methods on Java objects, it's as simple as providing the object in the script engine scope and using the scripting language's native syntax to make the method call:
Now that's powerful. We now have full interop from Java to the scripting language of our choice without any nasty legwork like we deal with when using JNI or other technologies.
There is plenty more to this Java scripting API. There are issues of scope, implementing interfaces, instantiating Java objects from within a scripting language, etc., but these are advanced features that we don't always care about. With all the powerful features available, the Java scripting API is a natural choice for cases where you need dynamic scripting embedded within your application.
A good primer on the scripting API can be found here: Java Scripting Programmer's Guide
We've used the scripting API along with our job parameter configuration support in our upcoming job scheduler so that our clients can quickly write new jobs without redeploying their applications. If you have suggestions for features or would like more information, post a comment and we will follow up.