Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

about.me/wuman 2 Monday, July 1, 13

Slide 3

Slide 3 text

What is Vert.x? 3 Monday, July 1, 13

Slide 4

Slide 4 text

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. 4 Monday, July 1, 13

Slide 5

Slide 5 text

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 5 Monday, July 1, 13

Slide 6

Slide 6 text

Background on the C10K Problem 6 Monday, July 1, 13

Slide 7

Slide 7 text

Shift from Threading to Asynchronous the story of switching from Apache to Node.js 7 Monday, July 1, 13

Slide 8

Slide 8 text

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 8 Monday, July 1, 13

Slide 9

Slide 9 text

Asynchronous Programming Model vertx.createHttpServer().requestHandler( new Handler() { public void handle(HttpServerRequest request) { log.info("A request has arrived on the server!"); request.response().end(); } } ).listen(8080, "localhost"); 9 Monday, July 1, 13

Slide 10

Slide 10 text

But there are 3 problems with Node.js 10 Monday, July 1, 13

Slide 11

Slide 11 text

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 11 Monday, July 1, 13

Slide 12

Slide 12 text

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 12 Monday, July 1, 13

Slide 13

Slide 13 text

Introduction to Vert.x instance and Verticles Verticle Verticle Worker Verticle Worker Verticle Vert.x instance 13 Monday, July 1, 13

Slide 14

Slide 14 text

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. 14 Monday, July 1, 13

Slide 15

Slide 15 text

What about communication? 15 Monday, July 1, 13

Slide 16

Slide 16 text

Message Passing via the Event Bus Verticle Verticle Worker Verticle Worker Verticle Vert.x instance 16 Monday, July 1, 13

Slide 17

Slide 17 text

Very Powerful Event Bus Verticle Verticle Worker Verticle Verticle Verticle Worker Verticle 17 Monday, July 1, 13

Slide 18

Slide 18 text

Registering a Message Handler EventBus eb = vertx.eventBus(); Handler myHandler = new Handler() { public void handle(Message message) { System.out.println("I received a message " + message.body); } }; eb.registerHandler("test.address", myHandler); 18 Monday, July 1, 13

Slide 19

Slide 19 text

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 19 Monday, July 1, 13

Slide 20

Slide 20 text

Pub/Sub Model eb.publish("test.address", "hello world"); 20 Monday, July 1, 13

Slide 21

Slide 21 text

Point-to-Point Model eb.send("test.address", "This is a message", new Handler>() { public void handle(Message message) { System.out.println("I received a reply " + message.body); } }); EventBus eb = vertx.eventBus(); Handler myHandler = new Handler() { 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); 21 Monday, July 1, 13

Slide 22

Slide 22 text

Shared Maps and Sets ConcurrentMap map = vertx.sharedData().getMap("demo.mymap"); map.put("some-key", 123); Set set = vertx.sharedData().getSet("demo.myset"); set.add("some-value"); 22 Monday, July 1, 13

Slide 23

Slide 23 text

Module System 23 Monday, July 1, 13

Slide 24

Slide 24 text

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() { public void handle(final NetSocket socket) { Pump.createPump(socket, socket).start(); } }).listen(1234); } } vertx run Server.java 24 Monday, July 1, 13

Slide 25

Slide 25 text

Deploying Verticles programmatically // For example - deploy some other verticle container.deployVerticle("foo.js", new AsyncResultHandler() { public void handle(AsyncResult deployResult) { if (deployResult.succeeded()) { System.out.println(“Yay!”); } else { System.out.println(“Error: “ + deployResult.cause()); } } }); 25 Monday, July 1, 13

Slide 26

Slide 26 text

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 26 Monday, July 1, 13

Slide 27

Slide 27 text

Example Modules • JDBC, Redis, MongoDB Persistors • AMQP • Mailer • Persistent work queue • Authentication Manager • Session Manager • Web framework • Language implementations 27 Monday, July 1, 13

Slide 28

Slide 28 text

Programming Experience 28 Monday, July 1, 13

Slide 29

Slide 29 text

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 29 Monday, July 1, 13

Slide 30

Slide 30 text

Container API • Deploying and undeploying verticles • Deploying and undeploying modules • Logging • Retrieve verticle configuration 30 Monday, July 1, 13

Slide 31

Slide 31 text

Easy development • Gradle template • Maven archetype and plugin • IDE debugging and testing 31 Monday, July 1, 13

Slide 32

Slide 32 text

What is it good for? 32 Monday, July 1, 13

Slide 33

Slide 33 text

Applications • Realtime analytics dashboard • Big data queries and task coordination • Polyglot integration 33 Monday, July 1, 13

Slide 34

Slide 34 text

Some Pain Points 34 Monday, July 1, 13

Slide 35

Slide 35 text

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. 35 Monday, July 1, 13

Slide 36

Slide 36 text

Conclusion 36 Monday, July 1, 13

Slide 37

Slide 37 text

37 Monday, July 1, 13

Slide 38

Slide 38 text

“Using a bleeding-edge framework is exciting at first. Not so much afterwards.” David Wu 37 Monday, July 1, 13

Slide 39

Slide 39 text

Make it better. Contribute. http://vert-x.github.io/ 38 Monday, July 1, 13

Slide 40

Slide 40 text

Thank you 39 Monday, July 1, 13

Slide 41

Slide 41 text

Q & A 40 Monday, July 1, 13