1

I need to run some external code from my Java application that will be updated frequently and orthogonally to the rest of the application. As I do not to re-deploy the entire application for every code change (and for other reasons as well) we chose to use groovy for this code, and store it either on the file system or in the database.

From the documentation I understand I have two ways to run the code - Using the GroovyShell or the GroovyClassLoader (eval does not fit here)

What are the pros and cons of each method?

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125

2 Answers2

3

GroovyShell uses GroovyClassLoader underneath. Use GroovyShell unless you need a feature that's only provided by GroovyClassLoader.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • But does the GroovyShell cache the executed script? – David Rabinowitz Oct 05 '09 at 07:24
  • im no expert im just starting my own groovyshell embedding but the compilerconfiguration class includes /** * Gets the target directory for writing classes. */ public File getTargetDirectory() { return this.targetDirectory; } – mP. Jun 27 '11 at 12:01
2

GroovyShell uses the default classloader until you load something in a script that customizes the classpath, then it switches to a custom GroovyClassLoader, which can cause problems loading some jdbc drivers or jndi items, etc... So, if your default classloader already has the classpath it needs and , and if you can avoid instantiating a new GroovyClassLoader object in your Java code, then loading a shell script with a simple GroovyShell will use the default classloader and you'll be better off for it.

Hope I understood your question.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • I have a Java application which wraps some logic which is written in a custom DSL in groovy. The code of the Java app changes seldom, the groovy code (the DSL) is changed frequently. I don't wont the DSL code to be interpreted every time, but to be compiled to bytecode for later executions. Can the GroovyShell do it? – David Rabinowitz Dec 08 '11 at 15:04
  • If your intention is to compile into bytecode, then why use Groovy? Also, the part that changes frequently seems suitable to be compiled dynamically rather than pre-compiled. Personally, for a DSL, I would have used XTend rather than Groovy. Each person has their own preferences. – djangofan Oct 11 '12 at 02:38
  • Changes frequently means that a single script can be changed once a month, but there are total of dozens of changes a day (we have over 2500 scripts we manage). So we have enough changes that we do not want a re-deploy, but each code is changed seldom, so I want it to be complied. Regarding XTend - it looks nice, but the questions are (a) can I load it dynamically; and (b) what is the size of the community behind it. – David Rabinowitz Oct 11 '12 at 08:53