Name |
|
---|---|
Backends | JVM |
Maven coordinates | |
Compatible Ceylon release |
JVM: 1.1.0 (outdated) |
Published | Oct 10, 2014 |
Stats |
Downloads (JVM): 3192 Source downloads: 460 |
Authors |
Julien Viet |
License | ASL2 |
Description |
Vert.x for CeylonVert.x is a lightweight, high performance application platform for the JVM that's designed for modern mobile, web, and enterprise applications. The original Vert.x documentation explains how to use Vert.x. This Ceylon module is a port of the Vert.x API to Ceylon, with this API you can:
ReferenceThe Event BusSee Shared DataSee BuffersMost data in Vert.x is shuffled around using instances of [[org.vertx.java.core.buffer::Buffer]]. We chose deliberately to use the Vert.x native type as it can easily used from Ceylon and a wrapper would complicate the bridge. A Buffer represents a sequence of zero or more bytes that can be written to or read from, and which expands automatically as necessary to accomodate any bytes written to it. You can perhaps think of a buffer as smart byte array. Creating BuffersCreate a new empty buffer: value buff = Buffer(); Create a buffer from a String. The String will be encoded in the buffer using UTF-8. value buff = Buffer("some-string"); Create a buffer from a String: The String will be encoded using the specified encoding, e.g: value buff = Buffer("some-string", "UTF-16"); Create a buffer from a byte[] (using [[java.lang::ByteArray]]) ByteArray bytes = ...; value buff = Buffer(bytes); Create a buffer with an initial size hint. If you know your buffer will have a certain amount of data written to it you can create the buffer and specify this size. This makes the buffer initially allocate that much memory and is more efficient than the buffer automatically resizing multiple times as data is written to it. Note that buffers created this way are empty. It does not create a buffer filled with zeros up to the specified size. value buff = Buffer(10000); Writing to a BufferThere are two ways to write to a buffer: appending, and random access. In either case buffers
will always expand automatically to encompass the bytes. It's not possible to get an
Appending to a BufferTo append to a buffer, you use the The return value of the appendXXX methods is the buffer itself, so these can be chained: value buff = Buffer(); buff.appendInt(123).appendString("hello\n"); Random access buffer writesYou can also write into the buffer at a specific index, by using the value buff = Buffer(); buff.setInt(1000, 123); buff.setString(0, "hello"); Reading from a BufferData is read from a buffer using the Buffer buff = ...; Integer i = buff.getInt(0); Other buffer methods:
JSONWhereas JavaScript has first class support for JSON, and Ruby has Hash literals which make representing JSON easy within code, things aren't so easy in Ceylon A JSON object is represented by instances of [[ceylon.json::Object]]. A JSON array is represented by instances of [[ceylon.json::Array]]. A usage example would be using a Ceylon verticle to send or receive JSON messages from the event bus. value eb = vertx.eventBus(); value obj = Object { "foo"->"wibble", "age"->1000 }; eb.send("some-address", obj); // .... // And in a handler somewhere: shared void handle(Message<Object> message) { print("foo is ``message.body["foo"]``"); print("age is ``message.body["age"]``"); } Delayed and Periodic TasksIt's very common in Vert.x to want to perform an action after a delay, or periodically. In standard verticles you can't just make the thread sleep to introduce a delay, as that will block the event loop thread. Instead you use Vert.x timers. Timers can be one-shot or periodic. We'll discuss both One-shot TimersA one shot timer calls an event handler after a certain delay, expressed in milliseconds. To set a timer to fire once you use the value timerId = vertx.setTimer(1000, (Integer timerId) => print("And one second later this is printed")); print("First this is printed"); The return value is a unique timer id which can later be used to cancel the timer. The handler is also passed the timer id. Periodic TimersYou can also set a timer to fire periodically by using the value timerId = vertx.setTimer(1000, (Integer timerId) => print("And every second this is printed")); print("First this is printed"); Cancelling timersTo cancel a periodic timer, call the value timerId = vertx.setTimer(1000, (Integer timerId) => print("Should not be printed")); // And immediately cancel it vertx.cancelTimer(timerID); Or you can cancel it from inside the event handler. The following example cancels the timer after it has fired 10 times. variable Integer count = 0; vertx.setTimer { delay = 1000; void handle(Integer timerId) { print("In event handler ``count``"); if (++count == 10) { vertx.cancelTimer(timerId); } } }; Writing TCP Servers and ClientsFlow Control - Streams and PumpsSee Writing HTTP Servers and ClientsSee Routing HTTP requests with Pattern MatchingSee WebSocketsSee SockJSSee SockJS - EventBus BridgeSee File System |
Dependencies |
ceylon.io/1.1.0
shared
ceylon.json/1.1.0
shared
ceylon.promise/1.1.0
shared
ceylon.time/1.1.0
shared
io.vertx.core/2.1.2
shared
io.vertx.platform/2.1.2
shared
java.base/7 JDK
shared
|
Usage |
import io.vertx.ceylon.core "1.0.0"; $ ceylon run io.vertx.ceylon.core/1.0.0 <property name="ceylon.home" value="…"/> <property name="ceylon.ant.lib" value="${ceylon.home}/lib/ceylon-ant.jar"/> <path id="ant-tasks"> <pathelement path="${ceylon.ant.lib}"/> </path> <typedef resource="com/redhat/ceylon/ant/antlib.xml" classpathref="ant-tasks"/> <target name="run"> <ceylon-run module="io.vertx.ceylon.core/1.0.0"/> </target> |
Module links |
Members Imported By Browse Download .car No .js archive Download source archive Download module documentation View API documentation |