# 🇬🇧 Use Vert.x with OpenFaaS

That night, I could not sleep, and instead of going round in circles (this is a 🇫🇷 expression, I'm not even sure that exists in 🇬🇧), so, I decided to continue my experiments with OpenFaaS and create a template to use Vert.x (opens new window) with OpenFaaS. Vert.x (opens new window) is my favorite Java Web framework. Vert.x (opens new window) reminds me Node.js a lot (and I'm more a JavaScript guy than a Java guy). You can get this new template here: https://gitlab.com/openfaas-experiments/java-8-vert-x-template (opens new window)

# Create a new function using the Vert.x template

First create a directory for your project:

mkdir vert-x-project
cd vert-x-project

Then, fetch the Java 8 Vert.x (opens new window) template:

faas template pull https://gitlab.com/openfaas-experiments/java-8-vert-x-template 

And finally, create the function:

faas new demo-vert-x --lang java8-vert-x

If you're using the same setup as described in the previous blog post (opens new window), you have to change the demo-vert-x.yml file like that:

provider:
  name: faas
  gateway: http://openfaas.test:8080
functions:
  demo-vert-x:
    lang: java8-vert-x
    handler: ./demo-vert-x
    image: registry.test:5000/hello:latest

Or like that if you use the public Docker hub

provider:
  name: faas
  gateway: http://openfaas.test:8080
functions:
  demo-vert-x:
    lang: java8-vert-x
    handler: ./demo-vert-x
    image: k33g/demo-vert-x:latest

Remark: k33g is my handle on Docker Hub

# Your new Vert.x function

Your new function should look like this:

package com.openfaas.function;

import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.core.json.JsonObject;

public class Handler implements BodyHandler {

  @Override
  public void handle(RoutingContext routingContext) {
    
    routingContext.response()
      .putHeader("content-type", "application/json;charset=UTF-8")
      .end(
        new JsonObject()
          .put("message", "👋 Hello 🌍 World")
          .encodePrettily()
      );
  }

  @Override
  public BodyHandler setBodyLimit(long bodyLimit) {
    return null;
  }

  @Override
  public BodyHandler setUploadsDirectory(String uploadsDirectory) {
    return null;
  }

  @Override
  public BodyHandler setMergeFormAttributes(boolean mergeFormAttributes) {
    return null;
  }

  @Override
  public BodyHandler setDeleteUploadedFilesOnEnd(boolean deleteUploadedFilesOnEnd) {
    return null;
  }

}

# Build an deploy the function

It's easy:

# Build and push to the Docker Hub
faas-cli build -f demo-vert-x.yml
faas-cli push -f demo-vert-x.yml

# Deploy to OpenFaaS
#   - Set the url of the OpenFaaS server
#   - Authenticate
#   - Deploy
export OPENFAAS_URL=http://openfaas.test:8080
echo -n 21d0b92900a5cd99296cd4f8d96b260ec687fd6464a1eb522b6e699ee7896eed | faas-cli login --username=admin --password-stdin

faas-cli deploy -f demo-vert-x.yml

And now, you can use your new function:

echo | faas-cli invoke demo-vert-x

And you should get:

{
  "message" : "👋 Hello 🌍 World"
}

Or directilt in the web administration console of OpenFaaS

text

That's all for this 🌧 day 😃

Have a good Sunday 👋

Last Articles