Code Monkey home page Code Monkey logo

vertx-when's Introduction

vertx-when

Provides when.java wrappers for standard vert.x 3 objects to return promises.

Build Status Maven Central

Getting Started

Add a dependency to vertx-when

<dependency>
    <groupId>com.englishtown.vertx</groupId>
    <artifactId>vertx-when</artifactId>
    <version>4.0.0</version>
</dependency>

Dependency Injection

Use dependency injection to get an instance of:

  • com.englishtown.promises.When
  • com.englishtown.vertx.promises.WhenEventBus
  • com.englishtown.vertx.promises.WhenHttpClient
  • com.englishtown.vertx.promises.WhenVertx

HK2 and Guice binders are provided

  • com.englishtown.vertx.promises.hk2.HK2WhenBinder
  • com.englishtown.vertx.promises.guice.GuiceWhenBinder

Manual Dependency Creation

If not using DI, you can manually construct the default implementations like this:

        // Create the vert.x executor for callbacks to run on the vert.x event loop
        VertxExecutor executor = new VertxExecutor(vertx);
        when = WhenFactory.createFor(() -> executor);

        whenVertx = new DefaultWhenVertx(vertx, when);
        whenEventBus = new DefaultWhenEventBus(vertx, when);
        whenHttpClient = new DefaultWhenHttpClient(vertx, when);

(See the com.englishtown.vertx.promises.integration.simple.NoDIIntegrationTest integration test for an example.)

Vert.x 2.x

If running vert.x 2.x, then you should use module vertx-mod-when 3.0.1. See earlier README.md files for details.

<dependency>
    <groupId>com.englishtown</groupId>
    <artifactId>vertx-mod-when</artifactId>
    <version>3.0.1</version>
</dependency>

WhenVertx Examples

Deploy 1 Verticle

whenVertx.deployVerticle("com.englishtown.vertx.TestVerticle")
    .then(deploymentID -> {
        // On success
        return null;
    })
    .otherwise(t -> {
        // On fail
        return null;
    });

Deploy 2 Verticles

List<Promise<String>> promises = new ArrayList<>();

promises.add(vertxWhen.deployVerticle("com.englishtown.vertx.TestVerticle1"));
promises.add(vertxWhen.deployVerticle("com.englishtown.vertx.TestVerticle2"));

when.all(promises)
    .then(deploymentIDs -> {
        // Handle success
        return null;
    })
    .otherwise(t -> {
        // Handle failure
        return null;
    });

WhenEventBus Examples

Send 2 messages

List<Promise<Message<JsonObject>>> promises = new ArrayList<>();

promises.add(whenEventBus.<JsonObject>send("et.vertx.eb.1", new JsonObject().putString("message", "hello")));
promises.add(whenEventBus.<JsonObject>send("et.vertx.eb.2", new JsonObject().putString("message", "world")));

when.all(promises).then(
        replies -> {
            // On success
            return null;
        },
        t -> {
            // On fail
            return null;
        });

WhenHttpClient Examples

Send 2 http requests

List<Promise<HttpClientResponse>> promises = new ArrayList<>();

promises.add(whenHttpClient.requestAbs(HttpMethod.GET, "http://test.englishtown.com/test1", new RequestOptions()));
promises.add(whenHttpClient.requestAbs(HttpMethod.POST, "http://test.englishtown.com/test2", new RequestOptions()));

when.all(promises).then(
        responses -> {
            // On success
            return null;
        },
        t -> {
            // On fail
            return null;
        }
);

Get a response and body

RequestOptions options = new RequestOptions().setPauseResponse(true);
whenHttpClient.requestAbs(HttpMethod.GET, "http://localhost:8081/test", options)
    .then(response -> {
        return whenHttpClient.body(response);
    })
    .then(body -> {
        // Do something with the body
        return null;
    })
    .otherwise(t ->
        // On fail
        return null;
    });

vertx-when's People

Contributors

adrianluisgonzalez avatar johnwarneref avatar mikaelkaron avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.