Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Processing CloudEvents with Eclipse Vert.x

 

December 11, 2018
Matthias Wessendorf
Related topics:
Event-DrivenJavaKubernetesServerless
Related products:
Red Hat build of Eclipse Vert.x

Share:

    Our connected world is full of events that are triggered or received by different software services. One of the big issues is that event publishers tend to describe events differently and in ways that are mostly incompatible with each other.

    To address this, the Serverless Working Group from the Cloud Native Computing Foundation (CNCF) recently announced version 0.2 of the CloudEvents specification. The specification aims to describe event data in a common, standardized way. To some degree, a CloudEvent is an abstract envelope with some specified attributes that describe a concrete event and its data.

    Working with CloudEvents is simple. This article shows how to use the powerful JVM toolkit provided by Vert.x to either generate or receive and process CloudEvents.

    SDKs for working with CloudEvents

    In addition to the specification, the CloudEvents team from the Serverless Working Group is working on different SDKs for various platforms, such as JavaScript, Golang, C Sharp, Java, and Python. This article will give a quick overview of the Java SDK and how it can be used inside an application built with Eclipse Vert.x.

    The API is very simple and contains a generic CloudEvent class as well as a builder to create an instance of a CloudEvent:

    final CloudEvent<MyCustomEvent> cloudEvent = new CloudEventBuilder<MyCustomEvent>()
        .data(new MyCustomEvent(...))
        .type("My.Cloud.Event.Type")
        .id(UUID.randomUUID().toString();)
        .source(URI.create("/trigger");)
        .build();

    Above, we use the CloudEventBuilder to create a very simple CloudEvent instance. However, in isolation, the API does not show its strength.

    Eclipse Vert.x

    Eclipse Vert.x is a toolkit for building reactive applications on the JVM. It is event-driven and nonblocking, which means applications can handle a lot of concurrency using a small number of kernel threads. See the resources below for more info on Vert.x. Fortun

    Fortunately, support for Eclipse Vert.x is included in the CloudEvents Java SDK:

    <dependency>
        <groupId>io.cloudevents</groupId>
        <artifactId>http-vertx</artifactId>
        <version>0.2.0</version>
    </dependency>

    Sending a CloudEvent to a remote service

    Now that we have our CloudEvent object, capturing our event data, we want to send it to a remote cloud service, which will then process it:

    final HttpClientRequest request = vertx.createHttpClient().post(8080, "localhost", "/");
    
    // add a client response handler
    request.handler(resp -> {
        // react on the server response
    });
    
    // write the CloudEvent to the given HTTP Post request object
    VertxCloudEvents.create().writeToHttpClientRequest(cloudEvent, request);
    request.end();
    

    After creating an HTTP Post request, we set up an async handler to deal with the future response of the server. Finally, the writeToHttpClientRequest of our VertxCloudEvents utility is used to serialize the actual CloudEvent object to the given HttpClientRequest.

    Receiving CloudEvents with Vert.x

    The VertxCloudEvents utility also contains a different function to receive a CloudEvent inside an Eclipse Vert.x HTTP server application:

    import io.cloudevents.http.reactivex.vertx.VertxCloudEvents;
    import io.vertx.core.http.HttpHeaders;
    import io.vertx.reactivex.core.AbstractVerticle;
    
    public class CloudEventVerticle extends AbstractVerticle {
    
      public void start() {
    
        vertx.createHttpServer()
          .requestHandler(req -> VertxCloudEvents.create().rxReadFromRequest(req)
          .subscribe((receivedEvent, throwable) -> {
            if (receivedEvent != null) {
              // I got a CloudEvent object:
              System.out.println("The event type: " + receivedEvent.getType())
            }
          }))
          .rxListen(8080)
          .subscribe(server -> {
            System.out.println("Server running!");
        });
      }
    }

    Above, we start a simple HTTPServer, using the Vert.x API for RxJava 2. Inside the reactive request handler, we invoke the rxReadFromRequest() method and subscribe to the CloudEvents it returns for further processing. Now we can work with the CloudEvent object inside our own server-side framework!

    Conclusion and Outlook

    Working with CloudEvents is simple and Vert.x provides a powerful JVM toolkit to either generate or receive and process CloudEvents in our system. CloudEvents are being adopted by more and more tools and frameworks such as Knative, which uses CloudEvents to exchange data between different components and services in a standardized format.

    The CloudEvent specification is in its early stages with its current 0.2 version. However, even in such infancy, it is generating traction and proving an increasingly useful specification to allow interoperability between applications.

    Additonal Resources

    • Vert.x:
      • Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design, a free ebook
      • Introduction to Vert.x article series by Clement Escoffier
        • Part 1—Introduction to Vert.x - My First Vert.x Application
        • Part 2—Eclipse Vert.x Application Configuration
        • Part 3—Some REST with Vert.x
        • Part 4—Accessing Data, the Reactive Way
        • Part 5—When Vert.x meets Reactive eXtensions
    • CloudEvents:
      • EventFlow: Event-driven microservices on Red Hat OpenShift

     

    Last updated: January 12, 2024

    Recent Posts

    • Storage considerations for OpenShift Virtualization

    • Upgrade from OpenShift Service Mesh 2.6 to 3.0 with Kiali

    • EE Builder with Ansible Automation Platform on OpenShift

    • How to debug confidential containers securely

    • Announcing self-service access to Red Hat Enterprise Linux for Business Developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue