IMPLEMENTATION FOR EXPORT/IMPORT USING ABAP OOPs Concept

+ -

Summary

This Implementation will be used as a replacement to SAP Obsolete Object EXPORT/IMPORT  using Shared Memory Object Concept

 

Implementation For Shared Memory Objects

Shared Memory Objects consists of storing data in SAP memory (much likes the EXPORT/IMPORT statements before ECC6) for rapid data retrieval without the need for physically reading persistent DB tables. Unlike the “old” EXPORT/IMPORT technique.

 

In addition, it is, as the name implies, shared between user sessions, so that the data can be freely used and updated by any number of users or sessions. This makes shared memory objects more flexible than the session memory technique.


Shared memory-enabled classes (root classes) are defined in SE24, using the “shared memory-enabled” addition

 

  • Each shared memory area relates to one such specific global root class.
  • Shared memory areas can have several area instances.
  • Each area instance can have several versions (if versioning is allowed).
  • An area class is generated by defining an area in SHMA – this is used as the area handle (attach methods).
  • The root class is used for manipulating the data of the shared object.
  • Shared memory objects can be accessed by any number of users/sessions.
  • Write/update locks are exclusive.
  • All locks are for complete area instance and version (no partial locks possible on specific attributes of the object).

The Two Steps to Define a Shared Memory Object


There are two main steps to creating a shared memory object. Usually, we start by defining the object itself – this means creating an ordinary class using transaction SE24 (or SE80). We define attributes – the data going to be held by the class – and the methods required to enable our ABAP programs to access and work with these attributes. This class is also known as the “root class”, since it is used in step 2 to definesome vital properties of the memory area. Most importantly, we define the class as being “shared memory enabled”:

 

The second step is to allocate a new memory area for our shared object. This is done via transaction SHMA, and here, we will use our newly created root class as the global area root class” of our new memory area. This second step gives us the “area handle” – a specific class which is needed to interact with the memory object we created in the first step.

 

Step 1: The Root Class ( In Our Case ZCL_YXX_STATICCLASS).


The Purpose of the Root Class

As already mentioned, the concept of shared memory objects is based on memory areas, each related to a global area root class. The root class contains the attributes and methods you will use in your ABAP applications. It is created as any other class, the only difference being it having the “shared memory-enabled” attribute set – thus preparing it for being used as a shared memory object.

 

Basically, the root class will need attributes to hold the data you want to store, as well as methods for manipulating the data. In our example, this will include a method to initialize the table with specific data from the persistent layer, as well as retrieving data from it.

1.jpg


Defining Attributes of the Root Class.


First of all, we will need to define the data to be stored in our memory object, so we simply define a table attribute to hold this data.

 

2.jpg

 

Defining Methods for the Root Class


Now, we need methods to allow for interacting with the data. We need at least two methods in our root class, one for initializing the Attributes Defined above with data from the underlying DB tables (the persistent layer), and another method that can be used by our ABAP applications to retrieve data from the memory object.
The Third method visible below, IF_SHM_BUILD_INSTANCE~BUILD, relates to what is known as “automatic pre-load” of the object.


3.jpg

Method SET_DATA

 

The SET_DATA method is used for retrieving data from the DB layer. When our shared memory object is created for the first time, it will be empty, meaning we need to invoke the SET_DATA method to populate Attributes of Class.

 

 

METHOD SET_DATA.

 
IF e_auart IS SUPPLIED.
    zexp_auart = e_auart.
ENDIF.

 

IF e_werks IS SUPPLIED.
    zexp_swerk = e_werks.
ENDIF.

 

ENDMETHOD.

 


In above example ZEXP_AUART & ZEXP_SWERK are Attributes and E_AUART & E_WERKS are parameters for method SET_DATA.


You can Add/define your own Attributes & Parameters and map as above in example.

 

The method has the following parameters & Attributes


4.jpg

5.jpg

 

 

Method GET_DATA

 

The GET_DATA method is used to retrieve data which we stored with method SET_DATA

 

METHOD GET_DATA.
i_auart = zexp_auart.
i_werks = zexp_swerk.

ENDMETHOD.

 

In above example ZEXP_AUART & ZEXP_SWERK are Attributes and I_AUART & I_WERKS are parameters for method GET_DATA.

 

You can Add/define your own Attributes & Parameters and map as above in example.

6.jpg

7.jpg

Method IF_SHM_BUILD_INSTANCE~BUILD


Finally, we need to look at the method IF_SHM_BUILD_INSTANCE~BUILD. This method is related to what is known as automatic pre-load (or auto-start) of the memory area related to our memory object. Basically, when a memory area is defined, we can explicitly set its properties so that it will be automatically initialized whenever an application tries to access it. Alternatively, if we do not define the memory area in this way, any access to it without the area having been initialized would trigger an exception of type CX_SHM_NO_ACTIVE_VERSION. Whatever we choose, we have to keep in mind that without automatic pre-load, our ABAP applications will have to check for existence of the memory area and initialize it manually if required.

 

The automatic pre-load option removes this hassle by ensuring that any memory areas that are not yet initialized will be automatically started whenever an application tries to access the data. This means that if your ABAP program tries to GET data from the shared memory object, and the memory area hasn’t been initialized, it will be done “on the fly” – removing the need for handling the above exception in your applications.
Automatic pre-loading requires us to set a few parameters when defining the memory area. It also means we have to add the interface IF_SHM_BUILD_INSTANCE to our root class:

 

8.jpg

 

As a result, the method IF_SHM_BUILD_INSTANCE~BUILD is automatically added to the list of available methods – as already seen.
The method will be empty, however, and in order for pre-loading to work, we need to fill it with some ABAP code!


METHOD IF_SHM_BUILD_INSTANCE~BUILD.


data : l_area type REF TO ZCL_YXX_sharedarea,

       l_root
type ref to ZCL_yxx_staticclass.


**Get Pointer to shared area

l_area
= zcl_yxx_sharedarea=>attach_for_write( ).
** Create an Instance of ZCL_YXX_STATICCLASS
create object l_root area HANDLE l_area.
** Set Initial Value

l_root
->set_data( ).
**Set  the root back into area

l_Area
->set_root( l_root ).
** Commit & Detach

l_area
->detach_commit( ).


ENDMETHOD.


The code above initializes the memory object. This is done by invoking the ATTACH_FOR_WRITE method of the memory area, followed by the instantiation of the root class. When this is done, the SET_DATA method of the memory object is called in order to fill the attributes of class defined in SET_DATA method with some initial data.


Step 2: Creating the Memory Area


In order to create a shared memory area for our newly created memory object, we execute transaction SHMA and provide a name of the shared memory area.

9.jpg

After clicking Create, some properties of the object have to be defined:

10.jpg


Here, we relate our new memory area to the memory object we just created – the root class.

 

Note that we also define the root class also as constructor class for the memory area. This is strictly speaking not mandatory – we could have used another class as our constructor or omitted it altogether. However, since we have decided to use the automatic pre-load option, we have to specify a constructor class, which must implement the IF_SHM_BUILD_INSTANCE interface. For the sake of simplicity, we are using our root class also as the constructor class for our area.


Further, and also related to the automatic pre-load option, we need to check “Automatic area creation” – this ensures the memory object is automatically created whenever it is invoked by a running program – if it does not already exist. As already discussed, the alternative is to manually ensure there is an active memory object whenever you need it – risking an exception of type CX_SHM_NO_ACTIVE_VERSION if this is not the case


Step 3: Using Shared Memory Objects in your ABAP Programs

 

Using the memory object in an ABAP program normally follows the following four steps:

  • Create a reference to the shared memory area class.
  • Call one of the ATTACH methods of the area class to get an area handle for the shared memory area.
  • Call any of the data manipulation methods of the area’s root class.
  • Call a DETACH method to “free” the area handle.

 

When writing an ABAP program to read data from the memory object, we need to define references to both the memory area and the memory object (root class). This is because we will use the ATTACH methods of the memory area to establish a handle for the area, followed by the specific methods of the memory object in order to make use of the attributes.

 

Defining and using an Area Handle

When we defined our memory area using transaction SHMA, a related class was automatically generated. This class provides the methods for establishing a handle for the memory area. We can examine the class using transaction SE24 (it has the same name as the memory area):

11.jpg


The ATTACH methods are, as indicated, used for requesting locks on the memory area. This is a prerequisite for accessing the related memory object.

 

Most of the time, we will use ATTACH_FOR_READ and possibly ATTACH_FOR_UPDATE methods. The ATTACH_FOR_WRITE method is usually only used during the creation of a memory object – as seen in the code for method IF_SHM_BUILD_INSTANCE~BUILD in the previous section.

 

Step 4: Examining the Shared Memory Object


After the shared memory object has been created, we can use transaction SHMM to examine it:

12.jpg

The shared memory object now “lives” in SAP memory, and will remain there until the SAP instance goes down. It can however easily be deleted by selecting it and using the delete button. Note that if the root class is changed in any way (new logic, new methods, adding new parameters or even changing the type or visibility of existing ones etc.), any existing memory objects must be deleted and a new object created – either using the creation program or the mentioned auto start functionality. Failure to do so will result in inconsistencies and lead to dumps when using the shared memory object!

 

Basic Steps Required for Implementing Export/Import when calling in same Session

 

Use Class ZCL_YXX_STATICCLASS  instead of Import/Export Variables using method (SET_DATA, GET_DATA)

 

  • Create parameters as required for your development.
  • In SET_DATA parameter type will be importing and optional and For GET_DATA parameter type will be Exporting.
  • Define Attributes for the parameter you define in above step.
  • Call Method SET_DATA in program where you want to Export data
  • Assign Your parameter in Method -SET_DATA & GET_DATA , with inclusion of Header text for which object you are adding this parameters.


CALLING  SET_DATA  & GET_DATA IN SAME SESSION (Sample Code Below)

 

Call Method SET_DATA in program where you want to Import data as below.

 

CALL METHOD ZCL_YXX_STATICCLASS=>set_data
  EXPORTING
    E_AUART  =
    E_WERKS  =     .


You can pass data to the variable you want


Call Method GET_DATA in program where you want to Export data as below.

 

CALL METHOD ZCL_YXX_STATICCLASS=>get_data
  EXPORTING
    E_AUART  =
    E_WERKS  =     .


You can get data to the variable you want

  
Implementing  Shared Memory Object
(Can be used if export/Importing in Different Sessions).


CALLING  SET_DATA  & GET_DATA IN DIFFERENT SESSION (Sample Code Below)

* Define the area and memory object references

 

DATA : l_area TYPE REF TO ZCL_YXX_sharedarea,

       l_root
TYPE REF TO ZCL_yxx_staticclass.

* Call the area method attach_for_update to get an area handle

 

TRY.

  l_area
= ZCL_YXX_sharedarea=>attach_for_update( ).

 
CATCH cx_shm_no_active_version.

   
WAIT UP TO 1 SECONDS.

    l_area
= ZCL_YXX_sharedarea=>attach_for_update( ).
ENDTRY.

l_root ?= l_area
->get_root( ).

IF l_root IS INITIAL.
CREATE object l_root AREA HANDLE l_area.
ENDIF.

CALL METHOD l_root->set_data
 
EXPORTING
    e_auart
=    < " Pass Data >
    e_werks
=    < " Pass Data >

l_area
->set_root( l_root ).

l_area
->detach_commit( ).

 

*  GET Data Using below sample Program

 

     * Define the area and memory object references

 

DATA : l_area TYPE REF TO ZCL_YXX_sharedarea.

 

     * Call the area method attach_for_read to get an area handle

 

TRY.

  l_area
= ZCL_YXX_sharedarea=>ATTACH_FOR_READ( ).

 
CATCH cx_shm_no_active_version.

   
WAIT UP TO 1 SECONDS.

    l_area
= ZCL_YXX_sharedarea=>ATTACH_FOR_READ( ).
ENDTRY.


CALL METHOD l_area->root->GET_DATA
 
IMPORTING
    I_auart
=    < " Get Data into VAriable >
    I_werks
=    < " Get Data into Variable >

l_area
->DETACH( ).