
Architecture Diagram for Event Mesh-based Communication Between Two CAP based Microservices
mvn archetype:generate -DarchetypeArtifactId="cds-services-archetype" -DarchetypeGroupId="com.sap.cds" -DarchetypeVersion="RELEASE"This opens an interactive mode to setup your application in terminal/Command Line.
OR
you can download our sample application here.
Note: Your application name need not be the same as ours.
namespace sap.capire.enterpriseMessagingProducer;
using { cuid, managed } from '@sap/cds/common';
entity student : cuid , managed {
firstName : localized String(100);
lastName : localized String(100);
currentClass : String(10);
}
using { sap.capire.enterpriseMessagingProducer as db} from '../db/schema';
service EnterpriseMessagingProducerService {
entity Students as projection on db.student;
}
<dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-feature-enterprise-messaging</artifactId>
<scope>runtime</scope>
</dependency>
cds:
messaging.services:
- name: "messaging"
kind: "enterprise-messaging"
cds:
messaging.services:
messaging-em:
name: "messaging"
kind: "enterprise-messaging"
publishPrefix: '$namespace/'
subscribePrefix: '$namespace/'{
"VCAP_SERVICES" : {
"enterprise-messaging": [{
"label": "enterprise-messaging",
"credentials":{
/* add your service key here*/
}
}]
}
}
package com.sap.enterprisemessagingproducer.handlers;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.nimbusds.jose.shaded.json.JSONArray;
import com.nimbusds.jose.shaded.json.JSONObject;
import com.sap.cds.services.cds.CdsService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.messaging.MessagingService;
import cds.gen.enterprisemessagingproducerservice.Students;
import cds.gen.sap.capire.enterprisemessagingproducer.Student;
@Component
@ServiceName("EnterpriseMessagingProducerService")
public class ProducerHandler implements EventHandler {
private static final Logger logger = LoggerFactory.getLogger(ProducerHandler.class);
@Autowired
@Qualifier("messaging")
MessagingService messagingService;
@On(event = CdsService.EVENT_CREATE, entity = "EnterpriseMessagingProducerService.Students")
public void produceStudentEnrollementEvent(List<Students> studentlists) throws Exception {
JSONObject payload = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (Students students : studentlists) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(Student.FIRST_NAME, students.getFirstName());
jsonObject.put(Student.LAST_NAME, students.getLastName());
jsonObject.put(Student.CURRENT_CLASS, students.getCurrentClass());
jsonArray.add(jsonObject);
}
payload.put("data", jsonArray);
logger.info("Data Emitted to the topic {}", payload.toJSONString());
messagingService.emit("com/eventmesh/blog/StudentEnrolled", payload);
}
}
Application Log for CAP Microservice

Queue Created in SAP Event Mesh by CAP

Queue Subscription Created by CAP

Post Request to Create a Student Record

Receiving Message from CAP Based Microservice

Reading the Event In SAP Event Mesh UI
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.messaging.TopicMessageEventContext;
@Component
public class ConsumerHandler implements EventHandler{
private static final Logger logger = LoggerFactory.getLogger(ConsumerHandler.class);
@On(service = "messaging", event = "com/eventmesh/blog/StudentEnrolled")
public void listen(TopicMessageEventContext context) {
logger.info("---------------------------Reading Payload Emitted by the Event----------------------------------------------------");
logger.info("checking if the message if read from SAP Event Mesh
{}",context.getIsInbound());
logger.info("reading event id{}",context.getMessageId());
logger.info("reading event data{}", context.getData());
}
}

Creating a New Student Entity in Producer Microservice

Consuming Event in a Different Microservice

Snapshot of the Queue and the Consuming Microservice
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 48 | |
| 33 | |
| 23 | |
| 21 | |
| 18 | |
| 18 | |
| 16 | |
| 13 | |
| 12 | |
| 12 |