3

I'm developing a Groovy shell script which is launched frequently (doing rather small tasks), therefore I need a fast JVM startup time. To achieve that I'm trying to launch it with Nailgun.

I have installed Nailgun as an Ubuntu package. Then I fixed the argument bug by linking /usr/bin/ng-server to /usr/bin/ng. I'm starting the Nailgun-Server like this:

java -cp /usr/share/java/nailgun-0.7.1.jar -server com.martiansoftware.nailgun.NGServer

I have this simple dummy Groovy script named hello.groovy just to test the nailgun-server:

#!/usr/bin/env groovy
def sayHello() {
  println("Hello Groovy!");
}

sayHello();

I compiled the file with groovyc to hello.class.

Now I want to launch that script within the Nailgun server. My naive approach to do that would be:

ng hello
ng hello.sayHello

But all I get are ClassNotFoundExceptions:

java.lang.ClassNotFoundException: hello
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.martiansoftware.nailgun.NGSession.run(Unknown Source)

So, what is the right way to launch my Groovy script with the Nailgun Server? I'd also appreciate some good sites/tutorials about how to use Nailgun, it's really hard to get any information how to use it...

EDIT:

I would also appreciate a complete example (including how a specific class is called with ng) for using nailgun with pure Java classes, as I could also not get ng working with any Java class.

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
  • 1
    For anyone using Nailgun: Have a look at https://github.com/flatland/drip which could in some cases be more practical to use, since its concept is quite different (Nailgun reuses a JVM, causing it to get more and more dirty over time, whereas Drip keeps spawning clean new JVMs) – Wolkenarchitekt Feb 06 '13 at 10:41

3 Answers3

5

Finally got it. I just did not understand that I have to add all needed classes to the Nailgun classpath first (this SO question gave me the final hints).

First, add Groovy to the classpath:

ng ng-cp /usr/share/java/groovy-all.jar

Then add the directory which contains the Groovy script/Java class to the classpath, in my case it's:

ng ng-cp /home/$USER/tools/groovy

Now I can run my Groovy script with Nailgun like this:

ng hello
Community
  • 1
  • 1
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
0

I think jruby.org has the most recent reference to this long lost weapon (which some day Oracle should take up , since the sluggish cold start is an achilles heel for most new birds)

did you groovyc your script into classes?

then 'ng ... hello' with groovy on the classpath

my blind guess(too lazy to reinstall my railgun after using rvm to maintain jRuby)

han
  • 175
  • 2
  • Yes i groovyc'd the class, but it makes no difference. Thanks for the JRuby hint. It seems like JRuby included the Nailgun server directly into the runtime. Unfortunately that doesn't help me in getting Nailgun running groovy or java code. – Wolkenarchitekt Jul 20 '11 at 20:54
0

i will be surprised if the hello that is run is hello.groovy (interpreted) and not the compiled down hello.class

not a sustainable solution they should just have a JSR for hot start client JVM

so that JVM in client mode starts into a background daemon, waiting to spawn a java process, and clear all the illusories of poor performance Java has always endured

of course for safety sake, production will always use the full hotspot

han
  • 26
  • 1
  • Not an answer to my question. However, Nailgun is actually running the compiled .class. So I built a shell script as a launcher for my groovy-script, which recompiles the script and restarts/reloads the script into nailgun if needed. After the first run after compilation, it can be re-run without JVM startup time. Good enough for my use case. – Wolkenarchitekt Jul 22 '11 at 17:46