Triggering SAP Cloud Platform functions using timers and events from message topics
Few days ago, I saw a question from Nabheet Madan on twitter around the timer trigger for SAP Cloud Platform functions. I thought I will share some info around the usage of Timer trigger as well as the trigger event for a message topic.
This blog is not for beginner and I would recommend to go through the blog series “React to events and connect 3rd Party systems using Serverless services & Open Connectors” where I have explained how the Functions service can be configured to work in conjunction with the Messaging service on SAP Cloud Platform.
As the name suggests, you can setup a timer configuration to automatically schedule your function to run. You can either schedule it to run every hour or on a particular day of a month etc. To achieve this, the functions service leverages Cron expression. Its a string with subexpressions and below are the permitted values for each subexpressions. This is documented in SAP Help
Cron expressions were new to me and I explored for some examples and found some in a Corn Trigger Tutorial
In order to get started, you would need to create a function. In the below example, I have a simple console output statement which will show up in my logs every time the function is being triggered by the timer.
I have attached a “Timer” trigger type to this function and for the cron input, I have provided the value “*/1 * * * * “. This will ensure that the functions gets triggered every minute.
After deploying the artefacts, I am able to verify that the function gets called every minute.
In the previous blog post, I defined a queue in the Enterprise Messaging service and configured a function to be triggered every time there is message in a queue. In this section, I am going to introduce topics. Topics are great when you have pub-sub model and would like to have multiple subscribers receive a message which has been broadcasted to a topic.
In the example below, I have created a queue called “Orders” in the Enterprise Messaging service.
In the “Queue Subscriptions” menu, I have created few topics and bound them to the “Orders” queue as shown below. The SAP Help documentation provides guidelines on the how to name topics and separate the segments
In my Functions service, I have created two functions – Sales and Invoices which will be invoked when there is an event in the corresponding business objects.
The function has just a console output statement displaying the Object ID which is passed to the function via the event.
I have attached an event trigger to this function as shown below. This trigger will listen to any messages coming to the topics in sales/created or sales/updated.
Similarly, I have created another function to handle Invoice objects and attached it to an event trigger which listens to messages in the topic invoices/processed.
I am using a POSTMAN rest client to test this for now. I am hoping to configure the event administration to show how this will event will get triggered from an operation performed in an S4HANA on-premise system.
In the Rest Client, I have followed the guidelines/parameters as documented in the SAP Help in order to publish a message to a topic. Note that the segments of a topic need to be separated by “%2F” and not “/“. In the below example, I am publishing a message to the topic sales/created
The output of the Sales function can be viewed in the Execution log.
Similarly, I can also publish a message to a different topic invoices/processed
The output of the Invoices function can be viewed in the Execution log.