Blogs
Introduction
Where I live in the United States, it is nearly time for the Christmas Holidays. Any day now we will have the first snow of the season. People are busy putting up lights and Christmas trees. The shopping centers are full of eager shoppers. Given this is the season for sharing of gifts, what better time could there be for exploring the new ABAP Shared Memory functionality in WebAS 640. Now you might be saying to yourself, that we already have access to shared memory areas via the import and export statements. What could be so special about this new functionality? Well first of all, this is read access to shared memory without the need to copy it into user session memory. Second the new shared memory is implemented through ABAP Objects. Therefore you are provided with robust tools to interact with shared memory through code. You also have good administrative tools to monitor and support these memory areas. This is what I want to focus on today. We will look at how to create a shared memory class, but I will really focus on what I have seen to be some of the major advantages to this new technology. Creating our Root Class All of the data we want to store or expose through the shared memory area has to be contained in a Root Class. Later when we work with our shared memory, you will see that all we are really doing is getting a pointer to an instance of this root class. We then access or manipulate the data through this class. We will start off quite simply by creating a normal ABAP Class from SE80. We will name it ZCL_SDN_SHARED_ROOT. ![]() ![]() ![]() ![]() Next up we have to create a Shared Memory Area. This Area is another ABAP Class. It will become the information broker for your root classes. It will control access to the instances of the root class. That's right, you might have multiple versions of the root class alive at once. Therefore the Area manages these versions. For instance you might have a reader working with the active version while a separate writer is building a new version. This ability to version, which comes from the Area object, is one of the other major advantages of the shared memory technology. To create our Area we go to transaction SHMA. We are going to name our area class ZCL_SDN_SHARED_AREA. ![]() ![]() ![]() ![]() ![]() We are ready to write our first program. This program will get read access to our shared memory area. We can then access the GET_MESSAGE method of our root class to write out our message attribute. Write Program If you ran the read program you got a short dump, right. That's because our shared memory area doesn't exist yet. Later we will learn how we can turn on Auto Initialization so that on the first read request the memory area is created. For now let's just write a program to do that. This program will create an instance of the root object. It will then write to the message attribute and set the root back into the area. After you run this write program, you can go back to the first and now you should get results without the short dump. ![]() Before we move on to more advanced techniques, let's take a minute to look at the monitoring and administration tools for shared memory. If you successfully ran the write and then the read program, you should be able to see your area and root instance in these tools. You start off by going to transaction SHMM. From the first screen of the monitoring tool, we can see all of shared memory areas. We can also see memory usage, number of instances, number of versions, and the status breakdown of the versions. As you can see our new little shared memory area isn't taking up much space. ![]() ![]() As you saw from the example earlier, we got a short dump when we tried to read from the unloaded memory area. Wouldn't it be much nicer if the memory area could automatically preload itself on the first read attempt. Well that's exactly what we are going to do now. To do this we are first going to have to add the interface, IF_SHM_BUILD_INSTANCE, to our root class. ![]() ![]() ![]() ![]() We have looked at how to write initial data into our shared memory. But remember our write program created a new instance of the root class. We also need to be able to update the data in an existing instance. To do that we will use the attach_for_update. Because the update might trigger the BUILD method as well if the shared area is initial, we have to account for the Asynchronous activity again. Then we try to get a pointer to the existing root instance. If one doesn't exist, we just create one. Now while I was writing this weblog, I noticed some interesting attributes on the attach_for_update that aren't there on the attach for read. They were attach_mode and wait_time. It looks as though maybe SAP plans to do the wait logic for us. I decided to give these new options a try: All I got was this very interesting short dump. I guess I will just have to wait a little longer for this functionality. Maybe Support Stack 10 will have it. ![]() In this section I wanted to demonstrate the power of shared objects based upon their OO implementation. Instead of just accessing a memory area, you are accessing an ABAP class. That means that this shared memory area can also contain logic to manipulate the data as it is read. For this demonstration, I will append a random number between 1 and 100 to the end of the message in the GET_MESSAGE method. I have just been dying for an excuse to use the new Random Number classes in 640 anyway. The following is the code for the new GET_MESSAGE method. The following are the new results: ![]() I was going to close out the weblog with an example of how I could use OO events in my shared memory class. I thought it would be really neat to have two consumers of a shared memory area. One would write and the other would register for an ON_Update event. When the first program wrote an update, the second program's event handler would respond to this update and popup a message. Unfortunately I received the following message as soon as I added an event to the root class. ![]() Thomas Jung is an SAP NetWeaver Product Manager focusing on Custom Development - including ABAP, MDM APIs, UI Strategy, and NetWeaver Voice. |
shared memory
最新推荐文章于 2021-11-30 22:46:36 发布