# Vert.x Core Manual

At the heart of Vert.x is a set of Java APIs that we call Vert.x Core

Repository.

Vert.x core provides functionality for things like:

- Writing TCP clients and servers

- Writing HTTP clients and servers including support for WebSockets

- The Event bus

- Shared data - local maps and clustered distributed maps

- Periodic and delayed actions

- Deploying and undeploying Verticles

- Datagram Sockets

- DNS client

- File system access

- High availability

- Clustering

The functionality in core is fairly low level - you won’t find stuff like database access, authorisation or high level web functionality here - that kind of stuff you’ll find in Vert.x ext (extensions).

Vert.x core is small and lightweight. You just use the parts you want. It’s also entirely embeddable in your existing applications - we don’t force you to structure your applications in a special way just so you can use Vert.x.

You can use core from any of the other languages that Vert.x supports. But here’a a cool bit - we don’t force you to use the Java API directly from, say, JavaScript or Ruby - after all, different languages have different conventions and idioms, and it would be odd to force Java idioms on Ruby developers (for example). Instead, we automatically generate an idiomatic equivalent of the core Java APIs for each language.

From now on we’ll just use the word core to refer to Vert.x core.

If you are using Maven or Gradle, add the following dependency to the dependencies section of your project descriptor to access the Vert.x Core API and enable the JavaScript support:

- Maven (in your `pom.xml`):

    <dependency>
     <groupId>io.vertx</groupId>
     <artifactId>vertx-core</artifactId>
     <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-lang-js</artifactId>
      <version>3.1.0</version>
    </dependency>

- Gradle (in your `build.gradle` file):

    compile "io.vertx:vertx-core:3.1.0"
    compile "io.vertx:vertx-lang-js:3.1.0"

Let’s discuss the different concepts and features in core.

## In the beginning there was Vert.x

Note | Much of this is Java specific - need someway of swapping in language specific parts

You can’t do much in Vert.x-land unless you can commune with a `Vertx` object!

It’s the control centre of Vert.x and is how you do pretty much everything, including creating clients and servers, getting a reference to the event bus, setting timers, as well as many other things.

So how do you get an instance?

If you’re embedding Vert.x then you simply create an instance as follows:

    var Vertx = require("vertx-js/vertx");
    var vertx = Vertx.vertx();

If you’re using Verticles

Note | Most applications will only need a single Vert.x instance, but it’s possible to create multiple Vert.x instances if you require, for example, isolation between the event bus or different groups of servers and clients.

### Specifying options when creating a Vertx object

When creating a Vertx object you can also specify options if the defaults aren’t right for you:

    var Vertx = require("vertx-js/vertx");
    var vertx = Vertx.vertx({
      "workerPoolSize" : 40
    });

The `VertxOptions` object has many settings and allows you to configure things like clustering, high availability, pool sizes and various other settings. The Javadoc describes all the settings in detail.

### Creating a clustered Vert.x object

If you’re creating a clustered Vert.x (See the section on the event bus for more information on clustering the event bus), then you will normally use the asynchronous variant to create the Vertx object.

This is because it usually takes some time (maybe a few seconds) for the different Vert.x instances in a cluster to group together. During that time, we don’t want to block the calling thread, so we give the result to you asynchronously.

## Are you fluent?

You may have noticed that in the previous examples a fluent API was used.

A fluent API is where multiple methods calls can be chained together. For example:

    request.response().putHeader("Content-Type", "text/plain").write("some text").end();

This is a common pattern throughout Vert.x APIs, so get used to it.

Chaining calls like this allows you to write code that’s a little bit less verbose. Of course, if you don’t like the fluent approach we don’t force you to do it that way, you can happily ignore it if you prefer and write your code like this:

    var response = request.response();
    response.putHeader("Content-Type", "text/plain");
    response.write("some text");
    response.end();

## Don’t call us, we’ll call you.

The Vert.x APIs are largely event driven. This means that when things happen in Vert.x that you are interested in, Vert.x will call you by sending you events.

Some example events are:

- a timer has fired

- some data has arrived on a socket,

- some data has been read from disk

- an exception has occurred

- an HTTP server has received a request

You handle events by providing handlers to the Vert.x APIs. For example to receive a timer event every second you would do:

    vertx.setPeriodic(1000, function (id) {
      // This handler will get called every second
      console.log("timer fired!");
    });

Or to receive an HTTP request:

    // Respond to each http request with "Hello World"
    server.requestHandler(function (request) {
      // This handler will be called every time an HTTP request is received at the server
      request.response().end("hello world!");
    });

Some time later when Vert.x has an event to pass to your handler Vert.x will call it asynchronously.

This leads us to some important concepts in Vert.x:

## Don’t block me!

With very few exceptions (i.e. some file system operations ending in 'Sync'), none of the APIs in Vert.x block the calling thread.

If a result can be provided immediately, it will be returned immediately, otherwise you will usually provide a handler to receive events some time later.

Because none of the Vert.x APIs block threads that means you can use Vert.x to handle a lot of concurrency using just a small number of threads.

With a conventional blocking API the calling thread might block when:

- Reading data from a socket

- Writing data to disk

- Sending a message to a recipient and waiting for a reply.

- …​ Many other situations

In all the above cases, when your thread is waiting for a result it can’t do anything else - it’s effectively useless.

This means that if you want a lot of concurrency using blocking APIs then you need a lot of threads to prevent your application grinding to a halt.

Threads have overhead in terms of the memory they require (e.g. for their stack) and in context switching.

For the levels of concurrency required in many modern applications, a blocking approach just doesn’t scale.

## Reactor and Multi-Reactor

We mentioned before that Vert.x APIs are event driven - Vert.x passes events to handlers when they are available.

In most cases Vert.x calls your handlers using a thread called an event loop.

As nothing in Vert.x or your application blocks, the event loop can merrily run around delivering events to different handlers in succession as they arrive.

Because nothing blocks, an event loop can potentially deliver huge amounts of events in a short amount of time. For example a single event loop can handle many thousands of HTTP requests very quickly.

We call this the Reactor Pattern.

You may have heard of this before - for example Node.js implements this pattern.

In a standard reactor implementation there is a single event loop thread which runs around in a loop delivering all events to all handlers as they arrive.

The trouble with a single thread is it can only run on a single core at any one time, so if you want your single threaded reactor application (e.g. your Node.js application) to scale over your multi-core server you have to start up and manage many different processes.

Vert.x works differently here. Instead of a single event loop, each Vertx instance maintains several event loops. By default we choose the number based on the number of available cores on the machine, but this can be overridden.

This means a single Vertx process can scale across your server, unlike Node.js.

We call this pattern the Multi-Reactor Pattern to distinguish it from the single threaded reactor pattern.

Note | Even though a Vertx instance maintains multiple event loops, any particular handler will never be executed concurrently, and in most cases (with the exception of worker verticles) will always be called using the exact same event loop.

## The Golden Rule - Don’t Block the Event Loop

We already know that the Vert.x APIs are non blocking and won’t block the event loop, but that’s not much help if you block the event loop yourself in a handler.

If you do that, then that event loop will not be able to do anything else while it’s blocked. If you block all of the event loops in Vertx instance then your application will grind to a complete halt!

So don’t do it! You have been warned.

Examples of blocking include:

- Thread.sleep()

- Waiting on a lock

- Waiting on a mutex or monitor (e.g. synchronized section)

- Doing a long lived database operation and waiting for a result

- Doing a complex calculation that takes some significant time.

- Spinning in a loop

If any of the above stop the event loop from doing anything else for a significant amount of time then you should go immediately to the naughty step, and await further instructions.

So…​ what is a significant amount of time?

How long is a piece of string? It really depends on your application and the amount of concurrency you require.

If you have a single event loop, and you want to handle 10000 http requests per second, then it’s clear that each request can’t take more than 0.1 ms to process, so you can’t block for any more time than that.

The maths is not hard and shall be left as an exercise for the reader.

If your application is not responsive it might be a sign that you are blocking an event loop somewhere. To help you diagnose such issues, Vert.x will automatically log warnings if it detects an event loop hasn’t returned for some time. If you see warnings like these in your logs, then you should investigate.

    Thread vertx-eventloop-thread-3 has been blocked for 20458 ms

Vert.x will also provide stack traces to pinpoint exactly where the blocking is occurring.

If you want to turn of these warnings or change the settings, you can do that in the `VertxOptions` object before creating the Vertx object.

## Running blocking code

In a perfect world, there will be no war or hunger, all APIs will be written asynchronously and bunny rabbits will skip hand-in-hand with baby lambs across sunny green meadows.

But.. the real world is not like that. (Have you watched the news lately?)

Fact is, many, if not most libraries, especially in the JVM ecosystem have synchronous APIs and many of the methods are likely to block. A good example is the JDBC API - it’s inherently synchronous, and no matter how hard it tries, Vert.x cannot sprinkle magic pixie dust on it to make it asynchronous.

We’re not going to rewrite everything to be asynchronous overnight so we need to provide you a way to use "traditional" blocking APIs safely within a Vert.x application.

As discussed before, you can’t call blocking operations directly from an event loop, as that would prevent it from doing any other useful work. So how can you do this?

It’s done by calling `executeBlocking` specifying both the blocking code to execute and a result handler to be called back asynchronous when the blocking code has been executed.

    var someAPI = new (Java.type("examples.CoreExamples.BlockingAPI"))();
    vertx.executeBlocking(function (future) {
      // Call some blocking API that takes a significant amount of time to return
      var result = someAPI.blockingMethod("hello");
      future.complete(result);
    }, function (res, res_err) {
      console.log("The result is: " + res);
    });

By default, if executeBlocking is called several times from the same context (e.g. the same verticle instance) then the different executeBlocking are executed serially (i.e. one after another).

If you don’t care about ordering you can call `executeBlocking` specifying `false` as the argument to `ordered`. In this case any executeBlocking may be executed in parallel on the worker pool.

An alternative way to run blocking code is to use a worker verticle

A worker verticle is always executed with a thread from the worker pool.

## Verticles

Vert.x comes with a simple, scalable, actor-like deployment and concurrency model out of the box that you can use to save you writing your own.

This model is entirely optional and Vert.x does not force you to create your applications in this way if you don’t want to..

The model does not claim to be a strict actor-model implementation, but it does share similarities especially with respect to concurrency, scaling and deployment.

To use this model, you write your code as set of verticles.

Verticles are chunks of code that get deployed and run by Vert.x. Verticles can be written in any of the languages that Vert.x supports and a single application can include verticles written in multiple languages.

You can think of a verticle as a bit like an actor in the Actor Model.

An application would typically be composed of many verticle instances running in the same Vert.x instance at the same time. The different verticle instances communicate with each other by sending messages on the event bus.

### Writing Verticles

JavaScript verticles are implemented either as CommonJS modules or NPM modules.

Important | JavaScript support requires Java 1.8.0_45+. You can check your Java version with `java -version`. Please update if needed.

Here’s an example JavaScript verticle written as a CommonJS module.

JavaScript verticles will have the following globals pre-set as a convenience:

- vertx - A reference to the Vertx object

- console A reference to the console object

    // the vertx and console objects can be used directly as:
    vertx.setPeriodic(1000, function() {
      console.log('Timer has fired');
    });

    // Optional - called when verticle is undeployed
    exports.vertxStop = function() {
      console.log('stop called');
    }

When the verticle is deployed the body of the script will be executed.

Optionally a `vertxStop` callback can be defined to be notified when the verticle is undeployed. Notice that such a registration is made using `exports.vertxStop = function() { …​ }`.

### The console

In addition to the `vertx` object, JavaScript verticles have access to a `console` object that let them print messages. The messages are printed on Java `System.out` and `System.err`:

    console.log("hello"); // Print on System.out
    console.warn("warning, something wrong is happening"); // Print on System.err
    console.error("something terrible happened"); // Print on System.err

### Defining a start method

By default, when your verticle is deployed, vert.x execute the body of the javascript file. However, you can define a start method as follows:

    exports.vertxStart = function() {
     console.log("This is my verticle and it is starting...");
    }

As for `vertStop` seen above, the method is defined on the `exports` object.

Important | Even with a `vertxStart` method defined, vert.x executes the body of the file.

### Asynchronous Verticle start and stop

Sometimes you want to do something in your verticle start-up which takes some time and you don’t want the verticle to be considered deployed until that happens. For example you might want to deploy other verticles in the start method.

You can’t block waiting for the other verticles to deploy in your start method as that would break the Golden Rule.

So how can you do this?

The way to do it is to implement the asynchronous start method. This version of the method takes a `Future` object as a parameter. When the method returns the verticle will not be considered deployed yet. Some time later, after you’ve done everything you need to do (e.g. start other verticles), you can call complete on the Future (or fail) to signal that you’re done.

Here’s an example:

    exports.vertxStartAsync = function(startFuture) {
     vertx.deployVerticle("verticle.js", function(res, err) {
       if (err) {
         startFuture.fail();
       } else {
         startFuture.complete();
       }
     });
    };

Similarly, there is an asynchronous version of the `stop` method too. You use this if you want to do some verticle cleanup that takes some time.

    exports.vertxStopAsync = function(stopFuture) {
     obj.doSomethingThatTakesTimes(function(r, err) {
       if (err) {
         stopFuture.fail();
      } else {
         stopFuture.complete();
      }
     });
    };

INFO: You don’t need to manually undeploy child verticles started by a verticle, in the verticle’s stop method. Vert.x will automatically undeploy any child verticles when the parent is undeployed.

### Verticle Types

There are three different types of verticles:

Standard Verticles

  These are the most common and useful type - they are always executed using an event loop thread. We’ll discuss this more in the next section.

Worker Verticles

  These run using a thread from the worker pool. An instance is never executed concurrently by more than one thread.

Multi-threaded worker verticles

  These run using a thread from the worker pool. An instance can be executed concurrently by more than one thread.

### Standard verticles

Standard verticles are assigned an event loop thread when they are created and the start method is called with that event loop. When you call any other methods that takes a handler on a core API from an event loop then Vert.x will guarantee that those handlers, when called, will be executed on the same event loop.

This means we can guarantee that all the code in your verticle instance is always executed on the same event loop (as long as you don’t create your own threads and call it!).

This means you can write all the code in your application as single threaded and let Vert.x worrying about the threading and scaling. No more worrying about synchronized and volatile any more, and you also avoid many other cases of race conditions and deadlock so prevalent when doing hand-rolled 'traditional' multi-threaded application development.

### Worker verticles

A worker verticle is just like a standard verticle but it’s executed not using an event loop, but using a thread from the Vert.x worker thread pool.

Worker verticles are designed for calling blocking code, as they won’t block any event loops.

If you don’t want to use a worker verticle to run blocking code, you can also run inline blocking code directly while on an event loop.

If you want to deploy a verticle as a worker verticle you do that with `worker`.

    var options = {
      "worker" : true
    };
    vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", options);

Worker verticle instances are never executed concurrently by Vert.x by more than one thread, but can executed by different threads at different times.

#### Multi-threaded worker verticles

A multi-threaded worker verticle is just like a normal worker verticle but it can be executed concurrently by different threads.

Warning | Multi-threaded worker verticles are an advanced feature and most applications will have no need for them. Because of the concurrency in these verticles you have to be very careful to keep the verticle in a consistent state using standard Java techniques for multi-threaded programming.

### Deploying verticles programmatically

You can deploy a verticle using one of the `deployVerticle` method, specifying a verticle name or you can pass in a verticle instance you have already created yourself.

Note | Deploying Verticle instances is Java only.

    var myVerticle = new (Java.type("examples.CoreExamples.MyVerticle"))();
    vertx.deployVerticle(myVerticle);

You can also deploy verticles by specifying the verticle name.

The verticle name is used to look up the specific `VerticleFactory` that will be used to instantiate the actual verticle instance(s).

Different verticle factories are available for instantiating verticles in different languages and for various other reasons such as loading services and getting verticles from Maven at run-time.

This allows you to deploy verticles written in any language from any other language that Vert.x supports.

Here’s an example of deploying some different types of verticles:

    // Deploy a Java verticle - the name is the fully qualified class name of the verticle class
    vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle");

    // Deploy a JavaScript verticle
    vertx.deployVerticle("verticles/myverticle.js");

    // Deploy a Ruby verticle verticle
    vertx.deployVerticle("verticles/my_verticle.rb");

### Rules for mapping a verticle name to a verticle factory

When deploying verticle(s) using a name, the name is used to select the actual verticle factory that will instantiate the verticle(s).

Verticle names can have a prefix - which is a string followed by a colon, which if present will be used to look-up the factory, e.g.

    js:foo.js // Use the JavaScript verticle factory
    groovy:com.mycompany.SomeGroovyCompiledVerticle // Use the Groovy verticle factory
    service:com.mycompany:myorderservice // Uses the service verticle factory

If no prefix is present, Vert.x will look for a suffix and use that to lookup the factory, e.g.

    foo.js // Will also use the JavaScript verticle factory
    SomeScript.groovy // Will use the Groovy verticle factory

If no prefix or suffix is present, Vert.x will assume it’s a Java fully qualified class name (FQCN) and try and instantiate that.

### How are Verticle Factories located?

Most Verticle factories are loaded from the classpath and registered at Vert.x startup.

You can also programmatically register and unregister verticle factories using `registerVerticleFactory` and `unregisterVerticleFactory` if you wish.

### Waiting for deployment to complete

Verticle deployment is asynchronous and may complete some time after the call to deploy has returned.

If you want to be notified when deployment is complete you can deploy specifying a completion handler:

    vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", function (res, res_err) {
      if (res_err == null) {
        console.log("Deployment id is: " + res);
      } else {
        console.log("Deployment failed!");
      }
    });

The completion handler will be passed a result containing the deployment ID string, if deployment succeeded.

This deployment ID can be used later if you want to undeploy the deployment.

### Undeploying verticle deployments

Deployments can be undeployed with `undeploy`.

Un-deployment is itself asynchronous so if you want to be notified when un-deployment is complete you can deploy specifying a completion handler:

    vertx.undeploy(deploymentID, function (res, res_err) {
      if (res_err == null) {
        console.log("Undeployed ok");
      } else {
        console.log("Undeploy failed!");
      }
    });

### Specifying number of verticle instances

When deploying a verticle using a verticle name, you can specify the number of verticle instances that you want to deploy:

    var options = {
      "instances" : 16
    };
    vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", options);

This is useful for scaling easily across multiple cores. For example you might have a web-server verticle to deploy and multiple cores on your machine, so you want to deploy multiple instances to take utilise all the cores.

### Passing configuration to a verticle

Configuration in the form of JSON can be passed to a verticle at deployment time:

    var config = {
     "name" : "tim",
     "directory" : "/blah"
    };
    var options = {
     "config" : config
    };
    vertx.deployVerticle("MyOrderProcessorVerticle.js", options);

This configuration is then available via the `Context` object.

The configuration is returned as a JavaScript object so you can retrieve data as follows:

    // The context object can be retrieve either using:
    console.log("Configuration: " + Vertx.currentContext().config().name);
    // or:
    console.log("Configuration: " + vertx.getOrCreateContext().config().name);

Note | The access to the `context` object is made either using `Vertx.currentContext` or using `getOrCreateContext`.

### Accessing environment variables in a Verticle

Environment variables and system properties are accessible from a verticle using the Java API:

    console.log(java.lang.System.getProperty("foo"));
    console.log(java.lang.System.getenv("HOME"));

### Verticle Isolation Groups

By default, Vert.x has a flat classpath. I.e, when Vert.x deploys verticles it does so with the current classloader - it doesn’t create a new one. In the majority of cases this is the simplest, clearest and sanest thing to do.

However, in some cases you may want to deploy a verticle so the classes of that verticle are isolated from others in your application.

This might be the case, for example, if you want to deploy two different versions of a verticle with the same class name in the same Vert.x instance, or if you have two different verticles which use different versions of the same jar library.

When using an isolation group you provide a list of the class names that you want isolated using `isolatedClasses`- an entry can be a fully qualified classname such as `com.mycompany.myproject.engine.MyClass` or it can be a wildcard which will match any classes in a package and any sub-packages, e.g. `com.mycompany.myproject.*` would match any classes in the package `com.mycompany.myproject` or any sub-packages.

Please note that only the classes that match will be isolated - any other classes will be loaded by the current class loader.

Extra classpath entries can also be provided with `extraClasspath` so if you want to load classes or resources that aren’t already present on the main classpath you can add this.

Warning | Use this feature with caution. Class-loaders can be a can of worms, and can make debugging difficult, amongst other things.

Here’s an example of using an isolation group to isolate a verticle deployment.

    var options = {
      "isolationGroup" : "mygroup"
    };
    options.isolatedClasses = ["com.mycompany.myverticle.*", "com.mycompany.somepkg.SomeClass", "org.somelibrary.*"];
    vertx.deployVerticle("com.mycompany.myverticle.VerticleClass", options);

### High Availability

Verticles can be deployed with High Availability (HA) enabled. In that context, when a verticle is deployed on a vert.x instance that dies abruptly, the verticle is redeployed on another vert.x instance from the cluster.

To run an verticle with the high availability enabled, just append the `-ha` switch:

    vertx run my-verticle.js -ha

When enabling high availability, no need to add `-cluster`.

More details about the high availability feature and configuration in the High Availability and Fail-Over section.

### Running Verticles from the command line

You can use Vert.x directly in your Maven or Gradle projects in the normal way by adding a dependency to the Vert.x core library and hacking from there.

However you can also run Vert.x verticles directly from the command line if you wish.

To do this you need to download and install a Vert.x distribution, and add the `bin` directory of the installation to your `PATH` environment variable. Also make sure you have a Java 8 JDK on your `PATH`.

Note | The JDK is required to support on the fly compilation of Java code.

You can now run verticles by using the `vertx run` command. Here are some examples:

    # Run a JavaScript verticle
    vertx run my_verticle.js

    # Run a Ruby verticle
    vertx run a_n_other_verticle.rb

    # Run a Groovy script verticle, clustered
    vertx run FooVerticle.groovy -cluster

You can even run Java source verticles without compiling them first!

    vertx run SomeJavaSourceFile.java

Vert.x will compile the Java source file on the fly before running it. This is really useful for quickly prototyping verticles and great for demos. No need to set-up a Maven or Gradle build first to get going!

For full information on the various options available when executing `vertx` on the command line, type `vertx` at the command line.

### Causing Vert.x to exit

Threads maintained by Vert.x instances are not daemon threads so they will prevent the JVM from exiting.

If you are embedding Vert.x and you have finished with it, you can call `close` to close it down.

This will shut-down all internal thread pools and close other resources, and will allow the JVM to exit.

### The Context object

When Vert.x provides an event to a handler or calls the start or stop methods of a `Verticle`, the execution is associated with a `Context`. Usually a context is an event-loop context and is tied to a specific event loop thread. So executions for that context always occur on that exact same event loop thread. In the case of worker verticles and running inline blocking code a worker context will be associated with the execution which will use a thread from the worker thread pool.

To retrieve the context, use the `getOrCreateContext` method:

    var context = vertx.getOrCreateContext();

If the current thread has a context associated with it, it reuses the context object. If not a new instance of context is created. You can test the type of context you have retrieved:

    var Context = require("vertx-js/context");
    var context = vertx.getOrCreateContext();
    if (context.isEventLoopContext()) {
      console.log("Context attached to Event Loop");
    } else if (context.isWorkerContext()) {
      console.log("Context attached to Worker Thread");
    } else if (context.isMultiThreadedWorkerContext()) {
      console.log("Context attached to Worker Thread - multi threaded worker");
    } else if (!Context.isOnVertxThread()) {
      console.log("Context not attached to a thread managed by vert.x");
    }

When you have retrieved the context object, you can run code in this context asynchronously. In other words, you submit a task that will be eventually run in the same context, but later:

    vertx.getOrCreateContext().runOnContext(function (v) {
      console.log("This will be executed asynchronously in the same context");
    });

When several handlers run in the same context, they may want to share data. The context object offers methods to store and retrieve data shared in the context. For instance, it lets you pass data to some action run with `runOnContext`:

    var context = vertx.getOrCreateContext();
    context.put("data", "hello");
    context.runOnContext(function (v) {
      var hello = context.get("data");
    });

The context object also let you access verticle configuration using the `config` method. Check the Passing configuration to a verticle section for more details about this configuration.

### Executing periodic and delayed actions

It’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 Timers

A one shot timer calls an event handler after a certain delay, expressed in milliseconds.

To set a timer to fire once you use `setTimer` method passing in the delay and a handler

    var timerID = vertx.setTimer(1000, function (id) {
      console.log("And one second later this is printed");
    });

    console.log("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 Timers

You can also set a timer to fire periodically by using `setPeriodic`.

There will be an initial delay equal to the period.

The return value of `setPeriodic` is a unique timer id (long). This can be later used if the timer needs to be cancelled.

The argument passed into the timer event handler is also the unique timer id:

Keep in mind that the timer will fire on a periodic basis. If your periodic treatment takes a long amount of time to proceed, your timer events could run continuously or even worse : stack up.

In this case, you should consider using `setTimer` instead. Once your treatment has finished, you can set the next timer.

    var timerID = vertx.setPeriodic(1000, function (id) {
      console.log("And every second this is printed");
    });

    console.log("First this is printed");

#### Cancelling timers

To cancel a periodic timer, call `cancelTimer` specifying the timer id. For example:

    vertx.cancelTimer(timerID);

#### Automatic clean-up in verticles

If you’re creating timers from inside verticles, those timers will be automatically closed when the verticle is undeployed.

## The Event Bus

The `event bus` is the nervous system of Vert.x.

There is a single event bus instance for every Vert.x instance and it is obtained using the method `eventBus`.

The event bus allows different parts of your application to communicate with each other irrespective of what language they are written in, and whether they’re in the same Vert.x instance, or in a different Vert.x instance.

It can even be bridged to allow client side JavaScript running in a browser to communicate on the same event bus.

The event bus forms a distributed peer-to-peer messaging system spanning multiple server nodes and multiple browsers.

The event bus supports publish/subscribe, point to point, and request-response messaging.

The event bus API is very simple. It basically involves registering handlers, unregistering handlers and sending and publishing messages.

First some theory:

### The Theory

#### Addressing

Messages are sent on the event bus to an address.

Vert.x doesn’t bother with any fancy addressing schemes. In Vert.x an address is simply a string. Any string is valid. However it is wise to use some kind of scheme, e.g. using periods to demarcate a namespace.

Some examples of valid addresses are europe.news.feed1, acme.games.pacman, sausages, and X.

#### Handlers

Messages are received in handlers. You register a handler at an address.

Many different handlers can be registered at the same address.

A single handler can be registered at many different addresses.

#### Publish / subscribe messaging

The event bus supports publishing messages.

Messages are published to an address. Publishing means delivering the message to all handlers that are registered at that address.

This is the familiar publish/subscribe messaging pattern.

#### Point to point and Request-Response messaging

The event bus also supports point to point messaging.

Messages are sent to an address. Vert.x will then route it to just one of the handlers registered at that address.

If there is more than one handler registered at the address, one will be chosen using a non-strict round-robin algorithm.

With point to point messaging, an optional reply handler can be specified when sending the message.

When a message is received by a recipient, and has been handled, the recipient can optionally decide to reply to the message. If they do so the reply handler will be called.

When the reply is received back at the sender, it too can be replied to. This can be repeated ad-infinitum, and allows a dialog to be set-up between two different verticles.

This is a common messaging pattern called the request-response pattern.

#### Best-effort delivery

Vert.x does it’s best to deliver messages and won’t consciously throw them away. This is called best-effort delivery.

However, in case of failure of all or parts of the event bus, there is a possibility messages will be lost.

If your application cares about lost messages, you should code your handlers to be idempotent, and your senders to retry after recovery.

#### Types of messages

Out of the box Vert.x allows any primitive/simple type, String, or `buffers` to be sent as messages.

However it’s a convention and common practice in Vert.x to send messages as JSON

JSON is very easy to create, read and parse in all the languages that Vert.x supports so it has become a kind of lingua franca for Vert.x.

However you are not forced to use JSON if you don’t want to.

The event bus is very flexible and also supports sending arbitrary objects over the event bus. You do this by defining a `codec` for the objects you want to send.

### The Event Bus API

Let’s jump into the API

#### Getting the event bus

You get a reference to the event bus as follows:

    var eb = vertx.eventBus();

There is a single instance of the event bus per Vert.x instance.

#### Registering Handlers

This simplest way to register a handler is using `consumer`. Here’s an example:

    var eb = vertx.eventBus();

    eb.consumer("news.uk.sport", function (message) {
      console.log("I have received a message: " + message.body());
    });

When a message arrives for your handler, your handler will be called, passing in the `message`.

The object returned from call to consumer() is an instance of `MessageConsumer`

This object can subsequently be used to unregister the handler, or use the handler as a stream.

Alternatively you can use `consumer` to to return a MessageConsumer with no handler set, and then set the handler on that. For example:

    var eb = vertx.eventBus();

    var consumer = eb.consumer("news.uk.sport");
    consumer.handler(function (message) {
      console.log("I have received a message: " + message.body());
    });

When registering a handler on a clustered event bus, it can take some time for the registration to reach all nodes of the cluster.

If you want to be notified when this has completed, you can register a `completion handler` on the MessageConsumer object.

    consumer.completionHandler(function (res, res_err) {
      if (res_err == null) {
        console.log("The handler registration has reached all nodes");
      } else {
        console.log("Registration failed!");
      }
    });

#### Un-registering Handlers

To unregister a handler, call `unregister`.

If you are on a clustered event bus, un-registering can take some time to propagate across the nodes, if you want to be notified when this is complete use `unregister`.

    consumer.unregister(function (res, res_err) {
      if (res_err == null) {
        console.log("The handler un-registration has reached all nodes");
      } else {
        console.log("Un-registration failed!");
      }
    });

#### Publishing messages

Publishing a message is simple. Just use `publish` specifying the address to publish it to.

    eventBus.publish("news.uk.sport", "Yay! Someone kicked a ball");

That message will then be delivered to all handlers registered against the address news.uk.sport.

#### Sending messages

Sending a message will result in only one handler registered at the address receiving the message. This is the point to point messaging pattern. The handler is chosen in a non-strict round-robin fashion.

You can send a message with `send`

    eventBus.send("news.uk.sport", "Yay! Someone kicked a ball");

#### Setting headers on messages

Messages sent over the event bus can also contain headers.

This can be specified by providing a `headers` object inside the `DeliveryOptions` object when sending or publishing:

    var options = {
     headers: {
       "some-header" : "some-value"
     }
    }
    vertx.eventBus().send("news.uk.sport", "Yay! Someone kicked a ball", options);

On the other side, the consumer can retrieve the message header as follows:

    vertx.eventBus().consumer("news.uk.sport", function(e) {
     console.log(e.headers().get("some-header"));
    });

#### Message ordering

Vert.x will deliver messages to any particular handler in the same order they were sent from any particular sender.

#### The Message object

The object you receive in a message handler is a `Message`.

The `body` of the message corresponds to the object that was sent or published.

The headers of the message are available with `headers`.

#### Replying to messages

Sometimes after you send a message you want to receive a reply from the recipient. This is known as the request-response pattern.

To do this you can specify a reply handler when sending the message.

When the receiver receives the message they can reply to it by calling `reply`.

When this happens it causes a reply to be sent back to the sender and the reply handler is invoked with the reply.

An example will make this clear:

The receiver:

    var consumer = eventBus.consumer("news.uk.sport");
    consumer.handler(function (message) {
      console.log("I have received a message: " + message.body());
      message.reply("how interesting!");
    });

The sender:

    eventBus.send("news.uk.sport", "Yay! Someone kicked a ball across a patch of grass", function (ar, ar_err) {
      if (ar_err == null) {
        console.log("Received reply: " + ar.body());
      }
    });

The replies themselves can also be replied to so you can create a dialog between two different parties consisting of multiple rounds.

#### Sending with timeouts

When sending a message with a reply handler you can specify a timeout in the `DeliveryOptions`.

If a reply is not received within that time, the reply handler will be called with a failure.

The default timeout is 30 seconds.

#### Send Failures

Message sends can fail for other reasons, including:

- There are no handlers available to send the message to

- The recipient has explicitly failed the message using `fail`

In all cases the reply handler will be called with the specific failure.

#### Message Codecs

Message codecs are available exclusively with the Java API.

#### Clustered Event Bus

The event bus doesn’t just exist in a single Vert.x instance. By clustering different Vert.x instances together on your network they can form a single, distributed, event bus.

#### Clustering programmatically

If you’re creating your Vert.x instance programmatically you get a clustered event bus by configuring the Vert.x instance as clustered;

    var Vertx = require("vertx-js/vertx");
    var options = {
    };
    Vertx.clusteredVertx(options, function (res, res_err) {
      if (res_err == null) {
        var vertx = res;
        var eventBus = vertx.eventBus();
        console.log("We now have a clustered event bus: " + eventBus);
      } else {
        console.log("Failed: " + res_err);
      }
    });

You should also make sure you have a `ClusterManager` implementation on your classpath, for example the default `HazelcastClusterManager`.

#### Clustering on the command line

You can run Vert.x clustered on the command line with

    vertx run my-verticle.js -cluster

### Automatic clean-up in verticles

If you’re registering event bus handlers from inside verticles, those handlers will be automatically unregistered when the verticle is undeployed.

## Buffers

Most data is shuffled around inside Vert.x using buffers.

A buffer is a sequence of zero or more bytes that can read from or written to and which expands automatically as necessary to accommodate any bytes written to it. You can perhaps think of a buffer as smart byte array.

### Creating buffers

Buffers can create by using one of the static `Buffer.buffer` methods.

Buffers can be initialised from strings or byte arrays, or empty buffers can be created.

Here are some examples of creating buffers:

Create a new empty buffer:

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer();

Create a buffer from a String. The String will be encoded in the buffer using UTF-8.

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer("some string");

Create a buffer from a String: The String will be encoded using the specified encoding, e.g:

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer("some string", "UTF-16");

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.

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer(10000);

### Writing to a Buffer

There 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 `IndexOutOfBoundsException` with a buffer.

#### Appending to a Buffer

To append to a buffer, you use the `appendXXX` methods. Append methods exist for appending various different types.

The return value of the `appendXXX` methods is the buffer itself, so these can be chained:

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer();

    buff.appendInt(123).appendString("hello\n");

    socket.write(buff);

#### Random access buffer writes

You can also write into the buffer at a specific index, by using the `setXXX` methods. Set methods exist for various different data types. All the set methods take an index as the first argument - this represents the position in the buffer where to start writing the data.

The buffer will always expand as necessary to accommodate the data.

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer();

    buff.setInt(1000, 123);
    buff.setString(0, "hello");

### Reading from a Buffer

Data is read from a buffer using the `getXXX` methods. Get methods exist for various datatypes. The first argument to these methods is an index in the buffer from where to get the data.

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer();
    for (var i = 0; i < buff.length(); 4) {
      console.log("int value at " + i + " is " + buff.getInt(i));
    }

### Working with unsigned numbers

Unsigned numbers can be read from or appended/set to a buffer with the `getUnsignedXXX`, `appendUnsignedXXX` and `setUnsignedXXX` methods. This is useful when implementing a codec for a network protocol optimized to minimize bandwidth consumption.

In the following example, value 200 is set at specified position with just one byte:

    var Buffer = require("vertx-js/buffer");
    var buff = Buffer.buffer(128);
    var pos = 15;
    buff.setUnsignedByte(pos, 200);
    console.log(buff.getUnsignedByte(pos));

The console shows '200'.

### Buffer length

Use `length` to obtain the length of the buffer. The length of a buffer is the index of the byte in the buffer with the largest index + 1.

### Copying buffers

Use `copy` to make a copy of the buffer

### Slicing buffers

A sliced buffer is a new buffer which backs onto the original buffer, i.e. it does not copy the underlying data. Use `slice` to create a sliced buffers

### Buffer re-use

After writing a buffer to a socket or other similar place, they cannot be re-used.

## Writing TCP servers and clients

Vert.x allows you to easily write non blocking TCP clients and servers.

### Creating a TCP server

The simplest way to create a TCP server, using all default options is as follows:

    var server = vertx.createNetServer();

### Configuring a TCP server

If you don’t want the default, a server can be configured by passing in a `NetServerOptions` instance when creating it:

    var options = {
      "port" : 4321
    };
    var server = vertx.createNetServer(options);

### Start the Server Listening

To tell the server to listen for incoming requests you use one of the `listen` alternatives.

To tell the server to listen at the host and port as specified in the options:

    var server = vertx.createNetServer();
    server.listen();

Or to specify the host and port in the call to listen, ignoring what is configured in the options:

    var server = vertx.createNetServer();
    server.listen(1234, "localhost");

The default host is `0.0.0.0` which means 'listen on all available addresses' and the default port is `0`, which is a special value that instructs the server to find a random unused local port and use that.

The actual bind is asynchronous so the server might not actually be listening until some time after the call to listen has returned.

If you want to be notified when the server is actually listening you can provide a handler to the `listen` call. For example:

    var server = vertx.createNetServer();
    server.listen(1234, "localhost", function (res, res_err) {
      if (res_err == null) {
        console.log("Server is now listening!");
      } else {
        console.log("Failed to bind!");
      }
    });

### Listening on a random port

If `0` is used as the listening port, the server will find an unused random port to listen on.

To find out the real port the server is listening on you can call `actualPort`.

    var server = vertx.createNetServer();
    server.listen(0, "localhost", function (res, res_err) {
      if (res_err == null) {
        console.log("Server is now listening on actual port: " + server.actualPort());
      } else {
        console.log("Failed to bind!");
      }
    });

### Getting notified of incoming connections

To be notified when a connection is made you need to set a `connectHandler`:

    var server = vertx.createNetServer();
    server.connectHandler(function (socket) {
      // Handle the connection in here
    });

When a connection is made the handler will be called with an instance of `NetSocket`.

This is a socket-like interface to the actual connection, and allows you to read and write data as well as do various other things like close the socket.

### Reading data from the socket

To read data from the socket you set the `handler` on the socket.

This handler will be called with an instance of `Buffer` every time data is received on the socket.

    var server = vertx.createNetServer();
    server.connectHandler(function (socket) {
      socket.handler(function (buffer) {
        console.log("I received some bytes: " + buffer.length());
      });
    });

### Writing data to a socket

You write to a socket using one of `write`.

    var Buffer = require("vertx-js/buffer");

    // Write a buffer
    var buffer = Buffer.buffer().appendFloat(12.34).appendInt(123);
    socket.write(buffer);

    // Write a string in UTF-8 encoding
    socket.write("some data");

    // Write a string using the specified encoding
    socket.write("some data", "UTF-16");

Write operations are asynchronous and may not occur until some time after the call to write has returned.

### Closed handler

If you want to be notified when a socket is closed, you can set a `closeHandler` on it:

    socket.closeHandler(function (v) {
      console.log("The socket has been closed");
    });

### Handling exceptions

You can set an `exceptionHandler` to receive any exceptions that happen on the socket.

### Event bus write handler

Every socket automatically registers a handler on the event bus, and when any buffers are received in this handler, it writes them to itself.

This enables you to write data to a socket which is potentially in a completely different verticle or even in a different Vert.x instance by sending the buffer to the address of that handler.

The address of the handler is given by `writeHandlerID`

### Local and remote addresses

The local address of a `NetSocket` can be retrieved using `localAddress`.

The remote address, (i.e. the address of the other end of the connection) of a `NetSocket` can be retrieved using `remoteAddress`.

### Sending files

Files can be written to the socket directly using `sendFile`. This can be a very efficient way to send files, as it can be handled by the OS kernel directly where supported by the operating system.

    socket.sendFile("myfile.dat");

### Streaming sockets

Instances of `NetSocket` are also `ReadStream` and `WriteStream` instances so they can be used to pump data to or from other read and write streams.

See the chapter on streams and pumps for more information.

### Upgrading connections to SSL/TLS

A non SSL/TLS connection can be upgraded to SSL/TLS using `upgradeToSsl`.

The server or client must be configured for SSL/TLS for this to work correctly. Please see the chapter on SSL/TLS for more information.

### Closing a TCP Server

Call `close` to close the server. Closing the server closes any open connections and releases all server resources.

The close is actually asynchronous and might not complete until some time after the call has returned. If you want to be notified when the actual close has completed then you can pass in a handler.

This handler will then be called when the close has fully completed.

    server.close(function (res, res_err) {
      if (res_err == null) {
        console.log("Server is now closed");
      } else {
        console.log("close failed");
      }
    });

### Automatic clean-up in verticles

If you’re creating TCP servers and clients from inside verticles, those servers and clients will be automatically closed when the verticle is undeployed.

### Scaling - sharing TCP servers

The handlers of any TCP server are always executed on the same event loop thread.

This means that if you are running on a server with a lot of cores, and you only have this one instance deployed then you will have at most one core utilised on your server.

In order to utilise more cores of your server you will need to deploy more instances of the server.

You can instantiate more instances programmatically in your code:

    // Create a few instances so we can utilise cores

    for (var i = 0; i < 10; i++) {
      var server = vertx.createNetServer();
      server.connectHandler(function (socket) {
        socket.handler(function (buffer) {
          // Just echo back the data
          socket.write(buffer);
        });
      });
      server.listen(1234, "localhost");
    }

or, if you are using verticles you can simply deploy more instances of your server verticle by using the `-instances` option on the command line:

    vertx run com.mycompany.MyVerticle -instances 10

or when programmatically deploying your verticle

    var options = {
      "instances" : 10
    };
    vertx.deployVerticle("com.mycompany.MyVerticle", options);

Once you do this you will find the echo server works functionally identically to before, but all your cores on your server can be utilised and more work can be handled.

At this point you might be asking yourself 'How can you have more than one server listening on the same host and port? Surely you will get port conflicts as soon as you try and deploy more than one instance?'

Vert.x does a little magic here.*

When you deploy another server on the same host and port as an existing server it doesn’t actually try and create a new server listening on the same host/port.

Instead it internally maintains just a single server, and, as incoming connections arrive it distributes them in a round-robin fashion to any of the connect handlers.

Consequently Vert.x TCP servers can scale over available cores while each instance remains single threaded.

### Creating a TCP client

The simplest way to create a TCP client, using all default options is as follows:

    var client = vertx.createNetClient();

### Configuring a TCP client

If you don’t want the default, a client can be configured by passing in a `NetClientOptions` instance when creating it:

    var options = {
      "connectTimeout" : 10000
    };
    var client = vertx.createNetClient(options);

### Making connections

To make a connection to a server you use `connect`, specifying the port and host of the server and a handler that will be called with a result containing the `NetSocket` when connection is successful or with a failure if connection failed.

    var options = {
      "connectTimeout" : 10000
    };
    var client = vertx.createNetClient(options);
    client.connect(4321, "localhost", function (res, res_err) {
      if (res_err == null) {
        console.log("Connected!");
        var socket = res;
      } else {
        console.log("Failed to connect: " + res_err.getMessage());
      }
    });

### Configuring connection attempts

A client can be configured to automatically retry connecting to the server in the event that it cannot connect. This is configured with `reconnectInterval` and `reconnectAttempts`.

Note | Currently Vert.x will not attempt to reconnect if a connection fails, reconnect attempts and interval only apply to creating initial connections.

    var options = {
    };
    options.reconnectAttempts = 10.reconnectInterval = 500;

    var client = vertx.createNetClient(options);

By default, multiple connection attempts are disabled.

### Configuring servers and clients to work with SSL/TLS

TCP clients and servers can be configured to use Transport Layer Security - earlier versions of TLS were known as SSL.

The APIs of the servers and clients are identical whether or not SSL/TLS is used, and it’s enabled by configuring the `NetClientOptions` or `NetServerOptions` instances used to create the servers or clients.

#### Enabling SSL/TLS on the server

SSL/TLS is enabled with `ssl`.

By default it is disabled.

#### Specifying key/certificate for the server

SSL/TLS servers usually provide certificates to clients in order verify their identity to clients.

Certificates/keys can be configured for servers in several ways:

The first method is by specifying the location of a Java key-store which contains the certificate and private key.

Java key stores can be managed with the keytool utility which ships with the JDK.

The password for the key store should also be provided:

    var options = {
      "ssl" : true,
      "keyStoreOptions" : {
        "path" : "/path/to/your/server-keystore.jks",
        "password" : "password-of-your-keystore"
      }
    };
    var server = vertx.createNetServer(options);

Alternatively you can read the key store yourself as a buffer and provide that directly:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-keystore.jks");
    var jksOptions = {
      "value" : myKeyStoreAsABuffer,
      "password" : "password-of-your-keystore"
    };
    var options = {
      "ssl" : true,
      "keyStoreOptions" : jksOptions
    };
    var server = vertx.createNetServer(options);

Key/certificate in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS key stores:

    var options = {
      "ssl" : true,
      "pfxKeyCertOptions" : {
        "path" : "/path/to/your/server-keystore.pfx",
        "password" : "password-of-your-keystore"
      }
    };
    var server = vertx.createNetServer(options);

Buffer configuration is also supported:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-keystore.pfx");
    var pfxOptions = {
      "value" : myKeyStoreAsABuffer,
      "password" : "password-of-your-keystore"
    };
    var options = {
      "ssl" : true,
      "pfxKeyCertOptions" : pfxOptions
    };
    var server = vertx.createNetServer(options);

Another way of providing server private key and certificate separately using `.pem` files.

    var options = {
      "ssl" : true,
      "pemKeyCertOptions" : {
        "keyPath" : "/path/to/your/server-key.pem",
        "certPath" : "/path/to/your/server-cert.pem"
      }
    };
    var server = vertx.createNetServer(options);

Buffer configuration is also supported:

    var myKeyAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-key.pem");
    var myCertAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-cert.pem");
    var pemOptions = {
      "keyValue" : myKeyAsABuffer,
      "certValue" : myCertAsABuffer
    };
    var options = {
      "ssl" : true,
      "pemKeyCertOptions" : pemOptions
    };
    var server = vertx.createNetServer(options);

Keep in mind that pem configuration, the private key is not crypted.

#### Specifying trust for the server

SSL/TLS servers can use a certificate authority in order to verify the identity of the clients.

Certificate authorities can be configured for servers in several ways:

Java trust stores can be managed with the keytool utility which ships with the JDK.

The password for the trust store should also be provided:

    var options = {
      "ssl" : true,
      "clientAuth" : 'REQUIRED',
      "trustStoreOptions" : {
        "path" : "/path/to/your/truststore.jks",
        "password" : "password-of-your-truststore"
      }
    };
    var server = vertx.createNetServer(options);

Alternatively you can read the trust store yourself as a buffer and provide that directly:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.jks");
    var options = {
      "ssl" : true,
      "clientAuth" : 'REQUIRED',
      "trustStoreOptions" : {
        "value" : myTrustStoreAsABuffer,
        "password" : "password-of-your-truststore"
      }
    };
    var server = vertx.createNetServer(options);

Certificate authority in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS trust stores:

    var options = {
      "ssl" : true,
      "clientAuth" : 'REQUIRED',
      "pfxTrustOptions" : {
        "path" : "/path/to/your/truststore.pfx",
        "password" : "password-of-your-truststore"
      }
    };
    var server = vertx.createNetServer(options);

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.pfx");
    var options = {
      "ssl" : true,
      "clientAuth" : 'REQUIRED',
      "pfxTrustOptions" : {
        "value" : myTrustStoreAsABuffer,
        "password" : "password-of-your-truststore"
      }
    };
    var server = vertx.createNetServer(options);

Another way of providing server certificate authority using a list `.pem` files.

    var options = {
      "ssl" : true,
      "clientAuth" : 'REQUIRED',
      "pemTrustOptions" : {
        "certPaths" : [
          "/path/to/your/server-ca.pem"
        ]
      }
    };
    var server = vertx.createNetServer(options);

Buffer configuration is also supported:

    var myCaAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-ca.pfx");
    var options = {
      "ssl" : true,
      "clientAuth" : 'REQUIRED',
      "pemTrustOptions" : {
        "certValues" : [
          myCaAsABuffer
        ]
      }
    };
    var server = vertx.createNetServer(options);

#### Enabling SSL/TLS on the client

Net Clients can also be easily configured to use SSL. They have the exact same API when using SSL as when using standard sockets.

To enable SSL on a NetClient the function setSSL(true) is called.

#### Client trust configuration

If the `trustALl` is set to true on the client, then the client will trust all server certificates. The connection will still be encrypted but this mode is vulnerable to 'man in the middle' attacks. I.e. you can’t be sure who you are connecting to. Use this with caution. Default value is false.

    var options = {
      "ssl" : true,
      "trustAll" : true
    };
    var client = vertx.createNetClient(options);

If `trustAll` is not set then a client trust store must be configured and should contain the certificates of the servers that the client trusts.

Likewise server configuration, the client trust can be configured in several ways:

The first method is by specifying the location of a Java trust-store which contains the certificate authority.

It is just a standard Java key store, the same as the key stores on the server side. The client trust store location is set by using the function `path` on the `jks options`. If a server presents a certificate during connection which is not in the client trust store, the connection attempt will not succeed.

    var options = {
      "ssl" : true,
      "trustStoreOptions" : {
        "path" : "/path/to/your/truststore.jks",
        "password" : "password-of-your-truststore"
      }
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.jks");
    var options = {
      "ssl" : true,
      "trustStoreOptions" : {
        "value" : myTrustStoreAsABuffer,
        "password" : "password-of-your-truststore"
      }
    };
    var client = vertx.createNetClient(options);

Certificate authority in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS trust stores:

    var options = {
      "ssl" : true,
      "pfxTrustOptions" : {
        "path" : "/path/to/your/truststore.pfx",
        "password" : "password-of-your-truststore"
      }
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.pfx");
    var options = {
      "ssl" : true,
      "pfxTrustOptions" : {
        "value" : myTrustStoreAsABuffer,
        "password" : "password-of-your-truststore"
      }
    };
    var client = vertx.createNetClient(options);

Another way of providing server certificate authority using a list `.pem` files.

    var options = {
      "ssl" : true,
      "pemTrustOptions" : {
        "certPaths" : [
          "/path/to/your/ca-cert.pem"
        ]
      }
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/ca-cert.pem");
    var options = {
      "ssl" : true,
      "pemTrustOptions" : {
        "certValues" : [
          myTrustStoreAsABuffer
        ]
      }
    };
    var client = vertx.createNetClient(options);

#### Specifying key/certificate for the client

If the server requires client authentication then the client must present its own certificate to the server when connecting. The client can be configured in several ways:

The first method is by specifying the location of a Java key-store which contains the key and certificate. Again it’s just a regular Java key store. The client keystore location is set by using the function `path` on the `jks options`.

    var options = {
      "ssl" : true,
      "keyStoreOptions" : {
        "path" : "/path/to/your/client-keystore.jks",
        "password" : "password-of-your-keystore"
      }
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-keystore.jks");
    var jksOptions = {
      "value" : myKeyStoreAsABuffer,
      "password" : "password-of-your-keystore"
    };
    var options = {
      "ssl" : true,
      "keyStoreOptions" : jksOptions
    };
    var client = vertx.createNetClient(options);

Key/certificate in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS key stores:

    var options = {
      "ssl" : true,
      "pfxKeyCertOptions" : {
        "path" : "/path/to/your/client-keystore.pfx",
        "password" : "password-of-your-keystore"
      }
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-keystore.pfx");
    var pfxOptions = {
      "value" : myKeyStoreAsABuffer,
      "password" : "password-of-your-keystore"
    };
    var options = {
      "ssl" : true,
      "pfxKeyCertOptions" : pfxOptions
    };
    var client = vertx.createNetClient(options);

Another way of providing server private key and certificate separately using `.pem` files.

    var options = {
      "ssl" : true,
      "pemKeyCertOptions" : {
        "keyPath" : "/path/to/your/client-key.pem",
        "certPath" : "/path/to/your/client-cert.pem"
      }
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myKeyAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-key.pem");
    var myCertAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-cert.pem");
    var pemOptions = {
      "keyValue" : myKeyAsABuffer,
      "certValue" : myCertAsABuffer
    };
    var options = {
      "ssl" : true,
      "pemKeyCertOptions" : pemOptions
    };
    var client = vertx.createNetClient(options);

Keep in mind that pem configuration, the private key is not crypted.

#### Revoking certificate authorities

Trust can be configured to use a certificate revocation list (CRL) for revoked certificates that should no longer be trusted. The `crlPath` configures the crl list to use:

    var options = {
      "ssl" : true,
      "trustStoreOptions" : trustOptions,
      "crlPaths" : [
        "/path/to/your/crl.pem"
      ]
    };
    var client = vertx.createNetClient(options);

Buffer configuration is also supported:

    var myCrlAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/crl.pem");
    var options = {
      "ssl" : true,
      "trustStoreOptions" : trustOptions,
      "crlValues" : [
        myCrlAsABuffer
      ]
    };
    var client = vertx.createNetClient(options);

#### Configuring the Cipher suite

By default, the TLS configuration will uses the Cipher suite of the JVM running Vert.x. This Cipher suite can be configured with a suite of enabled ciphers:

    var options = {
      "ssl" : true,
      "keyStoreOptions" : keyStoreOptions,
      "enabledCipherSuites" : [
        "ECDHE-RSA-AES128-GCM-SHA256",
        "ECDHE-ECDSA-AES128-GCM-SHA256",
        "ECDHE-RSA-AES256-GCM-SHA384",
        "CDHE-ECDSA-AES256-GCM-SHA384"
      ]
    };
    var server = vertx.createNetServer(options);

Cipher suite can be specified on the `NetServerOptions` or `NetClientOptions` configuration.

## Writing HTTP servers and clients

Vert.x allows you to easily write non blocking HTTP clients and servers.

### Creating an HTTP Server

The simplest way to create an HTTP server, using all default options is as follows:

    var server = vertx.createHttpServer();

### Configuring an HTTP server

If you don’t want the default, a server can be configured by passing in a `HttpServerOptions` instance when creating it:

    var options = {
      "maxWebsocketFrameSize" : 1000000
    };

    var server = vertx.createHttpServer(options);

### Start the Server Listening

To tell the server to listen for incoming requests you use one of the `listen` alternatives.

To tell the server to listen at the host and port as specified in the options:

    var server = vertx.createHttpServer();
    server.listen();

Or to specify the host and port in the call to listen, ignoring what is configured in the options:

    var server = vertx.createHttpServer();
    server.listen(8080, "myhost.com");

The default host is `0.0.0.0` which means 'listen on all available addresses' and the default port is `80`.

The actual bind is asynchronous so the server might not actually be listening until some time after the call to listen has returned.

If you want to be notified when the server is actually listening you can provide a handler to the `listen` call. For example:

    var server = vertx.createHttpServer();
    server.listen(8080, "myhost.com", function (res, res_err) {
      if (res_err == null) {
        console.log("Server is now listening!");
      } else {
        console.log("Failed to bind!");
      }
    });

### Getting notified of incoming requests

To be notified when a request arrives you need to set a `requestHandler`:

    var server = vertx.createHttpServer();
    server.requestHandler(function (request) {
      // Handle the request in here
    });

### Handling requests

When a request arrives, the request handler is called passing in an instance of `HttpServerRequest`. This object represents the server side HTTP request.

The handler is called when the headers of the request have been fully read.

If the request contains a body, that body will arrive at the server some time after the request handler has been called.

The server request object allows you to retrieve the `uri`, `path`, `params` and `headers`, amongst other things.

Each server request object is associated with one server response object. You use `response` to get a reference to the `HttpServerResponse` object.

Here’s a simple example of a server handling a request and replying with "hello world" to it.

    vertx.createHttpServer().requestHandler(function (request) {
      request.response().end("Hello world");
    }).listen(8080);

#### Request version

The version of HTTP specified in the request can be retrieved with `version`

#### Request method

Use `method` to retrieve the HTTP method of the request. (i.e. whether it’s GET, POST, PUT, DELETE, HEAD, OPTIONS, etc).

#### Request URI

Use `uri` to retrieve the URI of the request.

Note that this is the actual URI as passed in the HTTP request, and it’s almost always a relative URI.

The URI is as defined in Section 5.1.2 of the HTTP specification - Request-URI

#### Request path

Use `path` to return the path part of the URI

For example, if the request URI was:

    a/b/c/page.html?param1=abc&param2=xyz

Then the path would be

    /a/b/c/page.html

#### Request query

Use `query` to return the query part of the URI

For example, if the request URI was:

    a/b/c/page.html?param1=abc&param2=xyz

Then the query would be

    param1=abc&param2=xyz

#### Request headers

Use `headers` to return the headers of the HTTP request.

This returns an instance of `MultiMap` - which is like a normal Map or Hash but allows multiple values for the same key - this is because HTTP allows multiple header values with the same key.

It also has case-insensitive keys, that means you can do the following:

    var headers = request.headers();

    // Get the User-Agent:
    console.log("User agent is " + headers.get("user-agent"));

    // You can also do this and get the same result:
    console.log("User agent is " + headers.get("User-Agent"));

#### Request parameters

Use `params` to return the parameters of the HTTP request.

Just like `headers` this returns an instance of `MultiMap` as there can be more than one parameter with the same name.

Request parameters are sent on the request URI, after the path. For example if the URI was:

    /page.html?param1=abc&param2=xyz

Then the parameters would contain the following:

    param1: 'abc'
    param2: 'xyz

Note that these request parameters are retrieved from the URL of the request. If you have form attributes that have been sent as part of the submission of an HTML form submitted in the body of a `multi-part/form-data` request then they will not appear in the params here.

#### Remote address

The address of the sender of the request can be retrieved with `remoteAddress`.

#### Absolute URI

The URI passed in an HTTP request is usually relative. If you wish to retrieve the absolute URI corresponding to the request, you can get it with `absoluteURI`

#### End handler

The `endHandler` of the request is invoked when the entire request, including any body has been fully read.

#### Reading Data from the Request Body

Often an HTTP request contains a body that we want to read. As previously mentioned the request handler is called when just the headers of the request have arrived so the request object does not have a body at that point.

This is because the body may be very large (e.g. a file upload) and we don’t generally want to buffer the entire body in memory before handing it to you, as that could cause the server to exhaust available memory.

To receive the body, you can use the `handler` on the request, this will get called every time a chunk of the request body arrives. Here’s an example:

    request.handler(function (buffer) {
      console.log("I have received a chunk of the body of length " + buffer.length());
    });

The object passed into the handler is a `Buffer`, and the handler can be called multiple times as data arrives from the network, depending on the size of the body.

In some cases (e.g. if the body is small) you will want to aggregate the entire body in memory, so you could do the aggregation yourself as follows:

    var Buffer = require("vertx-js/buffer");

    // Create an empty buffer
    var totalBuffer = Buffer.buffer();

    request.handler(function (buffer) {
      console.log("I have received a chunk of the body of length " + buffer.length());
      totalBuffer.appendBuffer(buffer);
    });

    request.endHandler(function (v) {
      console.log("Full body received, length = " + totalBuffer.length());
    });

This is such a common case, that Vert.x provides a `bodyHandler` to do this for you. The body handler is called once when all the body has been received:

    request.bodyHandler(function (totalBuffer) {
      console.log("Full body received, length = " + totalBuffer.length());
    });

#### Pumping requests

The request object is a `ReadStream` so you can pump the request body to any `WriteStream` instance.

See the chapter on streams and pumps for a detailed explanation.

#### Handling HTML forms

HTML forms can be submitted with either a content type of `application/x-www-form-urlencoded` or `multipart/form-data`.

For url encoded forms, the form attributes are encoded in the url, just like normal query parameters.

For multi-part forms they are encoded in the request body, and as such are not available until the entire body has been read from the wire.

Multi-part forms can also contain file uploads.

If you want to retrieve the attributes of a multi-part form you should tell Vert.x that you expect to receive such a form before any of the body is read by calling `setExpectMultipart` with true, and then you should retrieve the actual attributes using `formAttributes` once the entire body has been read:

    server.requestHandler(function (request) {
      request.setExpectMultipart(true);
      request.endHandler(function (v) {
        // The body has now been fully read, so retrieve the form attributes
        var formAttributes = request.formAttributes();
      });
    });

#### Handling form file uploads

Vert.x can also handle file uploads which are encoded in a multi-part request body.

To receive file uploads you tell Vert.x to expect a multi-part form and set an `uploadHandler` on the request.

This handler will be called once for every upload that arrives on the server.

The object passed into the handler is a `HttpServerFileUpload` instance.

    server.requestHandler(function (request) {
      request.setExpectMultipart(true);
      request.uploadHandler(function (upload) {
        console.log("Got a file upload " + upload.name());
      });
    });

File uploads can be large we don’t provide the entire upload in a single buffer as that might result in memory exhaustion, instead, the upload data is received in chunks:

    request.uploadHandler(function (upload) {
      upload.handler(function (chunk) {
        console.log("Received a chunk of the upload of length " + chunk.length());
      });
    });

The upload object is a `ReadStream` so you can pump the request body to any `WriteStream` instance. See the chapter on streams and pumps for a detailed explanation.

If you just want to upload the file to disk somewhere you can use `streamToFileSystem`:

    request.uploadHandler(function (upload) {
      upload.streamToFileSystem("myuploads_directory/" + upload.filename());
    });

Warning | Make sure you check the filename in a production system to avoid malicious clients uploading files to arbitrary places on your filesystem. See security notes for more information.

### Sending back responses

The server response object is an instance of `HttpServerResponse` and is obtained from the request with `response`.

You use the response object to write a response back to the HTTP client.

#### Setting status code and message

The default HTTP status code for a response is `200`, representing `OK`.

Use `setStatusCode` to set a different code.

You can also specify a custom status message with `setStatusMessage`.

If you don’t specify a status message, the default one corresponding to the status code will be used.

#### Writing HTTP responses

To write data to an HTTP response, you use one the `write` operations.

These can be invoked multiple times before the response is ended. They can be invoked in a few ways:

With a single buffer:

    var response = request.response();
    response.write(buffer);

With a string. In this case the string will encoded using UTF-8 and the result written to the wire.

    var response = request.response();
    response.write("hello world!");

With a string and an encoding. In this case the string will encoded using the specified encoding and the result written to the wire.

    var response = request.response();
    response.write("hello world!", "UTF-16");

Writing to a response is asynchronous and always returns immediately after the write has been queued.

If you are just writing a single string or buffer to the HTTP response you can write it and end the response in a single call to the `end`

The first call to write results in the response header being being written to the response. Consequently, if you are not using HTTP chunking then you must set the `Content-Length` header before writing to the response, since it will be too late otherwise. If you are using HTTP chunking you do not have to worry.

#### Ending HTTP responses

Once you have finished with the HTTP response you should `end` it.

This can be done in several ways:

With no arguments, the response is simply ended.

    var response = request.response();
    response.write("hello world!");
    response.end();

It can also be called with a string or buffer in the same way `write` is called. In this case it’s just the same as calling write with a string or buffer followed by calling end with no arguments. For example:

    var response = request.response();
    response.end("hello world!");

#### Closing the underlying connection

You can close the underlying TCP connection with `close`.

Non keep-alive connections will be automatically closed by Vert.x when the response is ended.

Keep-alive connections are not automatically closed by Vert.x by default. If you want keep-alive connections to be closed after an idle time, then you configure `idleTimeout`.

#### Setting response headers

HTTP response headers can be added to the response by adding them directly to the `headers`:

    var response = request.response();
    var headers = response.headers();
    headers.set("content-type", "text/html");
    headers.set("other-header", "wibble");

Or you can use `putHeader`

    var response = request.response();
    response.putHeader("content-type", "text/html").putHeader("other-header", "wibble");

Headers must all be added before any parts of the response body are written.

#### Chunked HTTP responses and trailers

Vert.x supports HTTP Chunked Transfer Encoding.

This allows the HTTP response body to be written in chunks, and is normally used when a large response body is being streamed to a client and the total size is not known in advance.

You put the HTTP response into chunked mode as follows:

    var response = request.response();
    response.setChunked(true);

Default is non-chunked. When in chunked mode, each call to one of the `write` methods will result in a new HTTP chunk being written out.

When in chunked mode you can also write HTTP response trailers to the response. These are actually written in the final chunk of the response.

To add trailers to the response, add them directly to the `trailers`.

    var response = request.response();
    response.setChunked(true);
    var trailers = response.trailers();
    trailers.set("X-wibble", "woobble").set("X-quux", "flooble");

Or use `putTrailer`.

    var response = request.response();
    response.setChunked(true);
    response.putTrailer("X-wibble", "woobble").putTrailer("X-quux", "flooble");

#### Serving files directly from disk

If you were writing a web server, one way to serve a file from disk would be to open it as an `AsyncFile` and pump it to the HTTP response.

Or you could load it it one go using `readFile` and write it straight to the response.

Alternatively, Vert.x provides a method which allows you to serve a file from disk to an HTTP response in one operation. Where supported by the underlying operating system this may result in the OS directly transferring bytes from the file to the socket without being copied through user-space at all.

This is done by using `sendFile`, and is usually more efficient for large files, but may be slower for small files.

Here’s a very simple web server that serves files from the file system using sendFile:

    vertx.createHttpServer().requestHandler(function (request) {
      var file = "";
      if (request.path() == "/") {
        file = "index.html";
      } else if (!request.path().contains("..")) {
        file = request.path();
      }
      request.response().sendFile("web/" + file);
    }).listen(8080);

Sending a file is asynchronous and may not complete until some time after the call has returned. If you want to be notified when the file has been writen you can use `sendFile`

Note | If you use `sendFile` while using HTTPS it will copy through user-space, since if the kernel is copying data directly from disk to socket it doesn’t give us an opportunity to apply any encryption.

Warning | If you’re going to write web servers directly using Vert.x be careful that users cannot exploit the path to access files outside the directory from which you want to serve them. It may be safer instead to use Vert.x Web.

When there is a need to serve just a segment of a file, say starting from a given byte, you can achieve this by doing:

    vertx.createHttpServer().requestHandler(function (request) {
      var offset = 0;
      try {
        offset = Java.type("java.lang.Long").parseLong(request.getParam("start"));
      } catch(err) {
        // error handling...
      }

      var end = Java.type("java.lang.Long").MAX_VALUE;
      try {
        end = Java.type("java.lang.Long").parseLong(request.getParam("end"));
      } catch(err) {
        // error handling...
      }

      request.response().sendFile("web/mybigfile.txt", offset, end);
    }).listen(8080);

You are not required to supply the length if you want to send a file starting from an offset until the end, in this case you can just do:

    vertx.createHttpServer().requestHandler(function (request) {
      var offset = 0;
      try {
        offset = Java.type("java.lang.Long").parseLong(request.getParam("start"));
      } catch(err) {
        // error handling...
      }

      request.response().sendFile("web/mybigfile.txt", offset);
    }).listen(8080);

#### Pumping responses

The server response is a `WriteStream` instance so you can pump to it from any `ReadStream`, e.g. `AsyncFile`, `NetSocket`, `WebSocket` or `HttpServerRequest`.

Here’s an example which echoes the request body back in the response for any PUT methods. It uses a pump for the body, so it will work even if the HTTP request body is much larger than can fit in memory at any one time:

    var Pump = require("vertx-js/pump");
    vertx.createHttpServer().requestHandler(function (request) {
      var response = request.response();
      if (request.method() === 'PUT') {
        response.setChunked(true);
        Pump.pump(request, response).start();
        request.endHandler(function (v) {
          response.end();
        });
      } else {
        response.setStatusCode(400).end();
      }
    }).listen(8080);

### HTTP Compression

Vert.x comes with support for HTTP Compression out of the box.

This means you are able to automatically compress the body of the responses before they are sent back to the client.

If the client does not support HTTP compression the responses are sent back without compressing the body.

This allows to handle Client that support HTTP Compression and those that not support it at the same time.

To enable compression use can configure it with `compressionSupported`.

By default compression is not enabled.

When HTTP compression is enabled the server will check if the client incldes an `Accept-Encoding` header which includes the supported compressions. Commonly used are deflate and gzip. Both are supported by Vert.x.

If such a header is found the server will automatically compress the body of the response with one of the supported compressions and send it back to the client.

Be aware that compression may be able to reduce network traffic but is more CPU-intensive.

### Creating an HTTP client

You create an `HttpClient` instance with default options as follows:

    var client = vertx.createHttpClient();

If you want to configure options for the client, you create it as follows:

    var options = {
      "keepAlive" : false
    };
    var client = vertx.createHttpClient(options);

### Making requests

The http client is very flexible and there are various ways you can make requests with it.

Often you want to make many requests to the same host/port with an http client. To avoid you repeating the host/port every time you make a request you can configure the client with a default host/port:

    // Set the default host
    var options = {
      "defaultHost" : "wibble.com"
    };
    // Can also set default port if you want...
    var client = vertx.createHttpClient(options);
    client.getNow("/some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

Alternatively if you find yourself making lots of requests to different host/ports with the same client you can simply specify the host/port when doing the request.

    var client = vertx.createHttpClient();

    // Specify both port and host name
    client.getNow(8080, "myserver.mycompany.com", "/some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

    // This time use the default port 80 but specify the host name
    client.getNow("foo.othercompany.com", "/other-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

Both methods of specifying host/port are supported for all the different ways of making requests with the client.

#### Simple requests with no request body

Often, you’ll want to make HTTP requests with no request body. This is usually the case with HTTP GET, OPTIONS and HEAD requests.

The simplest way to do this with the Vert.x http client is using the methods prefixed with `Now`. For example `getNow`.

These methods create the http request and send it in a single method call and allow you to provide a handler that will be called with the http response when it comes back.

    var client = vertx.createHttpClient();

    // Send a GET request
    client.getNow("/some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

    // Send a GET request
    client.headNow("/other-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

#### Writing general requests

At other times you don’t know the request method you want to send until run-time. For that use case we provide general purpose request methods such as `request` which allow you to specify the HTTP method at run-time:

    var client = vertx.createHttpClient();

    client.request('GET', "some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    }).end();

    client.request('POST', "foo-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    }).end("some-data");

#### Writing request bodies

Sometimes you’ll want to write requests which have a body, or perhaps you want to write headers to a request before sending it.

To do this you can call one of the specific request methods such as `post` or one of the general purpose request methods such as `request`.

These methods don’t send the request immediately, but instead return an instance of `HttpClientRequest` which can be used to write to the request body or write headers.

Here are some examples of writing a POST request with a body: m

    var client = vertx.createHttpClient();

    var request = client.post("some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

    // Now do stuff with the request
    request.putHeader("content-length", "1000");
    request.putHeader("content-type", "text/plain");
    request.write(body);

    // Make sure the request is ended when you're done with it
    request.end();

    // Or fluently:

    client.post("some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    }).putHeader("content-length", "1000").putHeader("content-type", "text/plain").write(body).end();

    // Or event more simply:

    client.post("some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    }).putHeader("content-type", "text/plain").end(body);

Methods exist to write strings in UTF-8 encoding and in any specific encoding and to write buffers:

    var Buffer = require("vertx-js/buffer");

    // Write string encoded in UTF-8
    request.write("some data");

    // Write string encoded in specific encoding
    request.write("some other data", "UTF-16");

    // Write a buffer
    var buffer = Buffer.buffer();
    buffer.appendInt(123).appendLong(245);
    request.write(buffer);

If you are just writing a single string or buffer to the HTTP request you can write it and end the request in a single call to the `end` function.

    var Buffer = require("vertx-js/buffer");

    // Write string and end the request (send it) in a single call
    request.end("some simple data");

    // Write buffer and end the request (send it) in a single call
    var buffer = Buffer.buffer().appendDouble(12.34).appendLong(432);
    request.end(buffer);

When you’re writing to a request, the first call to `write` will result in the request headers being written out to the wire.

The actual write is asynchronous and might not occur until some time after the call has returned.

Non-chunked HTTP requests with a request body require a `Content-Length` header to be provided.

Consequently, if you are not using chunked HTTP then you must set the `Content-Length` header before writing to the request, as it will be too late otherwise.

If you are calling one of the `end` methods that take a string or buffer then Vert.x will automatically calculate and set the `Content-Length` header before writing the request body.

If you are using HTTP chunking a a `Content-Length` header is not required, so you do not have to calculate the size up-front.

#### Writing request headers

You can write headers to a request using the `headers` multi-map as follows:

    // Write some headers using the headers() multimap

    var headers = request.headers();
    headers.set("content-type", "application/json").set("other-header", "foo");

The headers are an instance of `MultiMap` which provides operations for adding, setting and removing entries. Http headers allow more than one value for a specific key.

You can also write headers using `putHeader`

    // Write some headers using the putHeader method

    request.putHeader("content-type", "application/json").putHeader("other-header", "foo");

If you wish to write headers to the request you must do so before any part of the request body is written.

#### Ending HTTP requests

Once you have finished with the HTTP request you must end it with one of the `end` operations.

Ending a request causes any headers to be written, if they have not already been written and the request to be marked as complete.

Requests can be ended in several ways. With no arguments the request is simply ended:

    request.end();

Or a string or buffer can be provided in the call to `end`. This is like calling `write` with the string or buffer before calling `end` with no arguments

    var Buffer = require("vertx-js/buffer");
    // End the request with a string
    request.end("some-data");

    // End it with a buffer
    var buffer = Buffer.buffer().appendFloat(12.3).appendInt(321);
    request.end(buffer);

#### Chunked HTTP requests

Vert.x supports HTTP Chunked Transfer Encoding for requests.

This allows the HTTP request body to be written in chunks, and is normally used when a large request body is being streamed to the server, whose size is not known in advance.

You put the HTTP request into chunked mode using `setChunked`.

In chunked mode each call to write will cause a new chunk to be written to the wire. In chunked mode there is no need to set the `Content-Length` of the request up-front.

    request.setChunked(true);

    // Write some chunks
    for (var i = 0; i < 10; i++) {
      request.write("this-is-chunk-" + i);
    }

    request.end();

#### Request timeouts

You can set a timeout for a specific http request using `setTimeout`.

If the request does not return any data within the timeout period an exception will be passed to the exception handler (if provided) and the request will be closed.

#### Handling exceptions

You can handle exceptions corresponding to a request by setting an exception handler on the `HttpClientRequest` instance:

    var request = client.post("some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });
    request.exceptionHandler(function (e) {
      console.log("Received exception: " + e.getMessage());
      e.printStackTrace();
    });

This does not handle non 2xx response that need to be handled in the `HttpClientResponse` code:

    var request = client.post("some-uri", function (response) {
      if (response.statusCode() === 200) {
        console.log("Everything fine");
        return
      }
      if (response.statusCode() === 500) {
        console.log("Unexpected behavior on the server side");
        return
      }
    });
    request.end();

Important | `XXXNow` methods cannot receive an exception handler.

#### Specifying a handler on the client request

Instead of providing a response handler in the call to create the client request object, alternatively, you can not provide a handler when the request is created and set it later on the request object itself, using `handler`, for example:

    var request = client.post("some-uri");
    request.handler(function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

#### Using the request as a stream

The `HttpClientRequest` instance is also a `WriteStream` which means you can pump to it from any `ReadStream` instance.

For, example, you could pump a file on disk to a http request body as follows:

    var Pump = require("vertx-js/pump");

    request.setChunked(true);
    var pump = Pump.pump(file, request);
    file.endHandler(function (v) {
      request.end();
    });
    pump.start();

### Handling http responses

You receive an instance of `HttpClientResponse` into the handler that you specify in of the request methods or by setting a handler directly on the `HttpClientRequest` object.

You can query the status code and the status message of the response with `statusCode` and `statusMessage`.

    client.getNow("some-uri", function (response) {
      // the status code - e.g. 200 or 404
      console.log("Status code is " + response.statusCode());

      // the status message e.g. "OK" or "Not Found".
      console.log("Status message is " + response.statusMessage());
    });

#### Using the response as a stream

The `HttpClientResponse` instance is also a `ReadStream` which means you can pump it to any `WriteStream` instance.

#### Response headers and trailers

Http responses can contain headers. Use `headers` to get the headers.

The object returned is a `MultiMap` as HTTP headers can contain multiple values for single keys.

    var contentType = response.headers().get("content-type");
    var contentLength = response.headers().get("content-lengh");

Chunked HTTP responses can also contain trailers - these are sent in the last chunk of the response body.

You use `trailers` to get the trailers. Trailers are also a `MultiMap`.

#### Reading the request body

The response handler is called when the headers of the response have been read from the wire.

If the response has a body this might arrive in several pieces some time after the headers have been read. We don’t wait for all the body to arrive before calling the response handler as the response could be very large and we might be waiting a long time, or run out of memory for large responses.

As parts of the response body arrive, the `handler` is called with a `Buffer` representing the piece of the body:

    client.getNow("some-uri", function (response) {

      response.handler(function (buffer) {
        console.log("Received a part of the response body: " + buffer);
      });
    });

If you know the response body is not very large and want to aggregate it all in memory before handling it, you can either aggregate it yourself:

    var Buffer = require("vertx-js/buffer");

    client.getNow("some-uri", function (response) {

      // Create an empty buffer
      var totalBuffer = Buffer.buffer();

      response.handler(function (buffer) {
        console.log("Received a part of the response body: " + buffer.length());

        totalBuffer.appendBuffer(buffer);
      });

      response.endHandler(function (v) {
        // Now all the body has been read
        console.log("Total response body length is " + totalBuffer.length());
      });
    });

Or you can use the convenience `bodyHandler` which is called with the entire body when the response has been fully read:

    client.getNow("some-uri", function (response) {

      response.bodyHandler(function (totalBuffer) {
        // Now all the body has been read
        console.log("Total response body length is " + totalBuffer.length());
      });
    });

#### Response end handler

The response `endHandler` is called when the entire response body has been read or immediately after the headers have been read and the response handler has been called if there is no body.

#### Reading cookies from the response

You can retrieve the list of cookies from a response using `cookies`.

Alternatively you can just parse the `Set-Cookie` headers yourself in the response.

#### 100-Continue handling

According to the HTTP 1.1 specification a client can set a header `Expect: 100-Continue` and send the request header before sending the rest of the request body.

The server can then respond with an interim response status `Status: 100 (Continue)` to signify to the client that it is ok to send the rest of the body.

The idea here is it allows the server to authorise and accept/reject the request before large amounts of data are sent. Sending large amounts of data if the request might not be accepted is a waste of bandwidth and ties up the server in reading data that it will just discard.

Vert.x allows you to set a `continueHandler` on the client request object

This will be called if the server sends back a `Status: 100 (Continue)` response to signify that it is ok to send the rest of the request.

This is used in conjunction with `sendHead`to send the head of the request.

Here’s an example:

    var request = client.put("some-uri", function (response) {
      console.log("Received response with status code " + response.statusCode());
    });

    request.putHeader("Expect", "100-Continue");

    request.continueHandler(function (v) {
      // OK to send rest of body
      request.write("Some data");
      request.write("Some more data");
      request.end();
    });

On the server side a Vert.x http server can be configured to automatically send back 100 Continue interim responses when it receives an `Expect: 100-Continue` header. This is done by setting the option `handle100ContinueAutomatically`.

If you’d prefer to decide whether to send back continue responses manually, then this property should be set to `false` (the default), then you can inspect the headers and call `writeContinue` if you wish the client to continue sending the body or you can reject the request by sending back a failure status code if you don’t want it to send the body. For example:

### Enabling compression on the client

The http client comes with support for HTTP Compression out of the box.

This means the client can let the remote http server know that it supports compression, and will be able to handle compressed response bodies.

An http server is free to either compress with one of the supported compression algorithms or to send the body back without compressing it at all. So this is only a hint for the Http server which it may ignore at will.

To tell the http server which compression is supported by the client it will include an `Accept-Encoding` header with the supported compression algorithm as value. Multiple compression algorithms are supported. In case of Vert.x this will result in the following header added:

    Accept-Encoding: gzip, deflate

The server will choose then from one of these. You can detect if a server ompressed the body by checking for the `Content-Encoding` header in the response sent back from it.

If the body of the response was compressed via gzip it will include for example the following header:

    Content-Encoding: gzip

To enable compression set `tryUseCompression` on the options used when creating the client.

By default compression is disabled.

### Pooling and keep alive

Http keep alive allows http connections to be used for more than one request. This can be a more efficient use of connections when you’re making multiple requests to the same server.

The http client supports pooling of connections, allowing you to reuse connections between requests.

For pooling to work, keep alive must be true using `keepAlive` on the options used when configuring the client. The default value is true.

When keep alive is enabled. Vert.x will add a `Connection: Keep-Alive` header to each HTTP/1.0 request sent. When keep alive is disabled. Vert.x will add a `Connection: Close` header to each HTTP/1.1 request sent to signal that the connection will be closed after completion of the response.

The maximum number of connections to pool for each server is configured using `maxPoolSize`

When making a request with pooling enabled, Vert.x will create a new connection if there are less than the maximum number of connections already created for that server, otherwise it will add the request to a queue.

Keep alive connections will not be closed by the client automatically. To close them you can close the client instance.

Alternatively you can set idle timeout using `idleTimeout` - any connections not used within this timeout will be closed. Please note the idle timeout value is in seconds not milliseconds.

### Pipe-lining

The client also supports pipe-lining of requests on a connection.

Pipe-lining means another request is sent on the same connection before the response from the preceding one has returned. Pipe-lining is not appropriate for all requests.

To enable pipe-lining, it must be enabled using `pipelining`. By default pipe-lining is disabled.

When pipe-lining is enabled requests will be written to connections without waiting for previous responses to return.

### Server sharing

When several HTTP servers listen on the same port, vert.x orchestrates the request handling using a round-robin strategy.

Let’s take a verticle creating a HTTP server such as:

io.vertx.examples.http.sharing.HttpServerVerticle

    vertx.createHttpServer().requestHandler(function (request) {
      request.response().end("Hello from server " + this);
    }).listen(8080);

This service is listening on the port 8080. So, when this verticle is instantiated multiple times as with: `vertx run io.vertx.examples.http.sharing.HttpServerVerticle -instances 2`, what’s happening ? If both verticles would bind to the same port, you would receive a socket exception. Fortunately, vert.x is handling this case for you. When you deploy another server on the same host and port as an existing server it doesn’t actually try and create a new server listening on the same host/port. It binds only once to the socket. When receiving a request it calls the server handlers following a round robin strategy.

Let’s now imagine a client such as:

    vertx.setPeriodic(100, function (l) {
      vertx.createHttpClient().getNow(8080, "localhost", "/", function (resp) {
        resp.bodyHandler(function (body) {
          console.log(body.toString("ISO-8859-1"));
        });
      });
    });

Vert.x delegates the requests to one of the server sequentially:

    Hello from i.v.e.h.s.HttpServerVerticle@1
    Hello from i.v.e.h.s.HttpServerVerticle@2
    Hello from i.v.e.h.s.HttpServerVerticle@1
    Hello from i.v.e.h.s.HttpServerVerticle@2
    ...

Consequently the servers can scale over available cores while each Vert.x verticle instance remains strictly single threaded, and you don’t have to do any special tricks like writing load-balancers in order to scale your server on your multi-core machine.

### Using HTTPS with Vert.x

Vert.x http servers and clients can be configured to use HTTPS in exactly the same way as net servers.

Please see configuring net servers to use SSL for more information.

### WebSockets

WebSockets are a web technology that allows a full duplex socket-like connection between HTTP servers and HTTP clients (typically browsers).

Vert.x supports WebSockets on both the client and server-side.

#### WebSockets on the server

There are two ways of handling WebSockets on the server side.

##### WebSocket handler

The first way involves providing a `websocketHandler` on the server instance.

When a WebSocket connection is made to the server, the handler will be called, passing in an instance of `ServerWebSocket`.

    server.websocketHandler(function (websocket) {
      console.log("Connected!");
    });

You can choose to reject the WebSocket by calling `reject`.

    server.websocketHandler(function (websocket) {
      if (websocket.path() == "/myapi") {
        websocket.reject();
      } else {
        // Do something
      }
    });

##### Upgrading to WebSocket

The second way of handling WebSockets is to handle the HTTP Upgrade request that was sent from the client, and call `upgrade` on the server request.

    server.requestHandler(function (request) {
      if (request.path() == "/myapi") {

        var websocket = request.upgrade();
        // Do something

      } else {
        // Reject
        request.response().setStatusCode(400).end();
      }
    });

##### The server WebSocket

The `ServerWebSocket` instance enables you to retrieve the `headers`, `path` path}, `query` and `uri` URI} of the HTTP request of the WebSocket handshake.

#### WebSockets on the client

The Vert.x `HttpClient` supports WebSockets.

You can connect a WebSocket to a server using one of the `websocket` operations and providing a handler.

The handler will be called with an instance of `WebSocket` when the connection has been made:

    client.websocket("/some-uri", function (websocket) {
      console.log("Connected!");
    });

#### Writing messages to WebSockets

If you wish to write a single binary WebSocket message to the WebSocket you can do this with `writeBinaryMessage`:

    var Buffer = require("vertx-js/buffer");
    // Write a simple message
    var buffer = Buffer.buffer().appendInt(123).appendFloat(1.23);

    websocket.writeBinaryMessage(buffer);

If the WebSocket message is larger than the maximum websocket frame size as configured with `maxWebsocketFrameSize` then Vert.x will split it into multiple WebSocket frames before sending it on the wire.

#### Writing frames to WebSockets

A WebSocket message can be composed of multiple frames. In this case the first frame is either a binary or text frame followed by zero or more continuation frames.

The last frame in the message is marked as final.

To send a message consisting of multiple frames you create frames using `WebSocketFrame.binaryFrame` , `WebSocketFrame.textFrame` or `WebSocketFrame.continuationFrame` and write them to the WebSocket using `writeFrame`.

Here’s an example for binary frames:

    var WebSocketFrame = require("vertx-js/web_socket_frame");

    var frame1 = WebSocketFrame.binaryFrame(buffer1, false);
    websocket.writeFrame(frame1);

    var frame2 = WebSocketFrame.continuationFrame(buffer2, false);
    websocket.writeFrame(frame2);

    // Write the final frame
    var frame3 = WebSocketFrame.continuationFrame(buffer2, true);
    websocket.writeFrame(frame3);

In many cases you just want to send a websocket message that consists of a single final frame, so we provide a couple of shortcut methods to do that with `writeFinalBinaryFrame` and `writeFinalTextFrame`.

Here’s an example:

    var Buffer = require("vertx-js/buffer");

    // Send a websocket messages consisting of a single final text frame:

    websocket.writeFinalTextFrame("Geronimo!");

    // Send a websocket messages consisting of a single final binary frame:

    var buff = Buffer.buffer().appendInt(12).appendString("foo");

    websocket.writeFinalBinaryFrame(buff);

#### Reading frames from WebSockets

To read frames from a WebSocket you use the `frameHandler`.

The frame handler will be called with instances of `WebSocketFrame` when a frame arrives, for example:

    websocket.frameHandler(function (frame) {
      console.log("Received a frame of size!");
    });

#### Closing WebSockets

Use `close` to close the WebSocket connection when you have finished with it.

#### Streaming WebSockets

The `WebSocket` instance is also a `ReadStream` and a `WriteStream` so it can be used with pumps.

When using a WebSocket as a write stream or a read stream it can only be used with WebSockets connections that are used with binary frames that are no split over multiple frames.

### Automatic clean-up in verticles

If you’re creating http servers and clients from inside verticles, those servers and clients will be automatically closed when the verticle is undeployed.

## Using Shared Data with Vert.x

Shared data contains functionality that allows you to safely share data between different parts of your application, or different applications in the same Vert.x instance or across a cluster of Vert.x instances.

Shared data includes local shared maps, distributed, cluster-wide maps, asynchronous cluster-wide locks and asynchronous cluster-wide counters.

### Local shared maps

`Local shared maps` allow you to share data safely between different event loops (e.g. different verticles) in the same Vert.x instance.

Local shared maps only allow certain data types to be used as keys and values. Those types must either be immutable, or certain other types that can be copied like `Buffer`. In the latter case the key/value will be copied before putting it in the map.

This way we can ensure there is no shared access to mutable state between different threads in your Vert.x application so you don’t have to worry about protecting that state by synchronising access to it.

Here’s an example of using a shared local map:

    var Buffer = require("vertx-js/buffer");

    var sd = vertx.sharedData();

    var map1 = sd.getLocalMap("mymap1");

    map1.put("foo", "bar");

    var map2 = sd.getLocalMap("mymap2");

    map2.put("eek", Buffer.buffer().appendInt(123));

    // Then... in another part of your application:

    map1 = sd.getLocalMap("mymap1");

    var val = map1.get("foo");

    map2 = sd.getLocalMap("mymap2");

    var buff = map2.get("eek");

### Cluster-wide asynchronous maps

Cluster-wide asynchronous maps allow data to be put in the map from any node of the cluster and retrieved from any other node.

This makes them really useful for things like storing session state in a farm of servers hosting a Vert.x web application.

You get an instance of `AsyncMap` with `getClusterWideMap`.

Getting the map is asynchronous and the result is returned to you in the handler that you specify. Here’s an example:

    var sd = vertx.sharedData();

    sd.getClusterWideMap("mymap", function (res, res_err) {
      if (res_err == null) {
        var map = res;
      } else {
        // Something went wrong!
      }
    });

#### Putting data in a map

You put data in a map with `put`.

The actual put is asynchronous and the handler is notified once it is complete:

    map.put("foo", "bar", function (resPut, resPut_err) {
      if (resPut_err == null) {
        // Successfully put the value
      } else {
        // Something went wrong!
      }
    });

#### Getting data from a map

You get data from a map with `get`.

The actual get is asynchronous and the handler is notified with the result some time later

    map.get("foo", function (resGet, resGet_err) {
      if (resGet_err == null) {
        // Successfully got the value
        var val = resGet;
      } else {
        // Something went wrong!
      }
    });

##### Other map operations

You can also remove entries from an asynchronous map, clear them and get the size.

See the `API docs` for more information.

### Cluster-wide locks

`Cluster wide locks` allow you to obtain exclusive locks across the cluster - this is useful when you want to do something or access a resource on only one node of a cluster at any one time.

Cluster wide locks have an asynchronous API unlike most lock APIs which block the calling thread until the lock is obtained.

To obtain a lock use `getLock`.

This won’t block, but when the lock is available, the handler will be called with an instance of `Lock`, signifying that you now own the lock.

While you own the lock no other caller, anywhere on the cluster will be able to obtain the lock.

When you’ve finished with the lock, you call `release` to release it, so another caller can obtain it.

    sd.getLock("mylock", function (res, res_err) {
      if (res_err == null) {
        // Got the lock!
        var lock = res;

        // 5 seconds later we release the lock so someone else can get it

        vertx.setTimer(5000, function (tid) {
          lock.release();
        });

      } else {
        // Something went wrong
      }
    });

You can also get a lock with a timeout. If it fails to obtain the lock within the timeout the handler will be called with a failure:

    sd.getLockWithTimeout("mylock", 10000, function (res, res_err) {
      if (res_err == null) {
        // Got the lock!
        var lock = res;

      } else {
        // Failed to get lock
      }
    });

### Cluster-wide counters

It’s often useful to maintain an atomic counter across the different nodes of your application.

You can do this with `Counter`.

You obtain an instance with `getCounter`:

    sd.getCounter("mycounter", function (res, res_err) {
      if (res_err == null) {
        var counter = res;
      } else {
        // Something went wrong!
      }
    });

Once you have an instance you can retrieve the current count, atomically increment it, decrement and add a value to it using the various methods.

See the `API docs` for more information.

## Using the file system with Vert.x

The Vert.x `FileSystem` object provides many operations for manipulating the file system.

There is one file system object per Vert.x instance, and you obtain it with `fileSystem`.

A blocking and a non blocking version of each operation is provided. The non blocking versions take a handler which is called when the operation completes or an error occurs.

Here’s an example of an asynchronous copy of a file:

    var fs = vertx.fileSystem();

    // Copy file from foo.txt to bar.txt
    fs.copy("foo.txt", "bar.txt", function (res, res_err) {
      if (res_err == null) {
        // Copied ok!
      } else {
        // Something went wrong
      }
    });

The blocking versions are named `xxxBlocking` and return the results or throw exceptions directly. In many cases, depending on the operating system and file system, some of the potentially blocking operations can return quickly, which is why we provide them, but it’s highly recommended that you test how long they take to return in your particular application before using them from an event loop, so as not to break the Golden Rule.

Here’s the copy using the blocking API:

    var fs = vertx.fileSystem();

    // Copy file from foo.txt to bar.txt synchronously
    fs.copyBlocking("foo.txt", "bar.txt");

Many operations exist to copy, move, truncate, chmod and many other file operations. We won’t list them all here, please consult the `API docs` for the full list.

Let’s see a couple of examples using asynchronous methods:

    var Vertx = require("vertx-js/vertx");
    var Buffer = require("vertx-js/buffer");
    var vertx = Vertx.vertx();

    // Read a file
    vertx.fileSystem().readFile("target/classes/readme.txt", function (result, result_err) {
      if (result_err == null) {
        console.log(result);
      } else {
        console.error("Oh oh ..." + result_err);
      }
    });

    // Copy a file
    vertx.fileSystem().copy("target/classes/readme.txt", "target/classes/readme2.txt", function (result, result_err) {
      if (result_err == null) {
        console.log("File copied");
      } else {
        console.error("Oh oh ..." + result_err);
      }
    });

    // Write a file
    vertx.fileSystem().writeFile("target/classes/hello.txt", Buffer.buffer("Hello"), function (result, result_err) {
      if (result_err == null) {
        console.log("File written");
      } else {
        console.error("Oh oh ..." + result_err);
      }
    });

    // Check existence and delete
    vertx.fileSystem().exists("target/classes/junk.txt", function (result, result_err) {
      if (result_err == null && result) {
        vertx.fileSystem().delete("target/classes/junk.txt", function (r, r_err) {
          console.log("File deleted");
        });
      } else {
        console.error("Oh oh ... - cannot delete the file: " + result_err);
      }
    });

### Asynchronous files

Vert.x provides an asynchronous file abstraction that allows you to manipulate a file on the file system.

You open an `AsyncFile` as follows:

    var options = {
    };
    fileSystem.open("myfile.txt", options, function (res, res_err) {
      if (res_err == null) {
        var file = res;
      } else {
        // Something went wrong!
      }
    });

`AsyncFile` implements `ReadStream` and `WriteStream` so you can pump files to and from other stream objects such as net sockets, http requests and responses, and WebSockets.

They also allow you to read and write directly to them.

#### Random access writes

To use an `AsyncFile` for random access writing you use the `write` method.

The parameters to the method are:

- `buffer`: the buffer to write.

- `position`: an integer position in the file where to write the buffer. If the position is greater or equal to the size of the file, the file will be enlarged to accommodate the offset.

- `handler`: the result handler

Here is an example of random access writes:

    var Vertx = require("vertx-js/vertx");
    var Buffer = require("vertx-js/buffer");
    var vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/hello.txt", {
    }, function (result, result_err) {
      if (result_err == null) {
        var file = result;
        var buff = Buffer.buffer("foo");
        for (var i = 0; i < 5; i++) {
          file.write(buff, buff.length() * i, function (ar, ar_err) {
            if (ar_err == null) {
              console.log("Written ok!");
              // etc
            } else {
              console.error("Failed to write: " + ar_err);
            }
          });
        }
      } else {
        console.error("Cannot open file " + result_err);
      }
    });

#### Random access reads

To use an `AsyncFile` for random access reads you use the `read` method.

The parameters to the method are:

- `buffer`: the buffer into which the data will be read.

- `offset`: an integer offset into the buffer where the read data will be placed.

- `position`: the position in the file where to read data from.

- `length`: the number of bytes of data to read

- `handler`: the result handler

Here’s an example of random access reads:

    var Vertx = require("vertx-js/vertx");
    var Buffer = require("vertx-js/buffer");
    var vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/les_miserables.txt", {
    }, function (result, result_err) {
      if (result_err == null) {
        var file = result;
        var buff = Buffer.buffer(1000);
        for (var i = 0; i < 10; i++) {
          file.read(buff, i * 100, i * 100, 100, function (ar, ar_err) {
            if (ar_err == null) {
              console.log("Read ok!");
            } else {
              console.error("Failed to write: " + ar_err);
            }
          });
        }
      } else {
        console.error("Cannot open file " + result_err);
      }
    });

#### Opening Options

When opening an `AsyncFile`, you pass an `OpenOptions` instance. These options describe the behavior of the file access. For instance, you can configure the file permissions with the `read`, `write` and `perms` methods.

You can also configure the behavior if the open file already exists with `createNew` and `truncateExisting`.

You can also mark the file to be deleted on close or when the JVM is shutdown with `deleteOnClose`.

#### Flushing data to underlying storage.

In the `OpenOptions`, you can enable/disable the automatic synchronisation of the content on every write using `dsync`. In that case, you can manually flush any writes from the OS cache by calling the `flush` method.

This method can also be called with an handler which will be called when the flush is complete.

#### Using AsyncFile as ReadStream and WriteStream

`AsyncFile` implements `ReadStream` and `WriteStream`. You can then use them with a pump to pump data to and from other read and write streams. For example, this would copy the content to another `AsyncFile`:

    var Vertx = require("vertx-js/vertx");
    var Pump = require("vertx-js/pump");
    var vertx = Vertx.vertx();
    var output = vertx.fileSystem().openBlocking("target/classes/plagiary.txt", {
    });

    vertx.fileSystem().open("target/classes/les_miserables.txt", {
    }, function (result, result_err) {
      if (result_err == null) {
        var file = result;
        Pump.pump(file, output).start();
        file.endHandler(function (r) {
          console.log("Copy done");
        });
      } else {
        console.error("Cannot open file " + result_err);
      }
    });

You can also use the pump to write file content into HTTP responses, or more generally in any `WriteStream`.

#### Closing an AsyncFile

To close an `AsyncFile` call the `close` method. Closing is asynchronous and if you want to be notified when the close has been completed you can specify a handler function as an argument.

## Datagram sockets (UDP)

Using User Datagram Protocol (UDP) with Vert.x is a piece of cake.

UDP is a connection-less transport which basically means you have no persistent connection to a remote peer.

Instead you can send and receive packages and the remote address is contained in each of them.

Beside this UDP is not as safe as TCP to use, which means there are no guarantees that a send Datagram packet will receive it’s endpoint at all.

The only guarantee is that it will either receive complete or not at all.

Also you usually can’t send data which is bigger then the MTU size of your network interface, this is because each packet will be send as one packet.

But be aware even if the packet size is smaller then the MTU it may still fail.

At which size it will fail depends on the Operating System etc. So rule of thumb is to try to send small packets.

Because of the nature of UDP it is best fit for Applications where you are allowed to drop packets (like for example a monitoring application).

The benefits are that it has a lot less overhead compared to TCP, which can be handled by the NetServer and NetClient (see above).

### Creating a DatagramSocket

To use UDP you first need t create a `DatagramSocket`. It does not matter here if you only want to send data or send and receive.

    var socket = vertx.createDatagramSocket({
    });

The returned `DatagramSocket` will not be bound to a specific port. This is not a problem if you only want to send data (like a client), but more on this in the next section.

### Sending Datagram packets

As mentioned before, User Datagram Protocol (UDP) sends data in packets to remote peers but is not connected to them in a persistent fashion.

This means each packet can be sent to a different remote peer.

Sending packets is as easy as shown here:

    var Buffer = require("vertx-js/buffer");
    var socket = vertx.createDatagramSocket({
    });
    var buffer = Buffer.buffer("content");
    // Send a Buffer
    socket.send(buffer, 1234, "10.0.0.1", function (asyncResult, asyncResult_err) {
      console.log("Send succeeded? " + asyncResult_err == null);
    });
    // Send a String
    socket.send("A string used as content", 1234, "10.0.0.1", function (asyncResult, asyncResult_err) {
      console.log("Send succeeded? " + asyncResult_err == null);
    });

### Receiving Datagram packets

If you want to receive packets you need to bind the `DatagramSocket` by calling `listen(…​)}` on it.

This way you will be able to receive `DatagramPacket`s that were sent to the address and port on which the `DatagramSocket` listens.

Beside this you also want to set a `Handler` which will be called for each received `DatagramPacket`.

The `DatagramPacket` has the following methods:

- `sender`: The InetSocketAddress which represent the sender of the packet

- `data`: The Buffer which holds the data which was received.

So to listen on a specific address and port you would do something like shown here:

    var socket = vertx.createDatagramSocket({
    });
    socket.listen(1234, "0.0.0.0", function (asyncResult, asyncResult_err) {
      if (asyncResult_err == null) {
        socket.handler(function (packet) {
          // Do something with the packet
        });
      } else {
        console.log("Listen failed" + asyncResult_err);
      }
    });

Be aware that even if the {code AsyncResult} is successed it only means it might be written on the network stack, but gives no guarantee that it ever reached or will reach the remote peer at all.

If you need such a guarantee then you want to use TCP with some handshaking logic build on top.

### Multicast

#### Sending Multicast packets

Multicast allows multiple sockets to receive the same packets. This works by have same join a multicast group to which you can send packets.

We will look at how you can joint a Multicast Group and so receive packets in the next section.

For now let us focus on how to send those. Sending multicast packets is not different to send normal Datagram Packets.

The only difference is that you would pass in a multicast group address to the send method.

This is show here:

    var Buffer = require("vertx-js/buffer");
    var socket = vertx.createDatagramSocket({
    });
    var buffer = Buffer.buffer("content");
    // Send a Buffer to a multicast address
    socket.send(buffer, 1234, "230.0.0.1", function (asyncResult, asyncResult_err) {
      console.log("Send succeeded? " + asyncResult_err == null);
    });

All sockets that have joined the multicast group 230.0.0.1 will receive the packet.

##### Receiving Multicast packets

If you want to receive packets for specific Multicast group you need to bind the `DatagramSocket` by calling `listen(…​)` on it and join the Multicast group.

This way you will be able to receive DatagramPackets that were sent to the address and port on which the `DatagramSocket` listens and also to those sent to the Multicast group.

Beside this you also want to set a Handler which will be called for each received DatagramPacket.

The `DatagramPacket` has the following methods:

- `sender()`: The InetSocketAddress which represent the sender of the packet

- `data()`: The Buffer which holds the data which was received.

So to listen on a specific address and port and also receive packets for the Multicast group 230.0.0.1 you would do something like shown here:

    var socket = vertx.createDatagramSocket({
    });
    socket.listen(1234, "0.0.0.0", function (asyncResult, asyncResult_err) {
      if (asyncResult_err == null) {
        socket.handler(function (packet) {
          // Do something with the packet
        });

        // join the multicast group
        socket.listenMulticastGroup("230.0.0.1", function (asyncResult2, asyncResult2_err) {
          console.log("Listen succeeded? " + asyncResult2_err == null);
        });
      } else {
        console.log("Listen failed" + asyncResult_err);
      }
    });

##### Unlisten / leave a Multicast group

There are sometimes situations where you want to receive packets for a Multicast group for a limited time.

In this situations you can first start to listen for them and then later unlisten.

This is shown here:

    var socket = vertx.createDatagramSocket({
    });
    socket.listen(1234, "0.0.0.0", function (asyncResult, asyncResult_err) {
      if (asyncResult_err == null) {
        socket.handler(function (packet) {
          // Do something with the packet
        });

        // join the multicast group
        socket.listenMulticastGroup("230.0.0.1", function (asyncResult2, asyncResult2_err) {
          if (asyncResult2_err == null) {
            // will now receive packets for group

            // do some work

            socket.unlistenMulticastGroup("230.0.0.1", function (asyncResult3, asyncResult3_err) {
              console.log("Unlisten succeeded? " + asyncResult3_err == null);
            });
          } else {
            console.log("Listen failed" + asyncResult2_err);
          }
        });
      } else {
        console.log("Listen failed" + asyncResult_err);
      }
    });

##### Blocking multicast

Beside unlisten a Multicast address it’s also possible to just block multicast for a specific sender address.

Be aware this only work on some Operating Systems and kernel versions. So please check the Operating System documentation if it’s supported.

This an expert feature.

To block multicast from a specific address you can call `blockMulticastGroup(…​)` on the DatagramSocket like shown here:

    var socket = vertx.createDatagramSocket({
    });

    // Some code

    // This would block packets which are send from 10.0.0.2
    socket.blockMulticastGroup("230.0.0.1", "10.0.0.2", function (asyncResult, asyncResult_err) {
      console.log("block succeeded? " + asyncResult_err == null);
    });

#### DatagramSocket properties

When creating a `DatagramSocket` there are multiple properties you can set to change it’s behaviour with the `DatagramSocketOptions` object. Those are listed here:

- `sendBufferSize` Sets the send buffer size in bytes.

- `receiveBufferSize` Sets the TCP receive buffer size in bytes.

- `reuseAddress` If true then addresses in TIME_WAIT state can be reused after they have been closed.

- `trafficClass`

- `broadcast` Sets or clears the SO_BROADCAST socket option. When this option is set, Datagram (UDP) packets may be sent to a local interface’s broadcast address.

- `multicastNetworkInterface` Sets or clears the IP_MULTICAST_LOOP socket option. When this option is set, multicast packets will also be received on the local interface.

- `multicastTimeToLive` Sets the IP_MULTICAST_TTL socket option. TTL stands for "Time to Live," but in this context it specifies the number of IP hops that a packet is allowed to go through, specifically for multicast traffic. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded.

#### DatagramSocket Local Address

You can find out the local address of the socket (i.e. the address of this side of the UDP Socket) by calling `localAddress`. This will only return an `InetSocketAddress` if you bound the `DatagramSocket` with `listen(…​)` before, otherwise it will return null.

#### Closing a DatagramSocket

You can close a socket by invoking the `close` method. This will close the socket and release all resources

## DNS client

Often you will find yourself in situations where you need to obtain DNS informations in an asynchronous fashion.

Unfortunally this is not possible with the API that is shipped with the Java Virtual Machine itself. Because of this Vert.x offers it’s own API for DNS resolution which is fully asynchronous.

To obtain a DnsClient instance you will create a new via the Vertx instance.

    var client = vertx.createDnsClient(53, "10.0.0.1");

Be aware that you can pass in a varargs of InetSocketAddress arguments to specifiy more then one DNS Server to try to query for DNS resolution. The DNS Servers will be queried in the same order as specified here. Where the next will be used once the first produce an error while be used.

### lookup

Try to lookup the A (ipv4) or AAAA (ipv6) record for a given name. The first which is returned will be used, so it behaves the same way as you may be used from when using "nslookup" on your operation system.

To lookup the A / AAAA record for "vertx.io" you would typically use it like:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.lookup("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        console.log(ar);
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### lookup4

Try to lookup the A (ipv4) record for a given name. The first which is returned will be used, so it behaves the same way as you may be used from when using "nslookup" on your operation system.

To lookup the A record for "vertx.io" you would typically use it like:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.lookup4("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        console.log(ar);
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### lookup6

Try to lookup the AAAA (ipv6) record for a given name. The first which is returned will be used, so it behaves the same way as you may be used from when using "nslookup" on your operation system.

To lookup the A record for "vertx.io" you would typically use it like:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.lookup6("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        console.log(ar);
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### resolveA

Try to resolve all A (ipv4) records for a given name. This is quite similar to using "dig" on unix like operation systems.

To lookup all the A records for "vertx.io" you would typically do:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveA("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### resolveAAAA

Try to resolve all AAAA (ipv6) records for a given name. This is quite similar to using "dig" on unix like operation systems.

To lookup all the AAAAA records for "vertx.io" you would typically do:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveAAAA("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### resolveCNAME

Try to resolve all CNAME records for a given name. This is quite similar to using "dig" on unix like operation systems.

To lookup all the CNAME records for "vertx.io" you would typically do:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveCNAME("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### resolveMX

Try to resolve all MX records for a given name. The MX records are used to define which Mail-Server accepts emails for a given domain.

To lookup all the MX records for "vertx.io" you would typically do:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveMX("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

Be aware that the List will contain the `MxRecord` sorted by the priority of them, which means MX records with smaller priority coming first in the List.

The `MxRecord` allows you to access the priority and the name of the MX record by offer methods for it like:

    record.priority();
    record.name();

### resolveTXT

Try to resolve all TXT records for a given name. TXT records are often used to define extra informations for a domain.

To resolve all the TXT records for "vertx.io" you could use something along these lines:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveTXT("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### resolveNS

Try to resolve all NS records for a given name. The NS records specify which DNS Server hosts the DNS informations for a given domain.

To resolve all the NS records for "vertx.io" you could use something along these lines:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveNS("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### resolveSRV

Try to resolve all SRV records for a given name. The SRV records are used to define extra informations like port and hostname of services. Some protocols need this extra informations.

To lookup all the SRV records for "vertx.io" you would typically do:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolveSRV("vertx.io", function (ar, ar_err) {
      if (ar_err == null) {
        var records = ar;
        Array.prototype.forEach.call(records, function(record) {
          console.log(record);
        });
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

Be aware that the List will contain the SrvRecords sorted by the priority of them, which means SrvRecords with smaller priority coming first in the List.

The `SrvRecord` allows you to access all informations contained in the SRV record itself:

    record.priority();
    record.name();
    record.weight();
    record.port();
    record.protocol();
    record.service();
    record.target();

Please refer to the API docs for the exact details.

### resolvePTR

Try to resolve the PTR record for a given name. The PTR record maps an ipaddress to a name.

To resolve the PTR record for the ipaddress 10.0.0.1 you would use the PTR notion of "1.0.0.10.in-addr.arpa"

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.resolvePTR("1.0.0.10.in-addr.arpa", function (ar, ar_err) {
      if (ar_err == null) {
        var record = ar;
        console.log(record);
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

### reverseLookup

Try to do a reverse lookup for an ipaddress. This is basically the same as resolve a PTR record, but allows you to just pass in the ipaddress and not a valid PTR query string.

To do a reverse lookup for the ipaddress 10.0.0.1 do something similar like this:

    var client = vertx.createDnsClient(53, "10.0.0.1");
    client.reverseLookup("10.0.0.1", function (ar, ar_err) {
      if (ar_err == null) {
        var record = ar;
        console.log(record);
      } else {
        console.log("Failed to resolve entry" + ar_err);
      }
    });

## Streams

There are several objects in Vert.x that allow items to be read from and written.

In previous versions the streams.adoc package was manipulating `Buffer` objects exclusively. From now, streams are not anymore coupled to buffers and work with any kind of objects.

In Vert.x, calls to write item return immediately and writes are internally queued.

It’s not hard to see that if you write to an object faster than it can actually write the data to its underlying resource then the write queue could grow without bound - eventually resulting in exhausting available memory.

To solve this problem a simple flow control capability is provided by some objects in the Vert.x API.

Any flow control aware object that can be written-to implements `ReadStream`, and any flow control object that can be read-from is said to implement `WriteStream`.

Let’s take an example where we want to read from a `ReadStream` and write the data to a `WriteStream`.

A very simple example would be reading from a `NetSocket` on a server and writing back to the same `NetSocket` - since `NetSocket` implements both `ReadStream` and `WriteStream`, but you can do this between any `ReadStream` and any `WriteStream`, including HTTP requests and response, async files, WebSockets, etc.

A naive way to do this would be to directly take the data that’s been read and immediately write it to the `NetSocket`, for example:

    var server = vertx.createNetServer({
      "port" : 1234,
      "host" : "localhost"
    });
    server.connectHandler(function (sock) {
      sock.handler(function (buffer) {
        // Write the data straight back
        sock.write(buffer);
      });
    }).listen();

There’s a problem with the above example: If data is read from the socket faster than it can be written back to the socket, it will build up in the write queue of the `NetSocket`, eventually running out of RAM. This might happen, for example if the client at the other end of the socket wasn’t reading very fast, effectively putting back-pressure on the connection.

Since `NetSocket` implements `WriteStream`, we can check if the `WriteStream` is full before writing to it:

    var server = vertx.createNetServer({
      "port" : 1234,
      "host" : "localhost"
    });
    server.connectHandler(function (sock) {
      sock.handler(function (buffer) {
        if (!sock.writeQueueFull()) {
          sock.write(buffer);
        }
      });

    }).listen();

This example won’t run out of RAM but we’ll end up losing data if the write queue gets full. What we really want to do is pause the `NetSocket` when the write queue is full. Let’s do that:

    var server = vertx.createNetServer({
      "port" : 1234,
      "host" : "localhost"
    });
    server.connectHandler(function (sock) {
      sock.handler(function (buffer) {
        sock.write(buffer);
        if (sock.writeQueueFull()) {
          sock.pause();
        }
      });
    }).listen();

We’re almost there, but not quite. The `NetSocket` now gets paused when the file is full, but we also need to unpause it when the write queue has processed its backlog:

    var server = vertx.createNetServer({
      "port" : 1234,
      "host" : "localhost"
    });
    server.connectHandler(function (sock) {
      sock.handler(function (buffer) {
        sock.write(buffer);
        if (sock.writeQueueFull()) {
          sock.pause();
          sock.drainHandler(function (done) {
            sock.resume();
          });
        }
      });
    }).listen();

And there we have it. The `drainHandler` event handler will get called when the write queue is ready to accept more data, this resumes the `NetSocket` which allows it to read more data.

It’s very common to want to do this when writing Vert.x applications, so we provide a helper class called `Pump` which does all this hard work for you. You just feed it the `ReadStream` and the `WriteStream` and it tell it to start:

    var Pump = require("vertx-js/pump");
    var server = vertx.createNetServer({
      "port" : 1234,
      "host" : "localhost"
    });
    server.connectHandler(function (sock) {
      Pump.pump(sock, sock).start();
    }).listen();

Which does exactly the same thing as the more verbose example.

Let’s look at the methods on `ReadStream` and `WriteStream` in more detail:

### ReadStream

`ReadStream` is implemented by `HttpClientResponse`, `DatagramSocket`, `HttpClientRequest`, `HttpServerFileUpload`, `HttpServerRequest`, `HttpServerRequestStream`, `MessageConsumer`, `NetSocket`, `NetSocketStream`, `WebSocket`, `WebSocketStream`, `TimeoutStream`, `AsyncFile`.

Functions:

- `handler`: set a handler which will receive items from the ReadStream.

- `pause`: pause the handler. When paused no items will be received in the handler.

- `resume`: resume the handler. The handler will be called if any item arrives.

- `exceptionHandler`: Will be called if an exception occurs on the ReadStream.

- `endHandler`: Will be called when end of stream is reached. This might be when EOF is reached if the ReadStream represents a file, or when end of request is reached if it’s an HTTP request, or when the connection is closed if it’s a TCP socket.

### WriteStream

`WriteStream` is implemented by `HttpClientRequest`, `HttpServerResponse` `WebSocket`, `NetSocket`, `AsyncFile`, `PacketWritestream` and `MessageProducer`

Functions:

- `write`: write an object to the WriteStream. This method will never block. Writes are queued internally and asynchronously written to the underlying resource.

- `setWriteQueueMaxSize`: set the number of object at which the write queue is considered full, and the method `writeQueueFull` returns `true`. Note that, when the write queue is considered full, if write is called the data will still be accepted and queued. The actual number depends on the stream implementation, for `Buffer` the size represents the actual number of bytes written and not the number of buffers.

- `writeQueueFull`: returns `true` if the write queue is considered full.

- `exceptionHandler`: Will be called if an exception occurs on the `WriteStream`.

- `drainHandler`: The handler will be called if the `WriteStream` is considered no longer full.

### Pump

Instances of Pump have the following methods:

- `start`: Start the pump.

- `stop`: Stops the pump. When the pump starts it is in stopped mode.

- `setWriteQueueMaxSize`: This has the same meaning as `setWriteQueueMaxSize` on the `WriteStream`.

A pump can be started and stopped multiple times.

When a pump is first created it is not started. You need to call the `start()` method to start it.

## Record Parser

The record parser allows you to easily parse protocols which are delimited by a sequence of bytes, or fixed size records.

It transforms an sequence of input buffer to a sequence of buffer structured as configured (either fixed size or separated records).

For example, if you have a simple ASCII text protocol delimited by '\n' and the input is the following:

    buffer1:HELLO\nHOW ARE Y
    buffer2:OU?\nI AM
    buffer3: DOING OK
    buffer4:\n

The record parser would produce

    buffer1:HELLO
    buffer2:HOW ARE YOU?
    buffer3:I AM DOING OK

Let’s see the associated code:

    var RecordParser = require("vertx-js/record_parser");
    var Buffer = require("vertx-js/buffer");
    var parser = RecordParser.newDelimited("\n", function (h) {
      console.log(h.toString());
    });

    parser.handle(Buffer.buffer("HELLO\nHOW ARE Y"));
    parser.handle(Buffer.buffer("OU?\nI AM"));
    parser.handle(Buffer.buffer("DOING OK"));
    parser.handle(Buffer.buffer("\n"));

You can also produce fixed sized chunks as follows:

    var RecordParser = require("vertx-js/record_parser");
    RecordParser.newFixed(4, function (h) {
      console.log(h.toString());
    });

For more details, check out the `RecordParser` class.

## Thread safety

Most Vert.x objects are safe to access from different threads. However performance is optimised when they are accessed from the same context they were created from.

For example if you have deployed a verticle which creates a `NetServer` which provides `NetSocket` instances in it’s handler, then it’s best to always access that socket instance from the event loop of the verticle.

If you stick to the standard Vert.x verticle deployment model and avoid sharing objects between verticles then this should be the case without you having to think about it.

## Metrics SPI

By default Vert.x does not record any metrics. Instead it provides an SPI for others to implement which can be added to the classpath. The metrics SPI is an advanced feature which allows implementers to capture events from Vert.x in order to gather metrics. For more information on this, please consult the `API Documentation`.

## OSGi

Vert.x Core is packaged as an OSGi bundle, so can be used in any OSGi R4.2+ environment such as Apache Felix or Eclipse Equinox. The bundle exports `io.vertx.core*`.

However, the bundle has some dependencies on Jackson and Netty. To get the vert.x core bundle resolved deploy:

- Jackson Annotation [2.5.0,3)

- Jackson Core [2.5.0,3)

- Jackson Databind [2.5.0,3)

- Netty Buffer [4.0.27,5)

- Netty Codec [4.0.27,5)

- Netty Codec/Socks [4.0.27,5)

- Netty Codec/Common [4.0.27,5)

- Netty Codec/Handler [4.0.27,5)

- Netty Codec/Transport [4.0.27,5)

Here is a working deployment on Apache Felix 4.6.1:

      14|Active     |    1|Jackson-annotations (2.5.3)
      15|Active     |    1|Jackson-core (2.5.3)
      16|Active     |    1|jackson-databind (2.5.3)
      17|Active     |    1|Netty/Buffer (4.0.27.Final)
      18|Active     |    1|Netty/Codec (4.0.27.Final)
      19|Active     |    1|Netty/Codec/HTTP (4.0.27.Final)
      20|Active     |    1|Netty/Codec/Socks (4.0.27.Final)
      21|Active     |    1|Netty/Common (4.0.27.Final)
      22|Active     |    1|Netty/Handler (4.0.27.Final)
      23|Active     |    1|Netty/Transport (4.0.27.Final)
      25|Active     |    1|Vert.x Core (3.0.0.SNAPSHOT)

## The 'vertx' command line

The `vertx` command is used to interact with Vert.x from the command line. It’s main use is to run Vert.x verticles. To do this you need to download and install a Vert.x distribution, and add the `bin` directory of the installation to your `PATH` environment variable. Also make sure you have a Java 8 JDK on your `PATH`.

Note | The JDK is required to support on the fly compilation of Java code.

### Run verticles

You can run raw Vert.x verticles directly from the command line using `vertx run`. Here is a couple of examples of the `run` command:

    vertx run my-verticle.js                                 (1)
    vertx run my-verticle.groovy                             (2)
    vertx run my-verticle.rb                                 (3)

    vertx run io.vertx.example.MyVerticle                    (4)
    vertx run io.vertx.example.MVerticle -cp my-verticle.jar (5)

    vertx run MyVerticle.java                                (6)

1. Deploys a JavaScript verticle

2. Deploys a Groovy verticle

3. Deploys a Ruby verticle

4. Deploys an already compiled Java verticle. Classpath root is the current directory

5. Deploys a verticle packaged in a Jar, the jar need to be in the classpath

6. Compiles the Java source and deploys it

As you can see in the case of Java, the name can either be the fully qualified class name of the verticle, or you can specify the Java Source file directly and Vert.x compiles it for you.

You can also prefix the verticle with the name of the language implementation to use. For example if the verticle is a compiled Groovy class, you prefix it with `groovy:` so that Vert.x knows it’s a Groovy class not a Java class.

    vertx run groovy:io.vertx.example.MyGroovyVerticle

The `vertx run` command can take a few optional parameters, they are:

- `-conf <config_file>` - Provides some configuration to the verticle. `config_file` is the name of a text file containing a JSON object that represents the configuration for the verticle. This is optional.

- `-cp <path>` - The path on which to search for the verticle and any other resources used by the verticle. This defaults to `.` (current directory). If your verticle references other scripts, classes or other resources (e.g. jar files) then make sure these are on this path. The path can contain multiple path entries separated by `:` (colon) or `;` (semi-colon) depending on the operating system. Each path entry can be an absolute or relative path to a directory containing scripts, or absolute or relative filenames for jar or zip files. An example path might be `-cp classes:lib/otherscripts:jars/myjar.jar:jars/otherjar.jar`. Always use the path to reference any resources that your verticle requires. Do not put them on the system classpath as this can cause isolation issues between deployed verticles.

- `-instances <instances>` - The number of instances of the verticle to instantiate. Each verticle instance is strictly single threaded so to scale your application across available cores you might want to deploy more than one instance. If omitted a single instance will be deployed.

- `-worker` - This option determines whether the verticle is a worker verticle or not.

- `-cluster` - This option determines whether the Vert.x instance will attempt to form a cluster with other Vert.x instances on the network. Clustering Vert.x instances allows Vert.x to form a distributed event bus with other nodes. Default is `false` (not clustered).

- `-cluster-port` - If the cluster option has also been specified then this determines which port will be used for cluster communication with other Vert.x instances. Default is `0` - which means 'choose a free random port'. You don’t usually need to specify this parameter unless you really need to bind to a specific port.

- `-cluster-host` - If the cluster option has also been specified then this determines which host address will be used for cluster communication with other Vert.x instances. By default it will try and pick one from the available interfaces. If you have more than one interface and you want to use a specific one, specify it here.

- `-ha` - if specified the verticle will be deployed as high availability (HA) deployment. See related section for more details

- `-quorum` - used in conjunction with `-ha`. It specifies the minimum number of nodes in the cluster for any HA deploymentIDs to be active. Defaults to 0.

- `-hagroup` - used in conjunction with `-ha`. It specifies the HA group this node will join. There can be multiple HA groups in a cluster. Nodes will only failover to other nodes in the same group. The default value is ` __DEFAULT__`

You can also set system properties using: `-Dkey=value`.

Here are some more examples:

Run a JavaScript verticle server.js with default settings

    vertx run server.js

Run 10 instances of a pre-compiled Java verticle specifying classpath

    vertx run com.acme.MyVerticle -cp "classes:lib/myjar.jar" -instances 10

Run 10 instances of a Java verticle by source file

    vertx run MyVerticle.java -instances 10

Run 20 instances of a ruby worker verticle

    vertx run order_worker.rb -instances 20 -worker

Run two JavaScript verticles on the same machine and let them cluster together with each other and any other servers on the network

    vertx run handler.js -cluster
    vertx run sender.js -cluster

Run a Ruby verticle passing it some config

    vertx run my_verticle.rb -conf my_verticle.conf

Where `my_verticle.conf` might contain something like:

    {
     "name": "foo",
     "num_widgets": 46
    }

The config will be available inside the verticle via the core API.

When using the high-availability feature of vert.x you may want to create a bare instance of vert.x. This instance does not deploy any verticles when launched, but will receive a verticle if another node of the cluster dies. To create a bare instance, launch:

    vertx -ha

Depending on your cluster configuration, you may have to append the `cluster-host` and `cluster-port` parameters.

### Executing a Vert.x application packaged as a fat jar

A fat jar is an executable jar embedding its dependencies. This means you don’t have to have Vert.x pre-installed on the machine on which you execute the jar. Like any executable Java jar it can be executed with.

    java -jar my-application-fat.jar

There is nothing really Vert.x specific about this, you could do this with any Java application

You can either create your own main class and specify that in the manifest, but it’s recommended that you write your code as verticles and use the Vert.x `Launcher` class (`io.vertx.core.Launcher`) as your main class. This is the same main class used when running Vert.x at the command line and therefore allows you to specify command line arguments, such as `-instances` in order to scale your application more easily.

To deploy your verticle in a fatjar like this you must have a manifest with:

- `Main-Class` set to `io.vertx.core.Launcher`

- `Main-Verticle` specifying the main verticle (fully qualified class name or script file name)

You can also provide the usual command line arguments that you would pass to `vertx run`:

    java -jar my-verticle-fat.jar -cluster -conf myconf.json
    java -jar my-verticle-fat.jar -cluster -conf myconf.json -cp path/to/dir/conf/cluster_xml

Note | Please consult the Maven/Gradle simplest and Maven/Gradle verticle examples in the examples repository for examples of building applications as fatjars.

A fat jar executes the `run` command, by default.

### Displaying version of Vert.x

To display the vert.x version, just launch:

    vertx version

### Other commands

The `vertx` command line and the `Launcher` also provide other commands in addition to `run` and `version`:

You can create a `bare` instance using:

    vertx bare
    # or
    java -jar my-verticle-fat.jar bare

You can also start an application in background using:

    java -jar my-verticle-fat.jar start -Dvertx-id=my-app-name

If `my-app-name` is not set, a random id will be generated, and printed on the command prompt. You can pass `run` options to the `start` command:

    java -jar my-verticle-fat.jar start -Dvertx-id=my-app-name -cluster

Once launched in background, you can stop it with the `stop` command:

    java -jar my-verticle-fat.jar stop my-app-name

You can also list the vert.x application launched in background using:

    java -jar my-verticle-fat.jar list

The `start`, `stop` and `list` command are also available from the `vertx` tool. The start` command supports a couple of options:

- `vertx-id` : the application id, uses a random UUID if not set

- `java-opts` : the Java Virtual Machine options, uses the `JAVA_OPTS` environment variable if not set.

- `redirect-output` : redirect the spawned process output and error streams to the parent process streams.

      If option values contain spaces, don't forget to wrap the value between `""` (double-quotes).

The set of commands is extensible, refer to the Extending the vert.x Launcher section.

### Live Redeploy

When developing it may be convenient to automatically redeploy your application upon file changes. The `vertx` command line tool and more generally the `Launcher` class offers this feature. Here are some examples:

    vertx run MyVerticle.groovy --redeploy="**/*.groovy"
    vertx run MyVerticle.groovy --redeploy="**/*.groovy,**/*.rb"
    java io.vertx.core.Launcher run org.acme.MyVerticle –redeploy="**/*.class" -cp ...

The redeployment process is implemented as follows. First your application is launched as a background application (with the `start` command). On matching file changes, the process is stopped and the application is restarted. This way avoids leaks.

To enable the live redeploy, pass the `--redeploy` option to the `run` command. The `--redeploy` indicates the set of file to watch. This set can use Ant-style patterns (with `**`, `*` and `?`). You can specify several sets by separating them using a comma (`,`). Patterns are relative to the current working directory.

Parameters passed to the `run` command are passed to the application. Java Virtual Machine options can be configured using `--java-opts`.

The redeploy feature can be used in your IDE:

- Eclipse - create a Run configuration, using the `io.vertx.core.Launcher` class a main class. In the Program arguments area (in the Arguments tab), write `run your-verticle-fully-qualified-name --redeploy=**/*.java`. You can also add other parameters. The redeployment works smoothly as Eclipse incrementally compiles your files on save.

- IntelliJ - create a Run configuration (Application), set the Main class to `io.vertx.core.Launcher`. In the Program arguments write: `run your-verticle-fully-qualified-name --redeploy=**/*.class --launcher-class=io.vertx.core.Launcher`. As you can see, you need to set the `launcher-class` parameter because IntelliJ wraps your main class in its own class. To trigger the redeployment, you need to make the project or the module explicitly (Build menu → Make project).

To debug your application, create your run configuration as a remote application and configure the debugger using `--java-opts`. However, don’t forget to re-plug the debugger after every redeployment as a new process is created every time.

You can also hook your build process in the redeploy cycle:

    java –jar target/my-fat-jar.jar –redeploy="**/*.java" --on-redeploy="mvn package"

The "on-redeploy" option specifies a command invoked after the shutdown of the application and before the restart. So you can hook your build tool if it updates some runtime artifacts. For instance, you can launch `gulp` or `grunt` to update your resources.

## Cluster Managers

In Vert.x a cluster manager is used for various functions including:

- Discovery and group membership of Vert.x nodes in a cluster

- Maintaining cluster wide topic subscriber lists (so we know which nodes are interested in which event bus addresses)

- Distributed Map support

- Distributed Locks

- Distributed Counters

Cluster managers do not handle the event bus inter-node transport, this is done directly by Vert.x with TCP connections.

The default cluster manager used in the Vert.x distributions is one that uses Hazelcast but this can be easily replaced by a different implementation as Vert.x cluster managers are pluggable.

A cluster manager must implement the interface `ClusterManager`. Vert.x locates cluster managers at run-time by using the Java Service Loader functionality to locate instances of `ClusterManager` on the classpath.

If you are using Vert.x at the command line and you want to use clustering you should make sure the `lib` directory of the Vert.x installation contains your cluster manager jar.

If you are using Vert.x from a Maven or Gradle project just add the cluster manager jar as a dependency of your project.

You can also specify cluster managers programmatically if embedding Vert.x using `clusterManager`.

## Logging

Vert.x logs using it’s in-built logging API. The default implementation uses the JDK (JUL) logging so no extra logging dependencies are needed.

### Configuring JUL logging

A JUL logging configuration file can be specified in the normal JUL way by providing a system property called: `java.util.logging.config.file` with the value being your configuration file. For more information on this and the structure of a JUL config file please consult the JUL logging documentation.

Vert.x also provides a slightly more convenient way to specify a configuration file without having to set a system property. Just provide a JUL config file with the name `vertx-default-jul-logging.properties` on your classpath (e.g. inside your fatjar) and Vert.x will use that to configure JUL.

### Using another logging framework

If you don’t want Vert.x to use JUL for it’s own logging you can configure it to use another logging framework, e.g. Log4J or SLF4J.

To do this you should set a system property called `vertx.logger-delegate-factory-class-name` with the name of a Java class which implements the interface `LoggerFactory`. We provide pre-built implementations for Log4J and SLF4J with the class names `io.vertx.core.logging.Log4jLogDelegateFactory` and `io.vertx.core.logging.SLF4JLogDelegateFactory` respectively. If you want to use these implementations you should also make sure the relevant Log4J or SLF4J jars are on your classpath.

### Logging from your application

Vert.x itself is just a library and you can use whatever logging library you prefer to log from your own application, using that logging library’s API.

However, if you prefer you can use the Vert.x logging facility as described above to provide logging for your application too.

To do that you use `LoggerFactory` to get an instance of `Logger` which you then use for logging, e.g.

    // Note -these classes are Java only

    // You would normally maintain one static instance of Logger per Java class:

    var logger = Java.type("io.vertx.core.logging.LoggerFactory").getLogger(className);

    logger.info("something happened");
    logger.error("oops!", exception);

## High Availability and Fail-Over

Vert.x allows you to run your verticles with high availability (HA) support. In that case, when a vert.x instance running a verticle dies abruptly, the verticle is migrated to another vertx instance. The vert.x instances must be in the same cluster.

### Automatic failover

When vert.x runs with HA enabled, if a vert.x instance where a verticle runs fails or dies, the verticle is redeployed automatically on another vert.x instance of the cluster. We call this verticle fail-over.

To run vert.x with the HA enabled, just add the `-ha` flag to the command line:

    vertx run my-verticle.js -ha

Now for HA to work, you need more than one Vert.x instances in the cluster, so let’s say you have another Vert.x instance that you have already started, for example:

    vertx run my-other-verticle.js -ha

If the Vert.x instance that is running `my-verticle.js` now dies (you can test this by killing the process with `kill -9`), the Vert.x instance that is running `my-other-verticle.js` will automatic deploy `my-verticle .js` so now that Vert.x instance is running both verticles.

Note | the migration is only possible if the second vert.x instance has access to the verticle file (here `my-verticle.js`).

Important | Please note that cleanly closing a Vert.x instance will not cause failover to occur, e.g. `CTRL-C` or `kill -SIGINT`

You can also start bare Vert.x instances - i.e. instances that are not initially running any verticles, they will also failover for nodes in the cluster. To start a bare instance you simply do:

    vertx run -ha

When using the `-ha` switch you do not need to provide the `-cluster` switch, as a cluster is assumed if you want HA.

Note | depending on your cluster configuration, you may need to customize the cluster manager configuration (Hazelcast by default), and/or add the `cluster-host` and `cluster-port` parameters.

### HA groups

When running a Vert.x instance with HA you can also optional specify a HA group. A HA group denotes a logical group of nodes in the cluster. Only nodes with the same HA group will failover onto one another. If you don’t specify a HA group the default group `__DEFAULT__` is used.

To specify an HA group you use the `-hagroup` switch when running the verticle, e.g.

    vertx run my-verticle.js -ha -ha-group my-group

Let’s look at an example:

In a first terminal:

    vertx run my-verticle.js -ha -hagroup g1

In a second terminal, let’s run another verticle using the same group:

    vertx run my-other-verticle.js -ha -hagroup g1

Finally, in a third terminal, launch another verticle using a different group:

    vertx run yet-another-verticle.js -ha -hagroup g2

If we kill the instance in terminal 1, it will fail over to the instance in terminal 2, not the instance in terminal 3 as that has a different group.

If we kill the instance in terminal 3, it won’t get failed over as there is no other vert.x instance in that group.

### Dealing with network partitions - Quora

The HA implementation also supports quora. A quorum is the minimum number of votes that a distributed transaction has to obtain in order to be allowed to perform an operation in a distributed system.

When starting a Vert.x instance you can instruct it that it requires a `quorum` before any HA deployments will be deployed. In this context, a quorum is a minimum number of nodes for a particular group in the cluster. Typically you chose your quorum size to `Q = 1 + N/2` where `N` is the number of nodes in the group. If there are less than `Q` nodes in the cluster the HA deployments will undeploy. They will redeploy again if/when a quorum is re-attained. By doing this you can prevent against network partitions, a.k.a. split brain.

There is more information on quora here.

To run vert.x instances with a quorum you specify `-quorum` on the command line, e.g.

In a first terminal:

    vertx run my-verticle.js -ha -quorum 3

At this point the Vert.x instance will start but not deploy the module (yet) because there is only one node in the cluster, not 3.

In a second terminal:

    vertx run my-other-verticle.js -ha -quorum 3

At this point the Vert.x instance will start but not deploy the module (yet) because there are only two nodes in the cluster, not 3.

In a third console, you can start another instance of vert.x:

    vertx run yet-another-verticle.js -ha -quorum 3

Yay! - we have three nodes, that’s a quorum. At this point the modules will automatically deploy on all instances.

If we now close or kill one of the nodes the modules will automatically undeploy on the other nodes, as there is no longer a quorum.

Quora can also be used in conjunction with ha groups. In that case, quora are resolved for each particular group.

## Security notes

Vert.x is a toolkit, not an opinionated framework where we force you to do things in a certain way. This gives you great power as a developer but with that comes great responsibility.

As with any toolkit, it’s possible to write insecure applications, so you should always be careful when developing your application especially if it’s exposed to the public (e.g. over the internet).

### Web applications

If writing a web application it’s highly recommended that you use Vert.x-Web instead of Vert.x core directly for serving resources and handling file uploads.

Vert.x-Web normalises the path in requests to prevent malicious clients from crafting URLs to access resources outside of the web root.

Similarly for file uploads Vert.x-Web provides functionality for uploading to a known place on disk and does not rely on the filename provided by the client in the upload which could be crafted to upload to a different place on disk.

Vert.x core itself does not provide such checks so it would be up to you as a developer to implement them yourself.

### Clustered event bus traffic

When clustering the event bus between different Vert.x nodes on a network, the traffic is sent un-encrypted across the wire, so do not use this if you have confidential data to send and your Vert.x nodes are not on a trusted network.

### Standard security best practices

Any service can have potentially vulnerabilities whether it’s written using Vert.x or any other toolkit so always follow security best practice, especially if your service is public facing.

For example you should always run them in a DMZ and with an user account that has limited rights in order to limit the extent of damage in case the service was compromised.

## Vert.x Command Line Interface API

Vert.x Core provides an API for parsing command line arguments passed to programs.

It’s also able to print help messages detailing the options available for a command line tool. Even if such features are far from the Vert.x core topics, this API is used in the `Launcher` class that you can use in fat-jar and in the `vertx` command line tools. In addition, it’s polyglot (can be used from any supported language) and is used in Vert.x Shell.

Vert.x CLI provides a model to describe your command line interface, but also a parser. This parser supports different types of syntax:

- POSIX like options (ie. `tar -zxvf foo.tar.gz`)

- GNU like long options (ie. `du --human-readable --max-depth=1`)

- Java like properties (ie. `java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo`)

- Short options with value attached (ie. `gcc -O2 foo.c`)

- Long options with single hyphen (ie. `ant -projecthelp`)

Using the CLI api is a 3-steps process:

1. The definition of the command line interface

2. The parsing of the user command line

3. The query / interrogation

### Definition Stage

Each command line interface must define the set of options and arguments that will be used. It also requires a name. The CLI API uses the `Option` and `Argument` classes to describe options and arguments:

    var CLI = require("vertx-js/cli");
    var cli = CLI.create("copy").setSummary("A command line interface to copy files.").addOption({
      "longName" : "directory",
      "shortName" : "R",
      "description" : "enables directory support",
      "flag" : true
    }).addArgument({
      "index" : 0,
      "description" : "The source",
      "argName" : "source"
    }).addArgument({
      "index" : 0,
      "description" : "The destination",
      "argName" : "target"
    });

As you can see, you can create a new `CLI` using `CLI.create`. The passed string is the name of the CLI. Once created you can set the summary and description. The summary is intended to be short (one line), while the description can contain more details. Each option and argument are also added on the `CLI` object using the `addArgument` and `addOption` methods.

#### Options

An `Option` is a command line parameter identified by a key present in the user command line. Options must have at least a long name or a short name. Long name are generally used using a `--` prefix, while short names are used with a single `-`. Options can get a description displayed in the usage (see below). Options can receive 0, 1 or several values. An option receiving 0 values is a `flag`, and must be declared using `flag`. By default, options receive a single value, however, you can configure the option to receive several values using `multiValued`:

    var CLI = require("vertx-js/cli");
    var cli = CLI.create("some-name").setSummary("A command line interface illustrating the options valuation.").addOption({
      "longName" : "flag",
      "shortName" : "f",
      "flag" : true,
      "description" : "a flag"
    }).addOption({
      "longName" : "single",
      "shortName" : "s",
      "description" : "a single-valued option"
    }).addOption({
      "longName" : "multiple",
      "shortName" : "m",
      "multiValued" : true,
      "description" : "a multi-valued option"
    });

Options can be marked as mandatory. A mandatory option not set in the user command line throws an exception during the parsing:

    var CLI = require("vertx-js/cli");
    var cli = CLI.create("some-name").addOption({
      "longName" : "mandatory",
      "required" : true,
      "description" : "a mandatory option"
    });

Non-mandatory options can have a default value. This value would be used if the user does not set the option in the command line:

    var CLI = require("vertx-js/cli");
    var cli = CLI.create("some-name").addOption({
      "longName" : "optional",
      "defaultValue" : "hello",
      "description" : "an optional option with a default value"
    });

An option can be hidden using the `hidden` method. Hidden option are not listed in the usage, but can still be used in the user command line (for power-users).

Options can also be instantiated from their JSON form.

#### Arguments

Unlike options, arguments do not have a key and are identified by their index. For example, in `java com.acme.Foo`, `com.acme.Foo` is an argument.

Arguments do not have a name, there are identified using a mandatory 0-based index. The first parameter has the index `0`:

    var CLI = require("vertx-js/cli");
    var cli = CLI.create("some-name").addArgument({
      "index" : 0,
      "description" : "the first argument",
      "argName" : "arg1"
    }).addArgument({
      "index" : 1,
      "description" : "the second argument",
      "argName" : "arg2"
    });

The `argName` is optional and used in the usage message.

As options, `Argument` can:

- be hidden using `hidden`

- be mandatory using `required`

- have a default value using `defaultValue`

Arguments can also be instantiated from their JSON form.

#### Usage generation

Once your `CLI` instance is configured, you can generate the usage message:

    var CLI = require("vertx-js/cli");
    var cli = CLI.create("copy").setSummary("A command line interface to copy files.").addOption({
      "longName" : "directory",
      "shortName" : "R",
      "description" : "enables directory support",
      "flag" : true
    }).addArgument({
      "index" : 0,
      "description" : "The source",
      "argName" : "source"
    }).addArgument({
      "index" : 0,
      "description" : "The destination",
      "argName" : "target"
    });

    var builder = new (Java.type("java.lang.StringBuilder"))();
    cli.usage(builder);

It generates an usage message like this one:

    Usage: copy [-R] source target

    A command line interface to copy files.

      -R,--directory   enables directory support

If you need to tune the usage message, check the `UsageMessageFormatter` class.

### Parsing Stage

Once your `CLI` instance is configured, you can parse the user command line to evaluate each option and argument:

    var commandLine = cli.parse(userCommandLineArguments);

The `parse` method returns a `CommandLine` object containing the values. By default, it validates the user command line and checks that each mandatory options and arguments have been set as well as the number of values received by each option. You can disable the validation by passing `false` as second parameter of `parse`. This is useful if you want to check an argument or option is present even if the parsed command line is invalid.

### Query / Interrogation Stage

Once parsed, you can retrieve the values of the options and arguments from the `CommandLine` object returned by the `parse` method:

    var commandLine = cli.parse(userCommandLineArguments);
    var opt = commandLine.getOptionValue("my-option");
    var flag = commandLine.isFlagEnabled("my-flag");
    var arg0 = commandLine.getArgumentValue(0);

## The vert.x Launcher

The vert.x `Launcher` is used in fat jar as main class, and by the `vertx` command line utility. It executes a set of commands such as run, bare, start…​

### Extending the vert.x Launcher

You can extend the set of command by implementing your own `Command` (in Java only):

    @Name("my-command")
    @Summary("A simple hello command.")
    public class MyCommand extends DefaultCommand {

      private String name;

      @Option(longName = "name", required = true)
      public void setName(String n) {
        this.name = n;
      }

      @Override
      public void run() throws CLIException {
        System.out.println("Hello " + name);
      }
    }

Place the jar containing the command into the classpath of your fat-jar (or include it inside) or in the `lib` directory of your vert.x distribution, and you would be able to execute:

    vertx hello vert.x
    java -jar my-fat-jar.jar hello vert.x

### Using the Launcher in fat jars

To use the `Launcher` class in a fat-jar just set the `Main-Class` of the MANIFEST to `io.vertx.core.Launcher`. In addition, set the `Main-Verticle` MANIFEST entry to the name of your main verticle.

By default, it executed the `run` command. However, you can configure the default command by setting the `Main-Command` MANIFEST entry. The default command is used if the fat jar is launched without a command.

### Sub-classing the Launcher

You can also create a sub-class of `Launcher` to start your application. The class has been designed to be easily extensible.