In this tutorial we are going to play with nashorn and camel to create a very interesting mix.

Nashorn is the javascript engine shipped with the last release of jdk: 8.

Using Nashorn it's possible to launch shell script using java.

You can launch nashorn typing:

> /usr/java/jdk1.8.0_31/bin/jjs                                     

or

> jjs

Nashorn works like any other shell, but it uses javascript! The previous command opens the interactive javascript shell. With Nashorn you can execute javascript code calling also java methods on objects.

In this example we've added the camel library in the classpath of the project and the code is organized inside a file: camel.sh

The example is simple and powerful.

In the first line:

#!/usr/java/jdk1.8.0_31/bin/jjs -fv

the OS is notified: "ehi I'm a new shell:jjs

The command is:

 jjs -cp ./log4jproperties:./CamelInNashorn.jar:./src:. camel.sh 

-jjs is the nahsorn shell

- cp is the classpath argument

You can launch a jjs script also using directly ./filescript.sh or sh ./filescript.sh, but there is a bug (https://bugs.openjdk.java.net/browse/JDK-8072138) with the cp argument so that I have preferred to use jjs -cp.

Then we have to create a camel route. it is a simple extension of a RouteBuilder that implements the method configure.

In nashorn we can extend a java type in two steps:

1) Have the reference to the java type to extends

2) Extend it

var RouteBuilder = Java.type("org.apache.camel.builder.RouteBuilder");
var myRouteBuilder = Java.extend(RouteBuilder);

Now we can configure the camel route. A camel route is a simple flow of activities that implements the Enterprise Integration Pattern.

In this example we use an inline anonymous class.

This construction has got a little inconvenience: we can override or access to a method in the parent class only having a reference to the superclass using the static Java.super(parent) method. The Java.type, Java.extend, Java.super methods will be not loved by purist of OOP.

And now we are ready to create the route using the camel dsl. It's auto-explaining: it takes from a folder some files (xml files) and filter them using xpath. The file is redirected to another folder based on the author of the book.

#!/usr/java/jdk1.8.0_31/bin/jjs -fv

var RouteBuilder = Java.type("org.apache.camel.builder.RouteBuilder");
var myRouteBuilder = Java.extend(RouteBuilder);
var route=new myRouteBuilder(){
    configure: function() {
       var _super = Java.super(route);     
       _super.from("file:src/data?noop=true")
			.choice()
                		.when( _super.xpath("/book/author = 'Alessio Soldano'"))
                    			.log("WebServices")
                    			.to("file:target/messages/WS")
			        .when( _super.xpath("/book/author = 'Francesco Marchioni'"))
                    			.log("Wildfly")
                    			.to("file:target/messages/Wildfly")
                		.otherwise()
                    			.log("Other books")
                    			.to("file:target/messages/others");
        
    }
};

In order to launch the route: make it run and leave it watching for new added files we use the helper class: "org.apache.camel.main.Main": this class manages the camelcontext and routes. The complete code of camel.sh script is:

#!/usr/java/jdk1.8.0_31/bin/jjs -fv

var RouteBuilder = Java.type("org.apache.camel.builder.RouteBuilder");
var myRouteBuilder = Java.extend(RouteBuilder);
var route=new myRouteBuilder(){
    configure: function() {
       var _super = Java.super(route);     
       _super.from("file:src/data?noop=true")
			.choice()
                		.when( _super.xpath("/book/author = 'Alessio Soldano'"))
                    			.log("WebServices")
                    			.to("file:target/messages/WS")
			        .when( _super.xpath("/book/author = 'Francesco Marchioni'"))
                    			.log("Wildfly")
                    			.to("file:target/messages/Wildfly")
                		.otherwise()
                    			.log("Other books")
                    			.to("file:target/messages/others");
        
    }
};


var Main = Java.type("org.apache.camel.main.Main")
var mainCamel = new Main();
mainCamel.enableHangupSupport();
mainCamel.addRouteBuilder(route);
mainCamel.run();

To run the script we need to have the jdk 8 installed. It make the jjs command available.

The shell command is:

jjs -cp ./log4jproperties:./CamelInNashorn.jar:./src:. camel.sh

The classpath is referencing two files:

1) CamelINashorn.jar: is a jar with camel library

2) src folder is the folder containing data/message[12].xml files used as input

As the Nashorn shell run in a jvm and camel expose its MBeans it is possible to monitor the route using jmx.

You can inspect inside the jconsole or other tools connected to the jmx layer the number of messages completed, the messages in error and so on.

If you have the jdk installed just type:

jconsole

and click on the process related to the Nashorn shell.

You can find the code here:

https://github.com/mastertheintegration/Camel-in-Nashorn

To prepare the CamelInNashorn.jar just type in the same folder of pom.xml:

mvn clean compile assembly:single

Then copy the log4j.properties and the src folder inside the project folder and launch it:

 jjs -cp ./log4jproperties:./CamelInNashorn.jar:./src:. camel.sh 

0
0
0
s2smodern