Blog > How to consume RESTful Enterprise Messaging in ABAP

How to consume RESTful Enterprise Messaging in ABAP

RESTful_Enterprise_Messaging_Halicki_blogpost_int4
Andrzej Halicki SAP Integration Consultant
icon__calendar 2020-04-28

In this blog post you will learn:

  • How to configure connection to Enterprise Messaging from your ABAP system
  • How to send and consume messages from the service

 

Introduction

In this article I will show you how to connect from your on-premise system to the Enterprise Messaging service and how to process messages from ABAP via the REST protocol. We will also take a look at the management APIs. Let’s go!

Prerequisites:

  1. You have already created an Enterprise Messaging service instance on your Cloud Foundry trial account. If not, take a look at this documentation:

 

Destinations in SM59

We will need to create three  destinations in SM59. You could actually handle it manually in the code but it’s much more convenient to have it all in our beloved transaction – it means less code to write in the future plus all options are easily maintainable.

We will create a destination for obtaining token for further communication, management destination for getting information about our service and finally messaging destination for sending and consuming messages.

Go to your space (dev in my case) and go to your Enterprise Messaging service instance (sandbox in my case):

SAP Cloud Platform Cockpit

On the next screen go to Service Keys. If you haven’t created one yet – do this now. You will use it to bind and authorize connections. From the service key we will need to get three pieces of information.

 

Token Destination

When you look at the service key you will see two sections – messaging and management. There is only one authorization credential and endpoint for token, so it doesn’t matter which section you will take it from.

Token Destination


 Create a destination in SM59:

SM59 Destination creation

In the path prefix use ‘/oauth/token?grant_type=client_credentials&response_type=token’. In the Logon & Security use the Basic Authentication. Use ‘clientid’ as user and ‘clientsecret’ as password from the service key. Set SSL Active and use SSL Client (Standard). Follow this documentation to configure your SSL:

Management destination

Create management destination in SM59:

Management Destionation creation2

 

 

In the host use the uri from management section in your service key. No Logon & Security settings are necessary here as authentication will be performed with the use of the token.

 

Messaging destination

Now move to ‘messaging’ section in your service key and create the messaging destination with the REST uri.

 

Management destination SM59

 

Enterprise Messaging

 

In this case no Logon & security is necessary.

 

Let’s code a little bit

We went through the most confusing part, now we can move on and have some fun. Let’s create the HTTP and the REST client:



Do the same for all three destinations.

Now let’s fetch the token.



Class /ui2/cl_json will help us with transforming JSON responses into ABAP structures. Remember that our token can expire (in EE it’s around a few hours, so it’s not a threat in our case). If you wanted to handle that anyway, I do it this way:



And later you can check:



When you have your token you are ready to go!

Let’s post a message to a queue in the Enterprise Messaging.



In order to consume message from a queue use uri



Let’s use management destination and get some information about queues in our service:


 

Summary

And that’s all – it’s this easy. You can go into documentation and have some fun with available APIs. Remember that not all of them are available in the trial account.

I was using /ui2/cl_json because it’s super convenient to use but you can do it your way. Join our LinkedIn group and let me know what you think in the comments section under the post that I shared.

You can also take a look at a sample class I wrote for consuming Enterprise Messaging here:

Read also:

 

  1. SAP CPI: Technical guide to build a single XSLT mapping for multiple input types

 

Andrzej Halicki SAP Integration Consultant
Andrzej Halicki is an SAP integration consultant and developer. He has strong experience in ABAP development across local and international projects. Andrzej works with the latest solutions like ABAP for HANA and Cloud Platform Integration. Author of many technical articles that are available online in the SAP Community and Int4 website.