Using J2V8 with Heroku

Using J2V8 with Heroku

J2V8 is a set of Java bindings for the popular V8 JavaScript engine. In this tutorial we will explore how to use J2V8 as a JavaScript engine on Heroku.

Heroku

Heroku is a cloud based Platform as a Service (PaaS) supporting several programming languages. Heroku originally started as a platform for hosting Ruby, but today it supports many languages including Java. With Java, you simply push your sources to Heroku, along with a maven pom file, and Heroku will build, deploy and manage your application. There are add-ons for integrating databases, push notifications, log aggregation and more.

J2V8

V8 is a JavaScript runtime developed by Google and written in C++.  V8 is highly performant and is the workhorse behind the Chrome web browser. We developed J2V8 to bring the power and performance of V8 to the Java platform. J2V8 wraps V8 as a native library and exposes many of the V8 APIs to Java via the Java Native Interface (JNI).

J2V8 is available in Maven Central, and the pre-packaged jar file contains the native libraries. However, because the native libraries are platform dependent, several flavours of J2V8 are available. Each flavour is appropriate for a particular platform such as Android Arm, Android x86, Linux 64 bit, etc…

Developing Against J2V8

J2V8 is available in Maven Central and the Maven Coordinates for J2V8 specify the platform. For example, if you are developing on Windows, your pom.xml might look like this:

<pre lang="xml">
<dependency>
  <groupId>com.eclipsesource.j2v8</groupId>
  <artifactId>j2v8_win32_x86_64</artifactId>
  <version>2.2.1</version>
</dependency></pre>

You can now begin using J2V8 in your Java applications. In this example we will execute a script and return the result during a doGet request.
<pre lang="java">
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  V8 runtime = V8.createV8Runtime();
  String stringScript = runtime.executeStringScript("'Hello from JS!'");
  resp.getWriter().print("Hello from Java! " + stringScript);
  runtime.release();
}
</pre>

However, if you colleagues are developing on other platforms, then this particular dependency won’t help them.

To help with this, we have created an all-in-one download that contains the native code for each platform. Un-tar the file, and point the java.library.path at that directory. J2V8 will load natives from the library path before the ones contained within the jar. This can be done with Heroku in the Procfile:

<pre lang="txt">
web:    java -Djava.library.path=/path/to/natives -cp target/classes:target/dependency/* Main
</pre>

Deploying on Heroku

To deploy a Java application that uses J2V8, update your pom.xml to use 64 bit linux . Since J2V8 can self extract the native libraries and because 64 bit linux is architecture used on the Heroku servers, by updating your pom.xml you can simply push your sources to the Heroku and it will handle the rest. If you set your java.library.path in the Procfile, then you can run local instances on any architecture too.

<pre lang="xml">
  <repositories>
   <repository>
    <id>snapshots-repo</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    <releases><enabled>false</enabled></releases>
    <snapshots><enabled>true</enabled></snapshots>
   </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>com.eclipsesource.j2v8</groupId>
      <artifactId>j2v8_linux_x86_64</artifactId>
      <version>3.0.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</pre>

A local deployment can be triggered by first building the application and then starting foreman.

<pre>
[%] mvn clean install
[%] foreman start web
</pre>

For deployments on Heroku hardware, push your sources to the Heroku remote and open the application.

<pre>
[%] git push heroku master
[%] heroku open
</pre>

Screen Shot 2015-06-04 at 2.26.53 PM

For more information on J2V8, follow me on Twitter.

No Comments

Sorry, the comment form is closed at this time.