Slide 1

Slide 1 text

Java / Spring — Node.js

Slide 2

Slide 2 text

1995 Sun Microsystems Cross-platform Applet dedicated to the Web 2009 Ryan Dahl, Joyent, Node.js Foundation Asynchronous event driven framework built on top of Google V8 JS engine 2002 Pivotal Software Inversion of control container with extensions to ease JEE development JAVA SPRING NODE.JS

Slide 3

Slide 3 text

Platinum members

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

In the real world

Slide 6

Slide 6 text

Rewrote account overview page • Using JavaScript on both the front-end and the back-end removed an artificial boundary between the browser and server, allowing engineers to code both • Built almost twice as fast with fewer people • Written in 33% fewer lines of code • Constructed with 40% fewer files • Double the requests per second vs. the Java application • 35% decrease in the average response time

Slide 7

Slide 7 text

Moved From Rails, 27 Servers Cut And Up To 20x Faster • Much better performance and lower memory overhead than other tested options, running up to 20x faster in some scenarios • Programmers could leverage their JavaScript skills • Front-end and back-end mobile teams could be combined into a single unit • Servers were cut to 3 from 30. Enough headroom remains to handle 10x current levels of resource utilisation

Slide 8

Slide 8 text

Open Source project www.walmartlabs.com Powers our highly available real time push notifications Completely re-engineered the application to increase efficiency and improve the partner and customer experience

Slide 9

Slide 9 text

“Node.js powers our web applications and has allowed our teams to move much faster in bringing their designs to life” “On the server side, our entire mobile software stack is completely built in Node. One reason was scale. The second is Node showed us huge performance gains” “Node’s evented I/O model freed us from worrying about locking and concurrency issues that are common with multithreaded async I/O”

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Node Package Management repository Largest module repository used by developers in the world

Slide 12

Slide 12 text

library / module log4j spring-xyz libraryZ libraryY libraryX … … …

Slide 13

Slide 13 text

library / module log4j spring-xyz libraryZ libraryY libraryX … … Most of the Java containers use 1 Class Loader lib1.2

Slide 14

Slide 14 text

library / module archiver express moduleX m1 … … … … m1 m1 m1 Modules work like JavaScript closure

Slide 15

Slide 15 text

Spring framework an IoC container that eases maintaining configurations and switching implementations

Slide 16

Slide 16 text

public class MyComponent { @Autowired private PushService pushService; @PersistenceContext private EntityManager entityManager; public Message process(Message msg) { … } } var config = require('config'); var push = require('./push'); var dao = require(config.dao); function process(message) { … } module.exports = { process: process }; www.npmjs.com/package/config a popular module to manage development to production configurations Node.js Spring IoC

Slide 17

Slide 17 text

Spring is also a series of projects that helps you build web and enterprise applications

Slide 18

Slide 18 text

Web/mobile application WEB app Hybrid app Server DB Push

Slide 19

Slide 19 text

Java / Spring WEB app Hybrid app Server DB Push

Slide 20

Slide 20 text

JavaScript / Node.js WEB app Hybrid app Server DB Push You already know the language!

Slide 21

Slide 21 text

function diff() { var start = document.forms[0].start.value; var end = document.forms[0].end.value; console.log(moment(end).diff(moment(start), 'days')); } Start End @RequestMapping(
 path = "/api",
 method = RequestMethod.POST,
 produces = "application/json") public String api(
 @RequestBody String start,
 @RequestBody String end) { DateTimeFormatter dtf =
 DateTimeFormat.forPattern("yyyy-MM-dd"); LocalDate lStart = LocalDate.parse(start, dtf); LocalDate lEnd = LocalDate.parse(end, dtf);
 ObjectNode res =
 JsonNodeFactory.instance.objectNode();
 int diff = Days.daysBetween(lStart, lEnd).getDays(); return res.put("diff", diff).toString(); } Web Server Web Page

Slide 22

Slide 22 text

function diff() { var start = document.forms[0].start.value; var end = document.forms[0].end.value; console.log(moment(end).diff(moment(start), 'days')); } Start End var express = require('express') , bodyParser = require('body-parser') , moment = require('moment'); var app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(express.static(__dirname + '/public')); app.post('/api', (req, res) => { var start = req.body.start; var end = req.body.end; var diff = moment(end).diff(moment(start), 'days'); res.json({ diff:diff }); }); app.listen(3000); Web Server Web Page

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Run tests, package, minify, zip, manipulate files, git, generate manuals, upload, … Building, Testing 25% of our time in dev.

Slide 25

Slide 25 text

Building It's much more powerful to use a full language than writing build scripts from a fixed set of directives

Slide 26

Slide 26 text

Building gulp.task('scripts', function() {
 return gulp.src(findSources())
 .pipe(jshint('.jshintrc'))
 .pipe(jshint.reporter('default'))
 .pipe(concat('main.js'))
 .pipe(gulp.dest('dist/assets/js'))
 .pipe(rename({suffix: '.min'}))
 .pipe(uglify())
 .pipe(gulp.dest('dist/assets/js'))
 .pipe(notify({ message: 'Task complete' }));
 });

Slide 27

Slide 27 text

Testing Java / Spring, HTTP Client + Thread sleep? Node.js, request + setTimeout but will it be a callback hell? Write a test that invokes the /api 4 times, in sequence with 1 second delay

Slide 28

Slide 28 text

Callback hell function test(body, callback) {
 // POST 127.0.0.1:3000/api
 } test(body1, function(err, res) {
 test(body2, function(err, res) {
 test(body3, function(err, res) {
 …
 });
 });
 }); Move to Promises

Slide 29

Slide 29 text

function test(start, end, diff) {
 return new Promise((resolve, reject) => {
 request(
 { method: 'POST', uri: 'http://127.0.0.1:3000/api', form: { start, end }
 }, (error, res, body) => {
 if (error || JSON.parse(body).diff !== diff) { reject(error || 'diff do not match'); } else { resolve(); }
 });
 });
 } function wait1s() {
 return new Promise((resolve, reject) => setTimeout(() => resolve(), 1000));
 } test('2015-01-01', '2015-01-10', 9)
 .then(() => wait1s())
 .then(() => test('2015-01-01', '2015-01-11', 10))
 .then(() => wait1s())
 .then(() => test('2015-01-01', '2015-01-04', 3))
 .then(() => wait1s())
 .then(() => test('2015-01-01', '2015-01-06', 5))
 .then(() => console.log('All tests passed'))
 .catch(err => console.error(err));

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Food for thought Using the same language everywhere is a huge value for us Does a strongly typed language help you? There is a trend shared by the community to publish Node.js modules easy to use Asynchronous code has never been so easy

Slide 32

Slide 32 text

Food for thought Source code documentation is a key factor when building an application with JavaScript The Node.js ecosystem does not compete yet with some sophisticated libraries It’s almost impossible to know what your Node.js program is doing at all times

Slide 33

Slide 33 text

THANK YOU