Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Soumya-Pirlamarla
Associate
Associate
1,360

Event-driven architecture (EDA) is becoming an essential design pattern for modern business applications. With SAP BTP, CAP Java, and SAP Event Mesh, developers can build loosely coupled, scalable applications that react to real-time changes across systems.

In this blog, we will walk through how to publish and consume events from a CAP Java service using SAP Event Mesh. The goal is to provide a clear, practical, end-to-end example that you can reuse in enterprise applications.

1. Why Event-Driven Architecture on BTP

Traditional request-response patterns tightly couple services and increase latency.
Event-driven architecture improves application design by enabling:

  • Asynchronous communication between microservices

  • Decoupled integration between S/4HANA, BTP services, and custom apps

  • Real-time notifications and process automation

  • Better scalability and resilience

SAP Event Mesh acts as a fully managed messaging backbone on SAP BTP, supporting queue and topic-based messaging using AMQP and REST APIs.

2. Architecture Overview

The scenario covered in this blog:

  1. A CAP Java service publishes an event (e.g., new SupplierQuotation created).

  2. SAP Event Mesh receives the event and delivers it to a queue.

  3. Another CAP Java service or an external microservice consumes the event asynchronously.

This keeps the producing and consuming services independent and scalable.

3. Prerequisites

  • SAP BTP Cloud Foundry subaccount

  • SAP Event Mesh service instance

  • CAP Java project (Spring Boot or Node.js runtime)

  • Basic knowledge of OData services in CAP

  • A messaging queue and topic configured in Event Mesh

4. Configure Event Mesh in MTA

Add the Event Mesh service to your mta.yaml:

- name: event-mesh
  type: org.cloudfoundry.managed-service
  parameters:
    service: enterprise-messaging
    service-plan: default

Bind it to your CAP Java application:

requires:
  - name: event-mesh
    parameters:
      config:
        rules:
          - topic: sap/demo/quotation
            publish: true
            subscribe: true

 

Deploy using: 

mbt build -t mta && cf deploy mta_archives/app.mtar

5. Event Mesh Credentials in CAP Java

After deployment, the CAP runtime automatically reads the bound service credentials from VCAP_SERVICES.

These values include:

  • Broker URL

  • Client ID and Client Secret

  • Token endpoint

  • Messaging namespace

CAP Java uses the SAP Cloud SDK under the hood for Event Mesh connectivity.

6. Publishing Events from CAP Java

Let us assume we have an entity:

@Entity
public class SupplierQuotation {
    
    public String ID;
    public String supplierId;
    public BigDecimal amount;
}

 

To publish a message after creation, use a custom event handler:

@Component
public class SupplierQuotationHandler implements EventHandler {

    (event = CdsService.EVENT_CREATE, entity = "my.srv.SupplierQuotations")
    public void onCreate(SupplierQuotation quotation) {

        Map<String, Object> payload = Map.of(
                "quotationId", quotation.ID,
                "supplierId", quotation.supplierId,
                "amount", quotation.amount
        );

        sendEvent("sap/demo/quotation", payload);
    }

    private void sendEvent(String topic, Object payload) {
        MessagingService messaging = MessagingServiceProvider.getMessagingService();
        messaging.emitEvent(topic, payload);
    }
}

The MessagingService abstracts low-level connectivity and sends messages to Event Mesh using the REST interface.

7. Consuming Messages in CAP Java

To consume messages, bind the queue to the topic inside Event Mesh:

  • Topic: sap/demo/quotation

  • Queue: quotation-queue

  • Rule: subscribe to this topic

Then create a message listener:

@Component
public class QuotationMessageConsumer {

    @MessagingDestination("quotation-queue")
    public void onMessage(String message) {
        System.out.println("Received message: " + message);

        // process message
        // store into log table, trigger workflow, etc.
    }
}

This is a Spring-like annotation provided by the CAP messaging framework, automatically subscribing the service to the queue.

8. Error Handling and Retries

Event Mesh supports:

  • Automatic redelivery

  • Dead Letter Queues (DLQ)

  • Message TTL and backoff policies

In CAP Java you can implement structured error handling:

try {
    processMessage(message);
} catch (Exception ex) {
    throw new MessagingException("Processing failed", ex);
}

Throwing an exception ensures the message is returned to the queue and retried.

9. Securing Events with OAuth

Event Mesh uses OAuth for all publish and subscribe operations.

Key points:

  • Always use the XSUAA token from service binding (Refer to my blog for more details)

  • Ensure your app has messaging.publish or messaging.consume scope

  • Use minimal scopes for principle of least privilege

CAP Java automatically injects OAuth tokens when emitting or consuming events.

10. Best Practices for Event-Driven CAP Applications

  • Use clear topic naming standards:
    sap/<domain>/<subdomain>/<eventName>/v1

  • Avoid large payloads; store details in database, send only IDs

  • Keep consumers idempotent

  • Implement correlation IDs for debugging

  • Enable DLQ and retry policies for reliability

  • Do not block listener threads with long-running processing

  • Maintain versioned event schemas

11. Testing the Setup

You can test publishing manually using:

cf logs <app-name>

Or by sending a message from Event Mesh Dashboard:

  • Navigate to “Queues”

  • Choose your queue

  • Select “Send Test Message”

Verify that your consumer receives the message.

12. Final Thoughts

Event-driven architecture brings flexibility, decoupling, and real-time capabilities to modern business applications. By combining CAP Java with SAP Event Mesh, developers can implement scalable asynchronous workflows without tightly coupling services.

Using Event Mesh topics and queues allows CAP applications to communicate reliably, handle spikes in load, and integrate with S/4HANA or other BTP services through lightweight events. CAP Java simplifies this further by providing built-in messaging abstractions, allowing you to focus on business logic rather than low-level infrastructure.

Whether you are building microservices, automating procurement processes, or reacting to business events as they happen, this architecture provides a robust foundation for enterprise-grade applications on SAP BTP.