Shared objects are SAP's answer to an age-old request by programmers. Up until now, the only way to share variables between programs was to use EXPORT/IMPORT TO/FROM MEMORY. This method was very slow because the "sending" program had to copy variables from its memory to the shared memory, and the "receiving" program had to copy these variables from the shared memory into its own program memory. This generated two copies for basically nothing.
Here are the benefits of shared objects:
- Copy-free: very fast access
- Version management: you can handle many version of the variables at the same time
- Preloading: you can now request a specific variable no matter if the "sending" program already created them
- Automatic propagation from a database or to different application servers
- Could be client dependant
- Easy monitoring
Step 1 :
You have to define a Root class using SE24, In this class you will defined all variables you want to share in the attributes tab, This class must be "Shared memory enabled" :
Step 2 : You have to define your shared memory object using the transaction SHMA. It is important to put the class you created in step 1 in the field "root class".
Step 3 : You have to write a program to write to the shared memory.
Code: Select all
DATA : hdl TYPE REF TO zcl_sm_area, root TYPE REF TO zcl_root_area. * Create the default instance for write, * when we attach for write, an exclusive lock is performed on the shared memory object hdl = zcl_sm_area=>attach_for_write( ). *when we create our root object to access variables, we must use the new keyword addition AREA HANDLE CREATE OBJECT root AREA HANDLE hdl. * Create the link between the root object and the shared memory object hdl->set_root( root ). * put data in the shared memory variable root->big_var = 'hello world!'. * commit hdl->detach_commit( ). Step 4 : You have to create a program to read from the shared memory DATA : hdl TYPE REF TO zcl_sm_area. * Open default instance for read hdl = zcl_sm_area=>attach_for_read( ). *Access root object component to write the variable WRITE / hdl->root->big_var. *Release lock hdl->detach( ).
(Courtesy Dany Charbonneau)