How to Create Shared Memory Object Class in ABAP

Create AREA ROOT Class for Shared Memroy Object Area

The first step is to create an Area Rool class. ( let’s say ZCL_ROOT)

Go to SAP Transaction SE24 and create a new class.

Shared Memory Enabled Class

Make sure you checked “Shared Memory Enabled” under Class/Interface Propertiess Tab as following:

Create 2 Instance Attributes

The next step is to create, for example, 2 Instance Attributes within this class.
The Visibility has to be set to Private .

Create SET_DATA & GET_DATA Method

Both of these methods SET_DATA and GET_DATA with handle Shared Memory Object Write and Read operation. We will come back to these methods’ implementation later.

Check also How to use EXPORT or IMPORT data to / from ABAP Memory ID

Create SAP Shared Memory Area ( SHMA )

Launch the Transaction SHMA and create a new Area Name. (ZCL_MEM_AREA for sample)
Fill a short description for the Memory Shared Area. In the Root Class, put the class you have created above ( ZCL_ROOT ).

When saving, SAP will generate an area Class with the same name as the Area Name ( ZCL_MEM_AREA ). The new generated Area Class will contains all the methods needed to read, write and update the Shared Memory Object.

Shared Memory Object Area Class’s Methods

Method Visibility Description
ATTACH_FOR_READ Public Request a Read Lock
ATTACH_FOR_UPDATE Public Request a Change Lock
ATTACH_FOR_WRITE Public Request a Write Lock
BUILD Static Direct Call of Area Constructor
SET_ROOT Instanciate Sets Root Objects

SET_DATA and GET_DATA Implementation

Once you have created the area root class, the area class and generated the area class, you can implement the ABAP code for the SET_DATA and GET_DATA in order to read/Write the Shard Memory Object.

Write data in Shared Memory Area (SET_DATA)

In order to save data into an instance of Shared Memory Object, you can set the data as following :

Read data in Shared Memory Area (GET_DATA)

here an example how to use the GET_DATA in order to read an instance of Shared Memory Object:

Note that NAME and VALUE are the attribute of the Area Root Class.

List of Area Root Class Exception

Here the list of the main exception Class for Area Root: