Building Java scripts with groovy

Source: Internet
Author: User

I am a work Flow project, the workflow of each template engine needs to execute a dynamic business, these dynamic business has a variety of implementations, the most common is the user to write a script file, and then the workflow engine executes here, run the script file.

This function of running the script file was originally implemented with Scriptenginemanager. Ask the user to provide a jar to implement the business, and write a JS script, the script dynamically call Java built into the JS engine of several functions, these functions are to invoke the business function inside the jar dynamically.

But this is inconvenient, one is JS can not call Java code, can only preset Java functions, or through the pre-built Java function reflection of other business functions. At the same time JS syntax for Java programmers is too obscure, as a Java programmer, many of the JS syntax is not enough to understand, or even misunderstood.

So JS this way we deprecated, and now ready to use the groovy language to do the script.

Groovy believes that Java developers have heard that, after all, is the brother language of Java, he and Java are based on the JVM of this middleware language, the relationship can be analogous to c#.net and vb.net. There are a lot of JVM-based languages like groovy, and it's not going to unfold.

So what's good about using groovy?

1. The first requirement is that the dynamic scripting language is required to invoke the Java-written class library, so the language is best dynamic so that less code can be written, and the code length is critical for scripting. Groovy is a dynamic language that has features in closures and other dynamic languages. I'm not particularly interested in these features, primarily his collection object invocation is very simple, [] can be treated as a list object, {} can be treated as a map object, and is weakly typed, you don't need to spend a lot of time on type conversions like java. These are too important for scripting languages. After all, the scripting language simply organizes and calls Java-written business, does not do business, so it does not need to be very maintainable, but needs to be flexible enough to invoke business functions easily and efficiently.

2.Groovy Almost all Java syntax, you can use it directly as Java, so for the Java engineer is undoubtedly the gospel, if you refuse to use groovy's dynamic features, you can simply write Java code, completely use him as Java.

3.Groovy can call the Java existing class directly, that is, you can directly use a jar provided by the class library, this is too important for us, directly new business function class, you can invoke the business interface inside the jar.

In order to test whether groovy can fulfill the above requirements, I made a test demo.

First, define a Java class that acts as a business jar.

public class Javafunction {public static int i = 0;public String run () {return "ran Java code";}}

Then directly groovyshell the test script

Import Groovy.lang.binding;import Groovy.lang.groovyshell;public class Groovyscriptenginetest {public static void main (string[] args) throws Exception {Binding binding = new binding (); Binding.setvariable ("Javafunction", New Javafunction () ); Groovyshell GS = new Groovyshell (binding), Object value = gs.evaluate (//Can introduce Java class "import com.zrsoft.groovyScriptEngineTest.JavaFunction; " + "println" Groovy can import the current Classpath class directly! ‘;" Code that can write groovy syntax. Groovy is a dynamic language, better than Java scripting + "println" Running Groovy Code! ‘;" Java-style groovy, in fact, is Java code, because groovy supports java+ "String Javacode = \" To run Java-style groovy code! \";" + "System.out.println (Javacode);" You can create a Java custom object directly + "javafunction newjavafunction = new Javafunction ();" + "println \" creates Java objects in the groovy engine, and \ "+ Newjavafunction.run ();" You can invoke the script preset Java object + "return Javafunction.run ();"); System.out.println ("Presets Java objects for Groovyshell, and in groovy" + Value + ", proving that groovy can return variables to java.) ");}}

The above tests are all passed

Groovy can import the classes in the current CLASSPATH directly! Groovy code is running! Run Java-style groovy code! Creating Java objects in the groovy engine and running Java code to Groovyshell the Java object, and running Java code in groovy, proves that groovy can return variables to java.

Of course, the above test is not enough to let us determine that using groovy as a scripting scenario is possible, and that performance and post-run memory releases need further investigation.

Building Java scripts with groovy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.