cancel
Showing results for 
Search instead for 
Did you mean: 

Enterprise Messaging in CAP

Former Member
0 Kudos

Hi,

I have created an Enterprise Messaging Instance in Cloud Platform, How to use that in an Application built using Cloud Application Programming Model(CAP). What needs to be done next.?

Any lead is welcome.

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

CarlosRoggan
Product and Topic Expert
Product and Topic Expert

Hi,

you Need to do the following steps (assuming you have productive account in Cloud Foundry):

* Bind your CAP application to the msg instance (-> manifest.yml)
* In your package.json add entry to cds-requires section:

"cds":
{ 
"requires":
{
"anyMessagingName":
{ 
"kind":
"enterprise-messaging","credentials": {
"namespace":
"my/own/ns"

The chosen name (anyMessagingName) will be referenced in code
The “namespace” (my/own/ns) will be used by CAP to generate a queue

Make sure that the Namespace is compliant to your Topic rules, as defined when creating a Messaging instance

* In your custom Code handler, you connect to e.g. "anyMessagingName"
* example for emit:

const msg =
cds.connect.to('anyMessagingName‘)
const payload =
{
"theName":
"myName",
"theChangeId":
"123"
}
msg.emit('myEventName', payload)

* to consume an Event/resp message, you configure your consuming CAP app in the same way
* In the custom Code, you connect to your declared anyMessagingName in the same way
to react upon Events, you write the same custom handler Code as used to
With one difference in case of Enterprise messaging: you specify a Topic. In productive account, this is concatenated with the Namespace:

const msg = cds.connect.to('anyMessagingName‘)
msg.on('my/own/ns/BO/BusinessPartner/Changed', async (msg) => {

Once you have understood and configured, it is easy to use 😉

Kind Regards,

Carlos

BTW, here's the link to the docu: https://cap.cloud.sap/docs/guides/consuming-services#emitting-events

Former Member
0 Kudos

Thanks Carlos,

How to configure our CAP application to consume the events emitted by our service, and can you please explain how the connections are happening, so that we can have a clear image regarding the flow.

Answers (6)

Answers (6)

CarlosRoggan
Product and Topic Expert
Product and Topic Expert

One more addendum:

Since CAP version 3.30, the API has slightly changed:

We don't need to connect to a new messaging-configuration

Instead, we configure our own service as follows

package.json:

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

service.js

no connect necessary.

Just use the srv instance

module.exports = cds.service.impl ((srv) => {
  
  srv.on('company/customer/care/demo/customer/created', async (msg) => {  
    const messagePayload = JSON.stringify(msg.data)
    console.log('===> Received message : ' + messagePayload)
  })

and for sending:

  const message = { 
    'myProp': 'Sending message.' 
  }
  const topic = 'company/customer/care/demo/customer/created'
  srv.emit (topic, message) 
CarlosRoggan
Product and Topic Expert
Product and Topic Expert

In the meantime, I've started publishing a little series of tutorials about CAP and messaging:

https://blogs.sap.com/2020/03/03/sap-cloud-application-programming-model-and-enterprise-messaging-1-...
https://blogs.sap.com/2020/03/03/sap-cloud-application-programming-model-and-enterprise-messaging-2-...
https://blogs.sap.com/2020/03/03/sap-cloud-application-programming-model-and-enterprise-messaging-3-...

Note that currently, the CAP-integration of Enterprise Messaging is still in beta-mode, so there might be incompatible changes.

CarlosRoggan
Product and Topic Expert
Product and Topic Expert

Hello al5370 , I don't think that's possible to solve right now, because the FWK reads from binding and thus requires binding and fails during deploy if not bound
What you can do is to manually use the amqp lib

Kind Regards, Carlos

jait23
Explorer
0 Kudos

Is there support for consuming messages from kafka using CAP?

former_member695224
Discoverer
0 Kudos

Hello, Is there any configuration using which we can create webhook automatically through application service itself ?

CarlosRoggan
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi, as I mentioned, the configuration is the same:

"cds":{ 
"requires":{"anyMessagingName":{ "kind":"enterprise-messaging","credentials": {"namespace":"my/own/ns"

and in the JavaScript, you do the Connection:

const msg = cds.connect.to('anyMessagingName‘)

and you add an Event listener

msg.on('my/own/ns/BO/BusinessPartner/Changed', async (msg)=>{
the msg contains the message, Access it with
msg.data

I want to publish a blog, but it will take time, I'm sorry

Cheers,

Carlos

ErikP
Explorer

Hi Carlos,

What if you can't bind to the messaging service because you are in a different space. And you need to use de clientid and secret. How do you set this up in the package.json

thanks in advanced

Erik