Skip to content

Shared Memory Class Usage | Write&Read

Create a class that contains data that you want to keep in memory. Set Shared Memory Enabled mode enabled for this class on properties. Define setter and getter. Then go to SHMA t-code. Give your class name as the root class.

Shared memory class

Shared Memory Enabled Class

class ZPSAA_CL_BUDGET_AVC_SHARED definition
  public
  final
  create public
  shared memory enabled .

public section.

  interfaces IF_SHM_BUILD_INSTANCE .

  methods GET_ATTRIBUTES
    exporting
      !E_ATTRIBUTES type ZPSAA_S_AV_CONTROL_SHARED .
  methods SET_ATTRIBUTES
    importing
      !I_ATTRIBUTES type ZPSAA_S_AV_CONTROL_SHARED .
  PROTECTED SECTION.
  PRIVATE SECTION.
    data ATTRIBUTES type ZPSAA_S_AV_CONTROL_SHARED .
ENDCLASS.



CLASS ZPSAA_CL_BUDGET_AVC_SHARED IMPLEMENTATION.


  METHOD get_attributes.
    e_attributes = attributes.
  ENDMETHOD.


  METHOD if_shm_build_instance~build.
     DATA: area  TYPE REF TO ZPSAA_CL_AVC_SHARED_AREA, "area defined on SHMA tcode
          root  TYPE REF TO ZPSAA_CL_BUDGET_AVC_SHARED,
          excep TYPE REF TO cx_root.

    DATA: attributes       TYPE ZPSAA_S_AV_CONTROL_SHARED.

    TRY.
        area = ZPSAA_CL_AVC_SHARED_AREA=>attach_for_write( ).

      CATCH cx_shm_error INTO excep.
        RAISE EXCEPTION TYPE cx_shm_build_failed
          EXPORTING
            previous = excep.
    ENDTRY.

    CREATE OBJECT root AREA HANDLE area.

    CALL METHOD root->set_attributes      
      EXPORTING
        i_attributes       = attributes.

    area->set_root( root ).

    area->detach_commit( ).
  ENDMETHOD.


  METHOD set_attributes.
    attributes = i_attributes.
  ENDMETHOD.
ENDCLASS.

Shared Memory Write

class ZPSAA_CL_BUDGET_AVC_SHARED definition
  public
  final
  create public
  shared memory enabled .

public section.

  interfaces IF_SHM_BUILD_INSTANCE .

  methods GET_ATTRIBUTES
    exporting
      !E_ATTRIBUTES type ZPSAA_S_AV_CONTROL_SHARED .
  methods SET_ATTRIBUTES
    importing
      !I_ATTRIBUTES type ZPSAA_S_AV_CONTROL_SHARED .
  PROTECTED SECTION.
  PRIVATE SECTION.
    data ATTRIBUTES type ZPSAA_S_AV_CONTROL_SHARED .
ENDCLASS.



CLASS ZPSAA_CL_BUDGET_AVC_SHARED IMPLEMENTATION.


  METHOD get_attributes.
    e_attributes = attributes.
  ENDMETHOD.


  METHOD if_shm_build_instance~build.
     DATA: area  TYPE REF TO ZPSAA_CL_AVC_SHARED_AREA, "area defined on SHMA tcode
          root  TYPE REF TO ZPSAA_CL_BUDGET_AVC_SHARED,
          excep TYPE REF TO cx_root.

    DATA: attributes       TYPE ZPSAA_S_AV_CONTROL_SHARED.

    TRY.
        area = ZPSAA_CL_AVC_SHARED_AREA=>attach_for_write( ).

      CATCH cx_shm_error INTO excep.
        RAISE EXCEPTION TYPE cx_shm_build_failed
          EXPORTING
            previous = excep.
    ENDTRY.

    CREATE OBJECT root AREA HANDLE area.

    CALL METHOD root->set_attributes      
      EXPORTING
        i_attributes       = attributes.

    area->set_root( root ).

    area->detach_commit( ).
  ENDMETHOD.


  METHOD set_attributes.
    attributes = i_attributes.
  ENDMETHOD.
ENDCLASS.

Shared Memory Write

"fill structure
DATA(attributes) = VALUE zpsaa_s_av_control_shared( applik = i_applik
  value_type      =    i_value_type
  activity        =     i_activity
  fiscal_year     = i_fiscal_year
  fiscal_year_glg = i_fiscal_year_glg
  user            = i_user
  objnr           = lv_objnr
  posit           = i_posit
  fund            = i_fund
  farea           = i_farea
  distributable   = e_max_distributable
  assigned        = i_assigned
  flg_cover       = i_flg_cover
  org_level       = i_org_level
  flg_check_soft  = i_flg_check_soft ).

DATA:lo_area TYPE REF TO zpsaa_cl_avc_shared_area,
     lo_root TYPE REF TO zpsaa_cl_budget_avc_shared.

TRY.
    zpsaa_cl_avc_shared_area=>build( ).
  CATCH cx_shma_not_configured.
    RETURN.
  CATCH cx_shm_inconsistent.
    RETURN.
  CATCH cx_shm_build_failed.
    RETURN.
ENDTRY.

TRY.
    lo_area = zpsaa_cl_avc_shared_area=>attach_for_update( ).

  CATCH cx_shm_pending_lock_removed.
    RETURN.
  CATCH cx_shm_change_lock_active.
    RETURN.
  CATCH cx_shm_version_limit_exceeded.
    RETURN.
  CATCH cx_shm_exclusive_lock_active.
    RETURN.
  CATCH cx_shm_inconsistent.
    RETURN.
  CATCH cx_shm_no_active_version.
    RETURN.
ENDTRY.

lo_root ?= lo_area->get_root( ).
IF lo_root IS  INITIAL.
  CREATE OBJECT lo_root AREA HANDLE lo_area.
ENDIF.

"write data to shared memory class
CALL METHOD lo_root->set_attributes      
  EXPORTING
    i_attributes = attributes.  "comes from foreground program

lo_area->set_root( lo_root ).
lo_area->detach_commit( ).

Read Memory

TRY.
        DATA(area) = zpsaa_cl_avc_shared_area=>attach_for_read( ).
        
        "read memory data
        area->root->get_attributes(
          IMPORTING
              e_attributes = DATA(attributes) ).

        area->detach( ).
      CATCH cx_root.
        CLEAR attributes.
        RETURN.
    ENDTRY.

You can use t-code SHMM to see and analyze Shared Memory Objects.

Leave a Reply

Your email address will not be published. Required fields are marked *