We're using Closure Compiler jar file to minify our JS files. These JS files are generated when a client hit the [Save] button on their settings screen.
Each call to the Closure Compiler jar file takes 3-6 seconds because it starts a JVM per each call. This is way too long, and for no good reason... I would be glad to keep the JVM up and running in the background, either on boot or first call.
Is there a way to load JAR files from a running JVM machine as a service, or something like that? or perhaps a way to 'cache' the JVM so that the next time a jar is called, it will not start a new JVM but rather use the previous one?
The environment is Ubuntu server, Django, Python. Here's the current code that calls the jar files:
import time, subprocess, random
# run google closure compiler
jarjs = os.path.join(ROOT_DIR, "compiler.jar")
fn_min = fn_max.replace('.js','.min.js')
p = subprocess.Popen(['java','-jar',jarjs,'--jscomp_off','internetExplorerChecks','--compilation_level','SIMPLE_OPTIMIZATIONS','--js',fn_max,'--js_output_file',fn_min], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = p.communicate()
I think that instead of doing it by yourself you can use Gradle. Gradle has an excellent feature called gradle daemon
which is a long-lived background JVM process that executes your builds much more quickly than would otherwise be the case.
Gradle has closure compiler plugin, so integration can be done easily.
repositories {
mavenCentral() //or jcenter()
}
configurations {
closureCompiler
}
dependencies {
closureCompiler 'com.google.javascript:closure-compiler:v20150609'
}
task compileJS(type: JavaExec){
classpath configurations.closureCompiler
main = 'com.google.javascript.jscomp.CommandLineRunner'
def closureArgs = []
//append all your command line options here
closureArgs << "--compilation_level=SIMPLE_OPTIMIZATIONS"
closureArgs << "--js_output_file=app.js"
closureArgs << "input1.js"
closureArgs << "input2.js"
closureArgs << "src/**.js"
args closureArgs
}
And you don't have to have gradle installed on your machine, you can use gradle wraper
which will download appropriate version of gradle before it started.
If you don't want to use gradle, you can use Drip instead. Drip is a launcher for the Java Virtual Machine that provides much faster startup times than the java command. The drip script is intended to be a drop-in replacement for the java command, only faster.
You can install drip and use drip
command it instead of java
in your python script.