(undefined is not a function!)
Like it or not, JavaScript is everywhere these days - from browser to server to mobile - and now you, too, need to learn the language or dive deeper than you have.
Dr. Axel Rauschmayer
I think JavaScript has been seen as a serious language for the last two or three years; I think now increasingly we’re seeing JavaScript as a platform.
(Sam Newman, ThoughtWorks’ Global Innovation Lead)
JavaScript has emerged both as a platform for server-side code but also a platform to host other languages.
(January, 2014)
...are similar than car and carpet are similar.
$ $JAVA_HOME/bin/jjs
jjs> print('Hello Nashorn!');
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("print('Hello Nashorn!');");
engine.eval(new FileReader("scriptfile.js"));
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("jsSayHello", "Nashorn");
package my.package;
public class MyJavaClass {
static String sayHello(String name) {
return String.format("Hello %s from Java!", name);
}
}
var MyJavaClass = Java.type('my.package.MyJavaClass');
var result = MyJavaClass.sayHello('Nashorn');
print(result); // Hello Nashorn from Java!
var avatar = require('org/glassfish/avatar');
var threads = require('org/glassfish/avatar/threads');
var app = avatar.application;
var name = app.name;
var bus = app.bus;
// listen for messages on topic 'hello'
bus.on('hello', function(body, msg) {
print(name + ' got message: ' + JSON.stringify(body));
});
// publishing to 'hello' topic (e.g. in file hello.js):
bus.publish('hello', { x : 'x', y : 'y' });
// start a background thread for publishing
threads.newBackgroundThread('background', 'hello.js').start();
bus.publish(topic, body);
bus.publishTo(address, topic, body);
bus.send(topic, body);
bus.sendTo(address, topic, body);
bus.reply(replyTo, body);
bus.subscribe(topic);
bus.unsubscribe(topic);
var avatar = require('org/glassfish/avatar');
var state = avatar.application.state;
state.put('key', {'value': 'myValue'});
state.keys(); // -> Array
state.contains('key'); // -> boolean
state.get('key'); // -> Object
state.remove('key'); // -> void
database setup
var store = avatar.newStore(‘mysql’, {
host: 'localhost',
port: 3306,
database: 'test',
username: 'root',
createDb: true
});
var Family = avatar.newModel('family', {
"name" : {
type : "string",
primary : true
},
"description" : "string"
});
var Product = avatar.newModel('product', {
"name" : {
type : "string",
primary : true
},
"madeBy" : "string",
"price" : "number",
"quantity" : "integer"
});
Family.hasMany(Product, {
as : 'products',
foreign : 'family'
});
store.bind(Family, Product);
store.connect(function() {
Product.create({
name: 'Widget',
price: 1.00,
quantity: 2,
}, function(err, w1) {
console.log(JSON.stringify(w1));
store.disconnect(function() {
// done
});
});
});
--> Java Magazin 03/2015