Creating a shared memory object
This recipe shows how to store the instances of your classes in the shared memory of the application server. A number of programs may access these objects that reside on the application server shared memory.
Two classes are necessary for shared memory, namely the area
class and the area root
class. The root
class is necessary for storing (encapsulating) the data that are to be stored in the shared memory. An area
class may comprise of various instances that may consist of a number of versions.
An important concept shown in this recipe is the CREATE OBJECT
statement with the addition AREA HANDLE
. This will create the object in the application server that is shared memory pointed to by the area handle myarea
.
Getting ready
Prior to writing the code for storing objects in shared memory, an area root
class must be created and a shared memory area be defined using transaction SHMA
.
The steps required for creating a root class are:
Call transaction
SE24
; enter a suitable name to yourroot
class, as shown in the following screenshot. On the Properties tab, we need to make sure that theShared-Memory
checkbox is switched on.Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com . If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you
We have named it
ZCL_MY_ROOT
. We will then define two Instance Attributes, NUMBER and NAME, having private visibility, as shown in the following screenshot:Two suitable methods, SET_DATA and GET_DATA, are also added to the class. The SET_DATA method contains code that imports number and name and assigns to the attributes NUMBER and NAME of the class. The GET_DATA method does just the opposite, that is, it exports the NUMBER and NAME attribute for a given shared memory object.
Next, the shared memory area should be created. This is done via transaction
SHMA
.Enter a suitable name and click on the Create button. We have typed the name
ZCL_MY_EMP_AREA
. On the screen that appears, enter the description of the area. Also, enter the name of theroot
class created earlier in the Root Class field. You may leave the Client-Specific Area checkbox unchecked as it is not required for our recipe. Now, save your entries. Refer to the following screenshot:This will also generate an
area
class by entering the same nameZCL_MY_EMP_AREA
.This area class will contain the necessary methods used for reading, changing, and creating the
area
, such as ATTACH_FOR_UPDATE, ATTACH_FOR_READ, and ATTACH_FOR_WRITE.
How to do it...
For creating the set of code that writes object's contents to the shared memory, follow these steps:
Two object references
my_handle
andmy_root
are defined, one forarea
class and the other forroot
class.The static method
attach_for_write
of thearea
classzcl_my_emp_area
is called.The
CREATE OBJECT
with the area handle,my_handle
must then be called.The root and the created area instance must be linked using the
set_root
method of the handle.The
set_data
method is called with the relevant number and name.The
detach_commit
method of thearea
class is then called.
How it works...
In the shared memory-writing program, the statements collectively make the writing of object in the shared memory. Let us see how the program code works.
An area instance version needs to be created before any data may be written in the shared memory on the application server. The attach_for_write
static method is used for this purpose and returns a handle to the area instance created in the application server memory. This imposes write
lock on the version.
The CREATE OBJECT
statement is then called with the name of the created handle. This creates a root
object in the area
instance of the shared memory. The link between the area
instance and the root
class is created using the set_root
method. The set_data
method is then called for the root reference my_root
and supplied with the name and number of the employee, which are then stored in the shared area. Finally, the detach_commit
method is called and the write
lock is released.
Once the program has run successfully, you may see the created object in the shared memory using the shared memory transaction SHMM
. This will appear as your area class name ZCL_MY_EMP_AREA. Refer to the following screenshot:
Double-click on the name of area to view the details, as shown in the following screenshot:
There's more...
The read program is somewhat similar. However, instead of the attach_for_write
method used earlier, we will use attach_for_read
. The same instance name is passed and the handle is received. The method imposes a read
lock on the area instance. Then, the get_data
method of the root
object is called using the area handle, my_handle
. This returns the employee name and number stored earlier into the variables name and number respectively.
Finally, the detach
method is called and the read
lock is released.
While creating the shared memory area, if we select the Transactional Area checkbox, the area becomes transactional. In this case, the modifications to the area
instance versions are not active immediately after the call of detach_commit
method. Rather, they become active when the next database commit is executed.