Thursday, 14 December 2017

First steps with Vert.x and Infinispan - PUSH API (Part 2)

Welcome to the second in a multi-part series of blog posts about creating Eclipse Vert.x applications with Infinispan. In the previous blog post we have seen how to create a REST API. The purpose of this tutorial is to showcase how to create a PUSH API implemented with Vert.x and using Infinispan as a server.
All the code of this tutorial is available in this GitHub repository. The backend is a Java project using Maven, so all the needed dependencies can be found in the pom.xml. The front is a super simple react application.


PUSH API

Creating a REST API is very straightforward. But today, even if we are heavily using REST, we don't always want to use request/response or polling, but instead we want to push directly from the server to the client. In this example, we are going to create an API that pushes every new value inserted in the default cache of Infinispan. These values are cute names, as we did in the REST API example.

We are using two features here :
  • Infinispan client listeners
  • Vert.x bridge between the Event Bus and the browser
Infinispan Listeners provide a way to the client get notified when something happens in a cache.

The Event Bus Bridge that connects to the browser, uses SockJS. SockJS is a JavaScript library that provides a WebSocket API, but it can be used with browsers that don't support web-sockets (see the website of the project for more detailed information). Vert.x supports this library and creates a bridge between your browser and your back-end easily through the Event Bus.

Creating an Event Bus bridge


Vert.x is a reactive framework, which means that uses RxJava too, and provides a fancy API on top of it.

First, we are going to create a new verticle called SendCuteNamesAPI. This verticle extends the CacheAccessVerticle we created in the previous blog postCacheAccessVerticle initialises the connection with Infinispan using the Hot Rod protocol.

Now we need to create a SocketJSHandler. This handler has a method called bridge, where we configure some BridgeOptions. Obviously we don't want the client to be able to read everything traveling on the event bus, and this won't happen. We configure an address, 'cute-names', and we add the permission to read and write to this address.

This handler is passed to the event bus route, where the path is /eventbus/*.

Finally, we create a http server as we did in the REST API example. The difference is that instead of calling listen method, we call rxListen and subscribe.


public class SendCuteNamesAPI extends CacheAccessVerticle {
public static final String CUTE_NAMES_ADDRESS = "cute-names";
private final Logger logger = Logger.getLogger(SendCuteNamesAPI.class.getName());
@Override
protected void initSuccess(Future<Void> startFuture) {
logger.info("Starting SendCuteNamesAPI");
Router router = Router.router(vertx);
router.get("/").handler(rc -> {
rc.response().putHeader("content-type", "text/html")
.end("Welcome to Send Cute Names API Service");
});
SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
BridgeOptions options = new BridgeOptions();
options.addOutboundPermitted(new PermittedOptions().setAddress(CUTE_NAMES_ADDRESS));
sockJSHandler.bridge(options);
router.route("/eventbus/*").handler(sockJSHandler);
vertx.createHttpServer()
.requestHandler(router::accept)
.rxListen(config().getInteger("http.port", 8080))
.doOnSuccess(server -> logger.info("HTTP server started"))
.doOnError(t -> logger.log(Level.SEVERE, "HTTP server failed to start", t))
.toCompletable()
.subscribe(CompletableHelper.toObserver(startFuture));
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
DeploymentOptions options = new DeploymentOptions()
.setConfig(new JsonObject()
.put("http.port", 8082)
.put("infinispan.host", "localhost")
);
vertx.deployVerticle(SendCuteNamesAPI.class.getName(), options);
}
}

Getting notified and publishing


Using Infinispan listeners is very easy.

First, we are going to create a class that has the @ClientListener annotation. The client listener has to be added to the cache client configuration. We add a protected method called addConfigToCache that will be called just after the initialisation of the defaultCache in the abstract CacheAccessVerticle. Verticles extending the abstract class can now add custom configuration to the client.

We want to be notified when a new entry is created. In this case, our listener has to contain a method with the @ClientCacheEntryCreated annotation on it. The signature of the method has to include a ClientCacheEntryCreatedEvent<String> parameter. This parameter will hold the 'key' of the entry that has been created.

Finally, we use the key to retrieve the name using the getAsync method and then publish the value in the Vert.x event bus to the address where the socket listener is permitted to readcute-names.

public class SendCuteNamesAPI extends CacheAccessVerticle {
public static final String CUTE_NAMES_ADDRESS = "cute-names";
...
@Override
protected void addConfigToCache(RemoteCache<String, String> cache) {
logger.info("Added cute names listener");
cache.addClientListener(new CuteNamesListener());
}
@ClientListener
public final class CuteNamesListener {
@ClientCacheEntryCreated
@SuppressWarnings("unused")
public void created(ClientCacheEntryCreatedEvent<String> e) {
defaultCache.getAsync(e.getKey()).thenAccept(cuteName -> {
logger.log(Level.INFO, "publish cute name " + cuteName);
vertx.eventBus().publish(CUTE_NAMES_ADDRESS, cuteName);
});
}
}
}

Now we can run the main method and whenever we post a new name, we will see in the logs that the client listener is notified!

=====
Dec 08, 2017 12:20:53 PM org.infinispan.client.hotrod.RemoteCacheManager start
INFO: ISPN004021: Infinispan version: 9.1.1.Final
Dec 08, 2017 12:20:53 PM cutenames.CacheAccessVerticle lambda$start$1
INFO: Cache connection successfully done
Dec 08, 2017 12:20:53 PM cutenames.CuteNamesRestAPI initSuccess
INFO: Starting CuteNamesRestAPI in localhost:8081
Dec 08, 2017 12:21:22 PM cutenames.CuteNamesRestAPI handleAddCuteName
INFO: Add called
Dec 08, 2017 12:21:22 PM cutenames.CuteNamesRestAPI lambda$handleAddCuteName$2
INFO: Cute name added [42]
=====
INFO: ISPN004021: Infinispan version: 9.1.1.Final
Dec 08, 2017 12:20:57 PM cutenames.SendCuteNamesAPI addConfigToCache
INFO: Added cute names listener
Dec 08, 2017 12:20:57 PM cutenames.CacheAccessVerticle lambda$start$1
INFO: Cache connection successfully done
Dec 08, 2017 12:20:57 PM cutenames.SendCuteNamesAPI initSuccess
INFO: Starting SendCuteNamesAPI
Dec 08, 2017 12:20:57 PM cutenames.SendCuteNamesAPI lambda$initSuccess$1
INFO: HTTP server started
Dec 08, 2017 12:21:22 PM cutenames.SendCuteNamesAPI$CuteNamesListener lambda$created$0
INFO: Publish name Oihana
=====
view raw logs.txt hosted with ❤ by GitHub



Client code


We are going to create a super simple react application that will just display hello. React community is *huge*, so there are lot's of tutorials out there to create a hello world client application. This application has a single component that displays "Hello".

The react application runs calling npm install and npm start  in http://localhost:9000/.

Now we need to connect the client to the backend with SockJS. Vert.x provides a JavaScript library for that: vertx3-eventbus-client, built on top of SockJS. We create an EventBus object that will connect to http://localhost:8082/eventbus as we configured in the SendCuteNamesAPI. We register a handler on the 'cute-names' address. The body of the message will contain the new cute name published in the event bus. Every time the handler is called, we update the component's state, and it will be rendered.


import React from 'react'
import EventBus from 'vertx3-eventbus-client'
class CuteNames extends React.Component {
constructor(props) {
super(props);
const eventBus = new EventBus("http://localhost:8082/eventbus");
var _this = this;
eventBus.enableReconnect(true);
eventBus.onopen = function () {
eventBus.registerHandler('cute-names', function (error, message) {
if (error === null) {
console.info(message.body);
_this.setState({name:message.body});
} else {
console.error(error, 'cute-names');
}
});
};
this.state = {
name: 'no name'
};
}
render() {
return (
<div className='cute'>
<h1>Cute name</h1>
<p>{this.state.name}</p>
</div>
);
}
}
export default CuteNames
view raw CuteNames.js hosted with ❤ by GitHub

Wrap up

We have learned how to create PUSH APIs with Vert.x, powered by Infinispan. The repository has some unit tests. Feedback is more than welcome to improve the code and the provided examples. I hope you enjoyed this tutorial ! On the next tutorials we will talk about Infinispan as the cluster manager for Vert.x. Stay tuned !


No comments:

Post a Comment