Development with Vert.x: an event-driven application framework for the JVM

41
Development with Vert.x: an event-driven application framework for the JVM David Wu @wuman blog.wu-man.com Taiwan Java User Group (2013/06/29) 1 Monday, July 1, 13

description

For a long time JVM developers have been sitting out watching web developers write agile and performant applications with frameworks such as node.js. Fortunately this is no longer the case. Vert.x is a powerful, bleeding edge event-driven application framework for the JVM. It features native support for non-blocking I/O and concurrency. It has a distributed event bus that that allows real-time communication between client and server. In addition to including out-of-the-box modules for easy development, vert.x has a module system that enables code reuse. To be even more cool, vert.x is polyglot, supporting components written in different languages such as Javascript, Ruby, and Python. In this speech I will give an overview of the vert.x framework and some of the caveats that you should be aware of.

Transcript of Development with Vert.x: an event-driven application framework for the JVM

Page 1: Development with Vert.x: an event-driven application framework for the JVM

Development with Vert.x:an event-driven application

framework for the JVMDavid Wu@wuman

blog.wu-man.com

Taiwan Java User Group (2013/06/29)

1Monday, July 1, 13

Page 2: Development with Vert.x: an event-driven application framework for the JVM

about.me/wuman2Monday, July 1, 13

Page 3: Development with Vert.x: an event-driven application framework for the JVM

What is Vert.x?

3Monday, July 1, 13

Page 4: Development with Vert.x: an event-driven application framework for the JVM

What most people will tell you about Vert.x

• A general-purpose application framework running on JVM

• Performant: asynchronous APIs, event-driven, non-blocking, etc.

• Highly scalable but also easy to implement concurrency

• Polyglot: intermix Java, JS, Ruby, Groovy, Python, etc.

4Monday, July 1, 13

Page 5: Development with Vert.x: an event-driven application framework for the JVM

What I think about Vert.x

• All-in-one framework that allows you to focus on your application rather than fiddling with different technologies

• Business friendly Apache License 2.0

• Loose coupling modules that interoperate in a system

5Monday, July 1, 13

Page 6: Development with Vert.x: an event-driven application framework for the JVM

Background on the C10K Problem

6Monday, July 1, 13

Page 7: Development with Vert.x: an event-driven application framework for the JVM

Shift from Threading to Asynchronous

the story of switching from Apache to Node.js

7Monday, July 1, 13

Page 8: Development with Vert.x: an event-driven application framework for the JVM

The inevitable comparison to Node.js

• A single event loop serving a high volume of connections via an asynchronous programming model

• Most of the real work are done in a pool of background threads

8Monday, July 1, 13

Page 9: Development with Vert.x: an event-driven application framework for the JVM

Asynchronous Programming Model

vertx.createHttpServer().requestHandler( new Handler<HttpServerRequest>() { public void handle(HttpServerRequest request) { log.info("A request has arrived on the server!"); request.response().end(); } }).listen(8080, "localhost");

9Monday, July 1, 13

Page 10: Development with Vert.x: an event-driven application framework for the JVM

But there are 3 problems with Node.js

10Monday, July 1, 13

Page 11: Development with Vert.x: an event-driven application framework for the JVM

Problems with Node.js

• Well, it’s not Java (and this is a Java User Group)

• Does not easily scale both vertically and horizontally

• Single event loop fails when you have CPU-intensive tasks or 3rd-party blocking APIs

11Monday, July 1, 13

Page 12: Development with Vert.x: an event-driven application framework for the JVM

Vert.x to the rescue

• Use all available cores on a single machine

• Horizontally scale out to multiple boxes

• Allow blocking calls to NOT run on an event loop

12Monday, July 1, 13

Page 13: Development with Vert.x: an event-driven application framework for the JVM

Introduction to Vert.x instance and Verticles

Vert

icle

Vert

icle

Wor

ker V

ertic

le

Wor

ker V

ertic

le

Vert.x instance

13Monday, July 1, 13

Page 14: Development with Vert.x: an event-driven application framework for the JVM

Verticles are extremely isolated

• Verticles are isolated with separate class loaders

• A verticle never gets executed by more than one thread concurrently

• No race conditions, no deadlocks. You write your code as single threaded.

14Monday, July 1, 13

Page 15: Development with Vert.x: an event-driven application framework for the JVM

What about communication?

15Monday, July 1, 13

Page 16: Development with Vert.x: an event-driven application framework for the JVM

Message Passing via the Event Bus

Vert

icle

Vert

icle

Wor

ker

Vert

icle

Wor

ker

Vert

icle

Vert.x instance

16Monday, July 1, 13

Page 17: Development with Vert.x: an event-driven application framework for the JVM

Very Powerful Event Bus

Vert

icle

Vert

icle

Wor

ker V

ertic

le

Vert

icle

Vert

icle

Wor

ker V

ertic

le

17Monday, July 1, 13

Page 18: Development with Vert.x: an event-driven application framework for the JVM

Registering a Message Handler

EventBus eb = vertx.eventBus();

Handler<Message> myHandler = new Handler<Message>() { public void handle(Message message) { System.out.println("I received a message " + message.body); }};

eb.registerHandler("test.address", myHandler);

18Monday, July 1, 13

Page 19: Development with Vert.x: an event-driven application framework for the JVM

Message Types

• Primitives and their boxed types

• String

• org.vertx.java.core.json.JsonObject

• org.vertx.java.core.json.JsonArray

• org.vertx.java.core.buffer.Buffer

19Monday, July 1, 13

Page 20: Development with Vert.x: an event-driven application framework for the JVM

Pub/Sub Model

eb.publish("test.address", "hello world");

20Monday, July 1, 13

Page 21: Development with Vert.x: an event-driven application framework for the JVM

Point-to-Point Model

eb.send("test.address", "This is a message", new Handler<Message<String>>() { public void handle(Message<String> message) { System.out.println("I received a reply " + message.body); } });

EventBus eb = vertx.eventBus();

Handler<Message> myHandler = new Handler<Message>() { public void handle(Message message) { System.out.println("I received a message " + message.body); message.reply("This is a reply"); }};

eb.registerHandler("test.address", myHandler);

21Monday, July 1, 13

Page 22: Development with Vert.x: an event-driven application framework for the JVM

Shared Maps and Sets

ConcurrentMap<String, Integer> map = vertx.sharedData().getMap("demo.mymap");

map.put("some-key", 123);

Set<String> set = vertx.sharedData().getSet("demo.myset");

set.add("some-value");

22Monday, July 1, 13

Page 23: Development with Vert.x: an event-driven application framework for the JVM

Module System

23Monday, July 1, 13

Page 24: Development with Vert.x: an event-driven application framework for the JVM

Writing Verticles

import org.vertx.java.core.Handler;import org.vertx.java.core.net.NetSocket;import org.vertx.java.core.streams.Pump;import org.vertx.java.platform.Verticle;

public class Server extends Verticle {

public void start() { vertx.createNetServer().connectHandler(new Handler<NetSocket>() { public void handle(final NetSocket socket) { Pump.createPump(socket, socket).start(); } }).listen(1234); }}

vertx run Server.java

24Monday, July 1, 13

Page 25: Development with Vert.x: an event-driven application framework for the JVM

Deploying Verticles programmatically

// For example - deploy some other verticle container.deployVerticle("foo.js", new AsyncResultHandler<String>() { public void handle(AsyncResult<String> deployResult) { if (deployResult.succeeded()) { System.out.println(“Yay!”); } else { System.out.println(“Error: “ + deployResult.cause()); } } });

25Monday, July 1, 13

Page 26: Development with Vert.x: an event-driven application framework for the JVM

Modules

• Package verticles into re-usable modules

• Simply a zip file containing module definition, code, and resources

• Very similar to the module system in Node.js

• Modules can be “zip” artifacts released in Maven Central

• Vert.x web site maintains a listing of Vert.x modules

26Monday, July 1, 13

Page 27: Development with Vert.x: an event-driven application framework for the JVM

Example Modules

• JDBC, Redis, MongoDB Persistors

• AMQP

• Mailer

• Persistent work queue

• Authentication Manager

• Session Manager

• Web framework

• Language implementations

27Monday, July 1, 13

Page 28: Development with Vert.x: an event-driven application framework for the JVM

Programming Experience

28Monday, July 1, 13

Page 29: Development with Vert.x: an event-driven application framework for the JVM

Core APIs

• TCP/SSL servers and clients

• HTTP/HTTPS servers and clients

• WebSockets servers and clients

• SockJS

• EventBus

• Shared Maps and Sets

• Buffers

• Flow control

• Timers

• Files

• Configuration

29Monday, July 1, 13

Page 30: Development with Vert.x: an event-driven application framework for the JVM

Container API

• Deploying and undeploying verticles

• Deploying and undeploying modules

• Logging

• Retrieve verticle configuration

30Monday, July 1, 13

Page 31: Development with Vert.x: an event-driven application framework for the JVM

Easy development

• Gradle template

• Maven archetype and plugin

• IDE debugging and testing

31Monday, July 1, 13

Page 32: Development with Vert.x: an event-driven application framework for the JVM

What is it good for?

32Monday, July 1, 13

Page 33: Development with Vert.x: an event-driven application framework for the JVM

Applications

• Realtime analytics dashboard

• Big data queries and task coordination

• Polyglot integration

33Monday, July 1, 13

Page 34: Development with Vert.x: an event-driven application framework for the JVM

Some Pain Points

34Monday, July 1, 13

Page 35: Development with Vert.x: an event-driven application framework for the JVM

Real life caveats

• Content assist in IDEs don’t work for busmod APIs

• You often find yourself tracing errors in the busmods, which can be difficult because the stack trace stops at message passing

• People writing in other languages often get frustrated by not being able to use native extensions that they are already used to.

• Hell of anonymous inner class callbacks and no good Java flow control libraries

• Java7 only. Doesn’t work on Android.

35Monday, July 1, 13

Page 36: Development with Vert.x: an event-driven application framework for the JVM

Conclusion

36Monday, July 1, 13

Page 37: Development with Vert.x: an event-driven application framework for the JVM

37Monday, July 1, 13

Page 38: Development with Vert.x: an event-driven application framework for the JVM

“Using a bleeding-edge framework is exciting at first.

Not so much afterwards.”

David Wu

37Monday, July 1, 13

Page 39: Development with Vert.x: an event-driven application framework for the JVM

Make it better.Contribute.

http://vert-x.github.io/

38Monday, July 1, 13

Page 40: Development with Vert.x: an event-driven application framework for the JVM

Thank you

39Monday, July 1, 13

Page 41: Development with Vert.x: an event-driven application framework for the JVM

Q & A

40Monday, July 1, 13