Tuesday 5 December 2023

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.

Motivation:-


In this blog you will learn how to use shared memory as alternative option of ABAP Memory(Import/Export).

Business Scenario:- Many of times we need Import & Export at the time of SAP Enhancement or other requirements so Here use of shared memory which is also alternate option of Import/ Export and as per SAP standard guideline this approach is highly recommended.

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
Class Creation with Shared Memory

Add this Interface IF_SHM_BUILD_INSTANCE in Interface

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.

Create any table as per your requirement- in my case I have created a range table.

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
Assignment of Internal Table

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
Internal Table

Save and Activated

Open T-Code – SHMA , Give any name and create

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
T-Code SHMA

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
T-Code – SHMA inside Screen

Give below setting as per the screen shot.

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
SHMA Setting

Now Run T-Code – SE24 and open class ZCL_ROOT_SHMA in the change mode and double click on SHMA and double click on Method – IF_SHM_BUILD_INSTANCE~BUILD.

method IF_SHM_BUILD_INSTANCE~BUILD.

 data: lr_area TYPE REF TO ZCL_ROOT_SHMA_USE,
       lr_root type REF TO ZCL_ROOT_SHMA,
       lr_exp type REF TO cx_root.

  try.
  lr_area = ZCL_ROOT_SHMA_USE=>attach_for_write( ).

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

  CREATE OBJECT lr_root AREA HANDLE lr_area.
  lr_area->set_root( lr_root ).
  lr_area->detach_commit( ).
  endmethod.

Save and Activate the above code.

Now below code Report A & Report B ,I have created as example , Here will export internal table in the shared memory.

Report A-
TRY.
  zcl_root_shma_use=>build( ).
  CATCH cx_shma_not_configured. “ SHM Administration: Area Property Is Not Configured
  CATCH cx_shma_inconsistent.   “ SHM Administration: Inconsistent Attribute Combination
  CATCH cx_shm_build_failed.    “ Constructor Run Failed

ENDTRY.

TRY.
    DATA(lr_area) = zcl_root_shma_use=>attach_for_update(
*                 inst_name   = cl_shm_area=>default_instance
*                 attach_mode = cl_shm_area=>attach_mode_default
*                 wait_time   = 0
                   ).
  CATCH cx_shm_inconsistent.           “ Different Definitions Between Program and Area
  CATCH cx_shm_no_active_version.      “ No active version exists for an attach
  CATCH cx_shm_exclusive_lock_active.  “ Instance Already Locked
  CATCH cx_shm_version_limit_exceeded. “ No Additional Versions Available
  CATCH cx_shm_change_lock_active.     “ A write lock is already active
  CATCH cx_shm_parameter_error.        “ Passed parameter has incorrect value
  CATCH cx_shm_pending_lock_removed.   “ Shared Objects: Waiting Lock Was Deleted

ENDTRY.

DATA lr_root TYPE REF TO zcl_root_shma.
TRY.
    lr_root ?= lr_area->get_root( ).
  CATCH cx_shm_already_detached.
ENDTRY.

DATA wa_kunnr TYPE range_vbeln.
DATA gt_kunnr TYPE TABLE OF range_vbeln.
wa_kunnr-sign = ‘I’.
wa_kunnr-option = ‘EQ’.
wa_kunnr-low  = ‘12345’.
wa_kunnr-high = ‘9009000’.
APPEND wa_kunnr TO gt_kunnr.

lr_root->lt_kunnr = gt_kunnr.
lr_area->set_root( lr_root ).
lr_area->detach_commit( ).

Now Use below code for Import Internal table from shared Memory

Report B-
TRY.
    DATA(lr_area) = zcl_root_shma_use=>attach_for_read( ).
  CATCH cx_shm_inconsistent.          " Different Definitions Between Program and Area
  CATCH cx_shm_no_active_version.     " No active version exists for an attach
  CATCH cx_shm_read_lock_active.      " Request for a Second Read Lock
  CATCH cx_shm_exclusive_lock_active. " Instance Already Locked
  CATCH cx_shm_parameter_error.       " Incorrect parameter passed
  CATCH cx_shm_change_lock_active.    " A Change Lock Is Already Active

ENDTRY.

DATA LR_ROOT TYPE REF TO zcl_root_shma.
TRY.
LR_ROOT ?= LR_AREA->get_root( ).
          CATCH cx_shm_already_detached. " Handle Already Released

ENDTRY.
DATA  GT_kunnr type TABLE OF RANGE_VBELN.
GT_kunnr = LR_ROOT->lt_kunnr.

LR_AREA->detach_area( ).

Run Report A and see the Report B output in Debug mode

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
Debug mode for Report B

Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.
Captured data in the Internal table from Shared Memory

No comments:

Post a Comment