Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member196098
Participant

SHARED MEMORY

As you already know there is often cases that we need to share common data across programs.

This is a result of business requirements for the purpose of this blog i like to call it as Buffer.

The meaning i use here is not the shared buffer concept in abap which is also another topic.

So we need to store in a place like buffer and share the memory.

As an example: In the latest project i was working on there was a requirement where i needed to store all the data that user has entered inside a Goods Movement

Till the user goes and saves sit  i should be able to store the data in the Z interface that i developed.

So i needed to store the data which was a complex one.

Export to MEMORY ID concept is the most primitive one but not very powerful when you are needing to store smth very complex like an object

So I had to unleash the Shared memory and learn it to apply in the project.

Lets start applying here !!!

STEPS TO CREATE SHARED MEMORY CONCEPT IN ABAP

In the example below i like to give you a shared memory where you can store an object or data.

Step 1- Create the Shared memory Object- What will you store?

Create the object called  zcl_shared_root-DO NOT FORGET TO MARK IT AS SHARED MEMORY ENABLED

ZCL_SHARED_ROOT

class ZCL_SHARED_ROOT definition

  public

  final

  create public

  shared memory enabled .

public section.

  interfaces IF_SHM_BUILD_INSTANCE .

  data OREF type ref to OBJECT .

  data DREF type ref to DATA .

  methods SET_OREF

    importing

      !IO_OREF type ref to OBJECT .

  methods GET_OREF

    returning

      value(RO_OREF) type ref to OBJECT .

  methods SET_DATA

    importing

      !IO_DREF type ref to DATA .

  methods GET_DATA

    returning

      value(RO_DREF) type ref to DATA .

protected section.

private section.

ENDCLASS.

CLASS ZCL_SHARED_ROOT IMPLEMENTATION.

  method GET_DATA.

  ro_dref = dref.

  endmethod.

  method GET_OREF.

    ro_oref = oref.

  endmethod.

  method SET_DATA.

   dref = io_dref.

  endmethod.

  method SET_OREF.

  oref = io_oref.

  endmethod.

ENDCLASS.

Attributes: oref as reference to another object

                dref as a reference to any data

Step 2- Create the Shared memory Area- The memory area that wıill store your object

Check SHMA is quite useful !!


Question: Automatic Preload is a good feature, when we first read the data it will throw a dump so to avoid this we need to use automatic preload feature!!


Here the name of the Area is zcl_shared_areaç notice the root class name is our root at above and auto are creation is ticked !!

This is because of the interface we implemented at the zcl_shared_root object

if_shm_build_instance




Step 3 - Create a the Object we want to Save called zcl_object_saved

Note that its shared memory enabled as well!!

Example data taken from sflight database!!

zcl_object_saved

class zcl_object_saved definition

   public

   final

   create public

   shared memory enabled .

   public section.

     data flight_list type sflight_tab1 .

     methods add_flight

       importing

         !is_flight type sflight .

     methods get_flight_list

       returning

         value(rt_flight_list) type sflight_tab1 .

   protected section.

   private section.

endclass.

class zcl_object_saved implementation.

* <SIGNATURE>---------------------------------------------------------------------------------------+

* | Instance Public Method ZCL_OBJECT_SAVED->ADD_FLIGHT

* +-------------------------------------------------------------------------------------------------+

* | [--->] IS_FLIGHT                      TYPE        SFLIGHT

* +--------------------------------------------------------------------------------------</SIGNATURE>

   method add_flight.

     read table flight_list transporting no fields

     with key carrid = is_flight-carrid

              connid = is_flight-connid

              fldate = is_flight-fldate

              .

     if sy-subrc ne 0.

       append initial line to flight_list reference into data(lr_flight).

       lr_flight->* = is_flight.

     endif.

   endmethod.

* <SIGNATURE>---------------------------------------------------------------------------------------+

* | Instance Public Method ZCL_OBJECT_SAVED->GET_FLIGHT_LIST

* +-------------------------------------------------------------------------------------------------+

* | [<-()] RT_FLIGHT_LIST                 TYPE        SFLIGHT_TAB1

* +--------------------------------------------------------------------------------------</SIGNATURE>

   method get_flight_list.

     rt_flight_list = flight_list.


Step 4 - Implement the Build of the Automatic preload!! interfaces if_shm_build_instance


Implement the code inside ZCL_SHARED_ROOT build!!


ZCL_SHARED_ROOT

method if_shm_build_instance~build.

     data: lo_handle type ref to zcl_shared_area,

           lo_root type ref to zcl_shared_root,

           lo_object_saved type ref to zcl_object_saved,

           ls_sflight type sflight

              .

*Autamatic PreLoad!!

     try.

         lo_handle = zcl_shared_area=>attach_for_write( ).

         create object lo_root area handle lo_handle.

         lo_handle->set_root( lo_root ).

         create object lo_root->oref area handle lo_handle type zcl_object_saved.

         lo_object_saved ?= lo_root->oref.

**get data

         select single * from sflight

           into ls_sflight.

**Set a data

         lo_object_saved->add_flight( is_flight = ls_sflight ).

         lo_handle->detach_commit( ).

       catch cx_shm_error

         into data(lo_exception).

         raise exception type cx_shm_build_failed

           exporting

             previous = lo_exception.

     endtry.

if  invocation_mode = cl_shm_area=>invocation_mode_auto_build.

       call function 'DB_COMMIT'.

     endif.

   endmethod.


Step 5 -Create a test program!!

ZTEST_SHARED

*&---------------------------------------------------------------------*

*& Report  ZTEST_SHARED

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

report ztest_shared.

class lcl_test definition.

   public section.

     class-methods: test_auto_build.

     class-methods: test_write.

endclass.

class lcl_test implementation.

   method test_auto_build.

     data: lo_handle type ref to zcl_shared_area,

              lo_root type ref to zcl_shared_root,

              lo_object_saved type ref to zcl_object_saved,

              ls_sflight type sflight

                 .

*Autamatic PreLoad!!

     try.

         lo_handle = zcl_shared_area=>attach_for_read( ).

         lo_object_saved ?= lo_handle->root->oref.

         lo_handle->detach( ).

       catch cx_shm_attach_error into data(lo_exc).

     endtry.

   endmethod.

   method test_write.

     data: lo_handle type ref to zcl_shared_area,

              lo_root type ref to zcl_shared_root,

              lo_object_saved type ref to zcl_object_saved,

              ls_sflight type sflight

                 .

     try.

         lo_handle = zcl_shared_area=>attach_for_write( ).

         create object lo_root area handle lo_handle.

         lo_handle->set_root( lo_root ).

         create object lo_root->oref area handle lo_handle type zcl_object_saved.

         lo_object_saved ?= lo_root->oref.

**get data

         select single * from sflight

           into ls_sflight.

**Set a data

         lo_object_saved->add_flight( is_flight = ls_sflight ).

         lo_handle->detach_commit( ).

       catch cx_shm_error

         into data(lo_exception).

         raise exception type cx_shm_build_failed

           exporting

             previous = lo_exception.

     endtry.

   endmethod.

endclass.

start-of-selection.

   break solend.

   call method lcl_test=>test_auto_build.


Step 6- Observe the memory Area Created!!


Go to shma and choose the memory area-zcl_shared_area



As you can see you can check the memory allocated for the Shared memory and inside our memory we managed to store another object!!


This could be a much complex object!!


Conclusion: So i can read this memory area and access the object from another program which is great is not it.


I hope you find my blog useful!!


Thanks all






10 Comments