Vert.x is an asynchronous application platform that runs on the JVM and supports multiple programming languages. It uses an event-driven, non-blocking model where event handlers are used to avoid blocking threads. Verticles are deployed units that isolate classloading and contain event loops with event handlers. The event bus allows asynchronous messaging between verticles on the same or different machines. Worker verticles can be used to handle blocking operations so as not to block the event loop.
Apidays New York 2024 - The value of a flexible API Management solution for O...
Vert.X mini-talk
1. orb@nostacktrace.com
Vert.x
A polyglot
asynchronous
application platform
Norman Richards
for the JVM
2. orb@nostacktrace.com
Do you need Async?
Conan, What is
best in life?
To crush their servers,
Norman Richards
s! them smoking before you,
and to hear the lamentations
of their admins!
http://www.mikeperham.com/
Conan, the C10K Barbarian
3. orb@nostacktrace.com
Ok, so of course that means ...
Norman Richards
http://bit.ly/ACrwel
5. orb@nostacktrace.com
Key advantages of vert.x
Runs on the JVM (Java 7+)
Choose the best language
Hybrid evented/threaded model
Norman Richards
6. orb@nostacktrace.com
Evented IO
One thread handles all requests
Everything is an event handler
Never block the thread
Do work quickly + register callbacks
Norman Richards
Any kind of service - not just HTTP
7. orb@nostacktrace.com
simple Example
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;
public class ServerExample extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest req) {
req.response.headers().put("Content-Type", "text/html; charset=UTF-8");
req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}
Norman Richards
}).listen(8080);
}
}
8. orb@nostacktrace.com
slightly bigger Example
server.requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest request) {
final Buffer body = new Buffer(0);
request.dataHandler(new Handler<Buffer>() {
public void handle(Buffer buffer) {
body.appendBuffer(buffer);
}
});
request.endHandler(new SimpleHandler() {
public void handle() {
// The entire body has now been received
Norman Richards
log.info("The total body received was " + body.length() + " bytes");
}
});
}
}).listen(8080, "localhost");
9. orb@nostacktrace.com
the verticle
the unit of management/deployment
Event Loop
classloader isolated
Event Handler
Event Handler
one thread / one event loop
Event Handler
Event Handler
only one handler running at a time
Norman Richards
VERTICLE
no multi-threaded worries
16. orb@nostacktrace.com
The event bus
ad-hoc addressing
pubsub or p2p messaging
not persistent
JSON-based messaging
Norman Richards
17. orb@nostacktrace.com
The event bus
Handler<Message<String>> myHandler = new Handler<Message<String>>() {
public void handle(Message<String> message) {
System.out.println("I received a message " + message.body);
// do work
message.reply("This is a reply");
}
};
eb.registerHandler("test.address", myHandler);
eb.send("test.address", "This is a message", new Handler<Message<String>>() {
Norman Richards
public void handle(Message<String> message) {
System.out.println("I received a reply " + message.body);
}
});
18. orb@nostacktrace.com
... in groovy
def myHandler = { message ->
println "I received a message ${message.body}"
// do work
message.reply "This is a reply"
}
eb.registerHandler("test.address", myHandler)
Norman Richards
eb.send("test.address", "This is a message") { message ->
println "I received a reply ${message.body}"
}
19. orb@nostacktrace.com
... in ruby
Vertx::EventBus.registerHandler('test.address') do |message|
puts("I received a message #{message.body}")
# do work
message.reply('This is a reply')
end
Vertx::EventBus.send('test.address', 'This is a message') do |message|
puts("I received a reply #{message.body}")
Norman Richards
end
20. orb@nostacktrace.com
... in python
def handler(message):
print "I received a message %s" % message.body
# do work
message.reply('This is a reply')
EventBus.registerHandler('test.address', handler)
def reply_handler(message):
Norman Richards
print "I received a reply %s" % message.body
EventBus.send('test.address', 'This is a message', reply_handler)
21. orb@nostacktrace.com
... in javscript
var myHandler = function(message, replier) {
log.info('I received a message ' + message);
// Now reply to it
replier('This is a reply');
}
eb.registerHandler('test.address', myHandler);
eb.send('test.address', 'This is a message', function(reply) {
Norman Richards
log.info('I received a reply ' + reply);
});
22. orb@nostacktrace.com
... in clojure !!!
(defhandle my-handle [message]
(println "I received a mesage" (:body message))
;; do work
(reply message "This is a reply"))
(register-handler event-bus "test.address" my-handle)
(send event-bus "test.address"
"This is a message"
Norman Richards
(handle [response]
(println "I received a reply" (:body response))))
23. orb@nostacktrace.com
WHAT about things that block?
blocking IO
CPU-intensive operations
legacy Java libraries
Norman Richards
27. orb@nostacktrace.com
Austin Clojure meetup
Next Meeting:
Monday, March 4. 7pm @ Capital Factory
Norman Richards
http://www.meetup.com/Austin-Clojure-Meetup/