GithubHelp home page GithubHelp logo

bertranda / vertx-service-proxy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vert-x3/vertx-service-proxy

0.0 2.0 0.0 362 KB

EventBus Proxy generation

License: Apache License 2.0

Java 79.70% JavaScript 20.30%

vertx-service-proxy's Introduction

Service Proxies

Build Status

The documentation is available here.

Many Vert.x applications include various services which do useful things and often can be reused from one application to another. An example would be a database service.

Usually those services run in their own verticle and interact with other verticles by receiving and sending messages, e.g.

// Assume database service is already deployed somewhere....

// Save some data in the database

JsonObject message = new JsonObject();
message.putString("collection", "mycollection");
message.putObject("document", new JsonObject().putString("name", "tim"));
DeliveryOptions options = new DeliveryOptions().addHeader("action", "save");
vertx.eventBus().send("database-service-address", message, options, res2 -> {
    if (res2.succeeded()) {
        // done
    }
}

When creating a service there's a certain amount of boiler-plate code to listen on the eventbus for incoming messages, route them to the appropriate method and return results on the event bus.

With Vert.x service proxies, you can avoid writing all that boiler-plate code and concentrate on writing your service.

You write your service as a Java interface and annotate it with the @ProxyGen annotation, for example:

@ProxyGen
public interface SomeDatabaseService {

    // A couple of factory methods to create an instance and a proxy

    static SomeDatabaseService create(Vertx vertx) {
       return new SomeDatabaseServiceImpl(vertx);
    }
    
    static SomeDatabaseService createProxy(Vertx vertx, String address) {
      return new SomeDatabaseServiceVertxEBProxy(vertx, address);
    }
    
    // Actual service operations here...

    void save(String collection, JsonObject document, Handler<AsyncResult<Void>> resultHandler);
}

Given the interface, Vert.x will generate all the boiler-plate required to access your service over the event bus, and it will also generate a client side proxy for your service, so your clients can use a rich idiomatic API for your service instead of having to manually craft event bus messages to send. The client side proxy will work irrespective of where your service actually lives on the event bus (potentially on a different machine).

That means you can interact with your service like this:

// Assume database service is already deployed somewhere....
    
// Create a proxy
SomeDatabaseService service = SomeDatabaseService.createProxy(vertx, "database-service-address");

// Save some data in the database - this time using the proxy
service.save("mycollection", new JsonObject().putString("name", "tim"), res2 -> {
    if (res2.succeeded()) {
        // done
    }
});                

The documentation is available here.

vertx-service-proxy's People

Contributors

vietj avatar cescoffier avatar purplefox avatar pmlopes avatar mchunkytrunk avatar dano avatar nscavell avatar adrianluisgonzalez avatar msavy avatar meggarr avatar sdhays avatar shaunrader avatar

Watchers

James Cloos avatar Antoine Bertrand 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.