Skip to Content
Technical Articles
Author's profile photo Chaithanya Mirle Kantharaju

Using Enterprise Messaging for Extensions with Node Part 1

I have been working as a SAP Solution Architect for several years with specific focus on S/4HANA and Custom development on SAP Cloud Platform.

When I first came across the Enterprise Messaging service few months back, I was quite excited that we finally have a messaging broker which will help me design applications with micro-services based architecture on SAP Cloud platform. And that I don’t have to rely on non-SAP tools anymore.

So when I started to learn about enterprise messaging on SAP Cloud Platform, I realized there was not much information available. Hence I thought I will contribute my 2 cents to this knowledge base from the experience I have gathered.

This blog provides information on not just enterprise messaging service but also how you could use this to extend S/4HANA.

 

And what else do you do when there is a Pandemic and you are on vacation. 🙂

 

What is Enterprise Messaging?

I will not write too much about this, but to give a short description, Enterprise Messaging is a cloud based service that enables applications (on-Premise or Cloud) to communicate asynchronously.

The word “Asynchronous” is the key here, because it means a company can create numerous applications without worrying about programming language or co-ordination between development teams. The Enterprise Messaging service acts as a broker which manages messages, queues and subscriptions

I think a true micro service based applications always need a sophisticated messaging service like Enterprise Messaging.

 

So How does the picture look?

Enterprise_Message

 

There are two important points that the above picture depicts.

  • Asynchronous Communication
  • Integration with any Application

 

This pattern really helps in not only developing independent applications, but also plays a important role in S/4HANA extensibility.

For example: On Creation of a Business Partner in S/4HANA on premise, an event can be triggered which will be registered on Enterprise Messaging service in Cloud. And any application can subscribe to these events and perform subsequent actions.

In this blog, I have created a sample of a Sender service, the Orchestrator Service and a Receiver Service.

In our scenario the sender service will raise a message indicating an event that occurred in the Enterprise landscape.

The Orchestrator Service is the listerner and knows who all are interested in this event. It finally calls the Receiver API which performs the subsequent action.

I have used NodeJS for all of them but you could do similar services using Java, Python etc.,

 

Pre-Requisites

I have used below tools for this setup.

  • Visual Studio Code
  • SAP Business Application Studio
  • NodeJS v12.0 (When I started, SAP CAP was not supporting anything higher)
  • SAP CDS CLI
  • SAP Cloud Platform Trial Account
  • SAP Cloud Foundry CLI

 

Step 0: Setting up the tools

Installing Visual Studio Code

You can download and install the Visual Studio Code from https://code.visualstudio.com/download

 

Install NodeJS

Please install the NodeJS version v12 from https://nodejs.org/dist/latest-v12.x/

I selected ‘node-v12.20.0-win-x64.zip’, But choose an appropriate one based on your machine.

 

Unzip the file to a suitable location and set the environment Variable.

Prepare Cloud Foundry

I’m assuming you already have a SAP Cloud Platform Trial account. If not, please create one with a space.

 

Enter your space and click on Service Marketplace.

Search for Enterprise Messaging.

 

Select and click on Create Instance.

Since you are in a trial account you can only create a Dev Plan. Select it and enter a Instance Name as below.

 

Click Next and enter the below parameters

{
    "options": {
        "management": true,
        "messagingrest": true
    },
    "emname": "em-ps"
}

 

Then click on Create Instance.

 

Create Queue

The Enterprise Messaging Service also provides us a Dashboard which gives an overview of all the messages and Queues.

To access this, go to the service instance we just created and in the options menu select View Dashboard

Please Ignore the bindings shown in the above picture.

 

This should now launch the Messaging Administration.

Here, select Queues and click on Create. Enter the name as customerqueue.

 

Finally let’s create a Queue subscription. Click on Queue Subscriptions and click on Create.

Select the queue name which you created above and provide a topic name as customer/created.

The name of the subscription is important as this should match with the subscription name we are going to emit our message in our sender application.

As you can see, you can create multiple topics for the same queue which help you organize the messages better based on a different events. For example, you can have a subscription called customer/delete. And whenever a customer is deleted in S/4HANA it can send a message to this topic.

 

Installing Cloud Foundry CLI

If you haven’t yet installed the CLI for cloud foundry, please follow the instructions in the link https://developers.sap.com/tutorials/cp-cf-download-cli.html

 

Okay, so now we should be ready with our development!!

 

Sender Service

I will be writing a separate blog to connect S/4HANA as a sender system.

For the sake of simplicity, I have created a simple sender application based on NodeJS.

The sender application is based on the blog here https://blogs.sap.com/2020/03/03/sap-cloud-application-programming-model-and-enterprise-messaging-2-send-message/#create_app

 

Step 1: Login to SAP Cloud Platform Trial Account

Login to your SAP Cloud Platform trial account and click on SAP Business Application Studio.

If you are launching for the first time, it will prompt you to create a Dev Workspace. Create a workspace and make sure it is running.

Step 2: Create a CAP Application

In the welcome tab, click on Create project from template

Select CAP Project and click on Start. Then enter a project name as capsender.

This should create an empty project with different CAP modules such as db, srv

 

Step 3: Edit the project

As a first step let’s prepare the project dependencies. Click on the file package.json

CAP would’ve already generated some mandatory tags. Please add the below dependency.

  "cds": {
    "requires": {
      "messaging": {
        "kind": "enterprise-messaging"
      }
    }
  }

And application dependency

    "@sap/xb-msg-amqp-v100": "latest"

You will find the complete code sample below.

{
  "name": "capsender-srv",
  "version": "1.0.0",
  "description": "A simple CAP project.",
  "repository": "<Add your repository here>",
  "license": "UNLICENSED",
  "private": true,
  "dependencies": {
    "@sap/cds": "^3",
    "express": "^4",
    "@sap/xb-msg-amqp-v100": "latest"
  },
  "cds": {
    "requires": {
      "messaging": {
        "kind": "enterprise-messaging"
      }
    }
  },
  "devDependencies": {
    "sqlite3": "^5"
  },
  "scripts": {
    "start": "npx cds run"
  }
}

 

These dependencies will tell the build cycle to add the enterprise messaging service as a binding and also initialize the xb-msg-amqp droplet when deployed on cloud foundry.

 

Create Services

Since we are only creating a service to generate messages, we don’t need any persistence and hence the db module is not required in this application.

 

Create a new file under folder srv called sender-service.cds

Add the below code to it.

service SenderService {

    function send() returns String;
}

 

This file creates a service called SenderService with a function send(). However this function is not yet implemented. This will be implemented in the below file.

 

Create a new file under folder srv called sender-service.js

const cds = require ('@sap/cds')

module.exports = cds.service.impl ((srv) => {
 
  srv.on ('send', async(req)=>{
    const message = { 
      'Sending message. Current Time: ' + new Date().toLocaleString()
    }
  
    const topic = 'customer/created'
    
    srv.emit (topic, message)  
 
 //   return message
    return "Successfully sent event"
  })
})

 

This particular code basically sends a message with current date and time to a queue called customer/created.

And that’s it, this is all you require to send a message to the SAP Enterprise Messaging service which has the queue we had configured.

Add MTA file

Let us add the MTA.yaml file and build it.

To add the MTA file, right click on the project capsender and click on Open in Terminal.

Enter the command CDS ADD MTA

This will add a MTA.YAML file which can be used to build our application.

Now right click on the MTA.YAML file and select Build MTA.

This will generate the mta_archives folder with the .mtar files which will be used to deploy on cloud foundry.

 

Deploy the application to Cloud Foundry

Expand the folder mta_archives and right click on capsender-srv.mtar file and select Deploy MTA Archive.

Verify the cloud foundry space to make sure the instance is running.

 

 

Sometimes, the deployment fails to bind the Enterprise messaging instance, in that case manually bind the service and restart the instance.

 

Test the Service

Open the application URL in a browser and you should be able to see a catalog as below.

 

Click on sender and add send() method to the URL. And you should be able to get the below response.

Now, let’s check if the message has been sent to the queue. Open the Dashboard of Enterprise Messaging service instance and click on Queues.

Here you should be able to see a message against the queue customer/created.

 

Ok now that we have our sender application ready. We can create our Receiver and Orchestrator application to subscribe to this queue.

You can find the details of that in Part 2 here.

 

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Frank Li
      Frank Li

      Great, thanks a lot for your sharing.