Book Image

SAP ABAP Advanced Cookbook

By : Rehan Zaidi
Book Image

SAP ABAP Advanced Cookbook

By: Rehan Zaidi

Overview of this book

ABAP (Advanced Business Application Programming) is SAP's proprietary 4th Generation Language (4GL). SAP core is written almost entirely in ABAP.ABAP is a high level programming language used in SAP for development and other customization processes."SAP ABAP Advanced Cookbook"ù covers advanced SAP programming applications with ABAP. It teaches you to enhance SAP applications by developing custom reports and interfaces with ABAP programming. This cookbook has quick and advanced real world recipes for programming ABAP.It begins with the applications of ABAP Objects and ALV tips and tricks. It then covers Design Patterns and Dynamic Programming in detail.You will also learn the usage of quality improvement tools such as transaction SAT, SQL Trace, and the Code Inspector.Simple transformations and its application in Excel Downloading will also be discussed, as well as the newest topics of Adobe Interactive Forms and the consumption and creation of Web services. The book comes to an end by covering advanced usage of Web Dynpro for ABAP and the latest advancement in Floorplan Manager.
Table of Contents (22 chapters)
SAP ABAP Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Call transaction SE24; enter a suitable name to your root class, as shown in the following screenshot. On the Properties tab, we need to make sure that the Shared-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

  2. 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:

  3. 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.

  4. Next, the shared memory area should be created. This is done via transaction SHMA.

  5. 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 the root 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:

  6. This will also generate an area class by entering the same name ZCL_MY_EMP_AREA.

  7. 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:

  1. Two object references my_handle and my_root are defined, one for area class and the other for root class.

  2. The static method attach_for_write of the area class zcl_my_emp_area is called.

  3. The CREATE OBJECT with the area handle, my_handle must then be called.

  4. The root and the created area instance must be linked using the set_root method of the handle.

  5. The set_data method is called with the relevant number and name.

  6. The detach_commit method of the area 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.