Sie sind auf Seite 1von 11

ABAP Shared Memory Objects Tutorial with Sample ABAP

Code
This ABAP Shared Memory Objects tutorial includes sample ABAP code showing how to
code to store data in shared memory objects and read data from shared memory object. By
following the instructions in this step by step ABAP tutorial, developers can create a shared
memory object in SAP. Given sample ABAP codes and sample programs will help developers
to write data or read data from shared memory objects using shared memory programming.

ABAP developers can use following SAP transaction codes for Shared Memory Object
development, debugging and monitoring:
SE80 or SE24 transaction for ABAP W orkbench Class Builder editor to create root class for
data structure.
SHMA SAP transaction for creating Shared Object Area class interface.
SHMM to monitor Shared Memory Areas objects, their memory consumption, and view and
display data contained in shared memory object

Of course ABAP developers can use transaction SE38 to create two ABAP programs where
they set the shared memory object value and read back from shared memory area object.

Steps to Implement ABAP Shared Object Solution

1.) Create a class that represents data structure. (I'll refer this class as root class). Use SE80 or
SE24 tcode

2.) Create a shared memory area class to wrap data class for standard shared memory object
methods. (I'll refer this class as handle class). Use SHMA transaction

3.) ABAP code to write data to shared memory object. For example you can build a simple
ABAP program using SE38 to set shared memory object value.

4.) ABAP code to read data from shared memory object. A similar ABAP report can be written to
read data from shared memory object using SE38.

Naming of the root class and the shared memory area is important. The first part of the names
of both classes should be same. On the other hand root class name should be ending with
"_root" and the shared memory area class name should end with "_area"

In fact this naming simpifies the work of ABAP developer. ABAP programmers will have a better
understanding about the objects they're using to build a SAP shared memory object solution.

Root Class for Shared Memory Object

In this section of the ABAP tutorial, we will create a class with Shared Memory-Enabled option
is selected.
The class name is Z_VBRK_SHM_TABLE_ROOT for the sake of naming standards.

First of all, call SE80 W orkbench transaction code and choose Class / Interface as object type.
Then type the name of the new root class for your shared memory object as seen in below
screen shot.
Press Enter key for next dialog

When your approval is requested to create the mentioned class since it does not exist, click Yes
to continue to create the new root class for shared memory object.

Choose Class option instead of Interface to indicate that you're creating a class not an interface

Press Enter or OK icon button

Provide class description for the shared memory object root class.
Choose instantiation as Public.
Class type is Usual ABAP class and mark Final checkbox.
When you are finished with basic class properties, click Save to continue for the SAP Class
Builder screen.
Be sure that you do not forget to mark Shared Memory-Enabled checkbox on class properties
screen general data section.

Now after we mark the class as shared memory-enabled, we will now create an attribute where
data will be stored and two class methods to set and get shared data in the class attribute.
Now switch to Attributes tab and define a private attribute where data will be stored.
For example we can create an attribute where ABAP developers can store a list of
SAP documentnumbers in VBELN data type.

When I searched the ABAP dictionary for an existing table type for VBELN data type, I found
VBELN_VL_T table type. I'll use VBELN_VL_T table type in this tutorial to create a class
attribute for storing SAP document numbers like invoice numbers or order numbers in a shared
memory object.

After attribute is created, ABAP developers can continue with next step.
In this step, we will create two public methods.
These methods will be used to set data to private attribute and get data (read) from private
attribute of the shared memory object.

SET_VBELN method has import parameter P_VBELN of type VBELN_VL_T which is a table
type for VBELN data element. This method takes internal table of table type VBELN_VL_T to
set as shared memory object data content.

GET_VBELN method has export parameter P_VBELN_TBL of type VBELN_VL_T which is a


table type for document numbers. Using the export method Get_Vbeln, ABAP users will be able
to return shared memory object table for all stored document numbers.
After the developer defines class attribute and set/get methods of the root class, within the
methods following ABAP codes can be used.

METHOD SET_VBELN.
SHM_VBELN_TBL = P_VBELN.
ENDMETHOD.

METHOD GET_VBELN.
P_VBELN_TBL = SHM_VBELN_TBL.
ENDMETHOD.

Set method assigns the input parameter value as the value of shared memory object attribute.
And the Get method returns the attribute value as export parameter. W e will see how we will
call these shared memory enabled class methods within other ABAP programs later in this
ABAP tutorial.

Create Shared Memory Area: Handle Class

This tutorial step demonstrates the creation of the shared memory area.
To create a Shared Memory Area ABAP developers can use SHMA SAP transaction code.
Area name should be Z_VBRK_SHM_TABLE_AREA according to the naming rules which will
help the developers to reference it without any confusion later

When you press Create button, following Create Area screen will be displayed.
Enter the root class name we have created as shared memory root class in Attributes tab basic
properties section. In our tutorial, we have defined the root class name as
Z_VBRK_SHM_TABLE_ROOT in previous steps.
If you look at the details of the above Create Area screen, you will realize that an ABAP
developer can enable versioning with a Shared Area object. It is also possible to define
the maximum shared area object size that will keep on memory in kByte and maximum
number of allowed versions.

After you complete Shared Memory Area properties like deciding if you want to use it
with Versioning is enabled or not, first press Save icon and then press Release button at
top icons menu.

The class category is Area Class (Shared Objects) when you display the shared memory
area using SE80. The Root attribute of the handle class is automatically set to
previously created root class at step 1.
Ensure to set the superclass CL_SHM_AREA in the properties of the Area class

ABAP Programs

The below ABAP report whose source code is given reads shared memory object value.
Root class attach_for_read method must be called before reading shared memory data using
our custom Get_Vbeln method. At the end of the read process, ABAP developers must call
detach() method to free shared memory object.

REPORT Z_READ_SHARED_MEMORY_OBJECT.

DATA lt_vbeln type VBELN_VL_T.


DATA ls_vbeln like LINE OF lt_vbeln.

DATA lr_handle TYPE REF TO Z_VBRK_SHM_TABLE_AREA.

lr_handle = Z_VBRK_SHM_TABLE_AREA=>attach_for_read( ).
lr_handle->root->get_vbeln( IMPORTING P_VBELN_TBL = lt_vbeln ).
lr_handle->detach( ).

In order to update data stored in memory using shared memory objects, developers must
consider using one of attach_for_write() or attach_for_update() methods.
Following ABAP report is demonstrating how to assign shared memory object value.
First of all, using below ABAP a check is done to see if an active version of the shared memory
object exists or not. This check is done by reading the shared memory object.
If cx_shm_no_active_version exception is catched in the ABAP TRY and CATCH syntax, then
ABAP developer should use attach_for_write method while creating the root class object
handle. Otherwise, attach_for_update method can be used during root creation of the root class
handle.

REPORT z_shared_memory_object_sample.

PARAMETERS: pr_vbeln LIKE vbrk-vbeln DEFAULT '1111111111'.


* ABAP program appends p_VBELN parameter value TO Shared Memory Object

DATA lt_vbeln type VBELN_VL_T.


DATA ls_vbeln like LINE OF lt_vbeln.

DATA lr_handle TYPE REF TO z_vbrk_shm_table_area. " Area Class


DATA lr_root TYPE REF TO z_vbrk_shm_table_root. " Root attribute of handle
class. SHM: Model of a Data Clas

******************************************************
* First Try to Read
data excp type ref to cx_shm_no_active_version.
data lf_readerror type c1.

try.
lr_handle = z_vbrk_shm_table_area=>attach_for_read( ).
lr_handle->root->get_vbeln( IMPORTING P_VBELN_TBL = lt_vbeln ).
lr_handle->detach( ).
catch cx_shm_no_active_version into excp.
lf_readerror = 'X'.
endtry.
******************************************************

APPEND pr_vbeln to lt_vbeln. " append input parameter value to internal table

IF lf_readerror = 'X'.
lr_handle = z_vbrk_shm_table_area=>attach_for_write( ).
ELSE.
lr_handle = z_vbrk_shm_table_area=>attach_for_update( ).
ENDIF.
CREATE OBJECT lr_root AREA HANDLE lr_handle.
lr_handle->set_root( lr_root ).

lr_root->set_vbeln( p_vbeln = lt_vbeln ). " custom SET method is executed

lr_handle->detach_commit( ).

At the end of the ABAP code block for storing data in shared memory object, COMMIT in
database must be executed. Since the ABAP developer should free the SAP shared memory
objects, the DETACH process must be executed too.
To detach from shared memory area with saving data in memory variables, detach_commit()
method can be called.

Monitor Shared Memory Area using SHMM Transaction

SAP transaction SHMM is used for monitoring shared memory areas for memory consumption
and to display data stored in shared memory objects.

When you call SHMM tcode, SAP Shared Memory: Areas screen will be displayed. On the
screen on Areas tab which is the default displayed tab, you will see shared memory areas
created on the related SAP system.

Highlight the shared memory area which you want to monitor in detail as follows.

When you double click the shared memory area, instances of the shared area will be displayed
as a list on a new screen.
Choose the default instance by highlighting it and click on Read Active Version icon to display
shared memory object contents.

In a new SAP screen, we will be displayed root object data in different tabs. The default tab is
Explorer. You will see the shared memory object attribute we have created earlier in this tutorial.

Highlight the SAP shared memory class attribute SHM_VBELN_TBL which is a table storing
document numbers. Then double click it to view its stored row values.

You see below, tutorial sample shared memory object has two document numbers stored in its
class attribute.
To summarize, in this ABAP tutorial I tried to give brief information about SAP shared memory
object and how to use shared memory in your ABAP programs. Using the transaction codes
and step by step guide, an ABAP developer can create a sample shared memory root class and
the shared memory area for this class. Then given ABAP reports can be used to write, update
or read shared memory objects.

Das könnte Ihnen auch gefallen