Skip to content

Latest commit

 

History

History
102 lines (62 loc) · 3.52 KB

README.adoc

File metadata and controls

102 lines (62 loc) · 3.52 KB

Vert.x Service Proxy examples

Here you will find examples demonstrating Vert.x Service Proxies.

Service Proxies provide a way to expose services on the event bus and reducing the code required to invoke them.

In this example, we will run two verticles:

  • one provides a ProcessorService

  • the seconds one consumes it

Verticles runs on two clustered vert.x instances.

Providing a service

To provide a service, you need a service interface, an implementation and a verticle.

Service interface

The service interface is a Java interface annotated with @ProxyGen.

Compiling this class creates two other classes:

These classes are going to be used in the provider and consumer code

Service Provider

The service provider contains a verticle registering the service handler and an implementation of the service:

The service implementation is a simple Java class implementing the service interface. The only trick to know is the invocation of the given result handler:

resultHandler.handle(Future.succeededFuture(result));

In the verticle, the handler registration is made using:

new ServiceBinder(vertx)
   .setAddress("vertx.processor")
   .register(ProcessorService.class, service);

service is the service object we register (so an instance of the ProcessorServiceImpl. The last parameter is the address on which the service can be invoked.

Alternatively, you can instantiate the generated handler:

new ProcessorServiceVertxProxyHandler(vertx, service).registerHandler("vertx.processor");

In this case, classes need to be generated beforehand, and so, requires a manual mvn compile.

Running the service provider

To run the service provider, execute the ProcessorServiceVerticle in your IDE.

Depending on your cluster configuration you may have to specify the cluster-host and configure Hazelcast.

Client generation

Vert.x generates the clients to use this service in the different supported languages.

Consuming a service

To consume our service, we just need a verticle that runs on a vert.x instance on the same cluster as the service provider.

The service is retrieved using:

ProcessorService service = ProcessorService.createProxy(vertx, "vertx.processor");

The second argument is the service address.

To run the service provider, execute the ConsumerVerticle in your IDE.

Depending on your cluster configuration you may have to specify the cluster-host and configure Hazelcast.

Notice that the verticle has in its classpath the service provider verticle to access the service interface. We could have packaged the service interface in its owned packaged (alongside the generated handler and proxy).

Once launched you should see:

{"name":"vertx","approved":true}

The service can be consumed from Java.