Introduction:
The so-called “soft state” mode enables the SAP NetWeaver Gateway runtime to process several requests in one ABAP application server session, similar to stateful behavior.
- copied from andre.fischer 's blog post 😄 ( I am too lazy, I guess )
But in the latest releases (>7.5, not sure about the exact version), you can control the soft state behavior like opening & closing a soft state whenever it is required, and
this blog post is about exploring that option 🙂
Links that you should check
You can check the blog post by
andre.fischer for more information on Soft state:
https://blogs.sap.com/2014/10/14/how-to-use-soft-state-support-for-odata-services/
Help link:
https://help.sap.com/viewer/68bf513362174d54b58cddec28794093/7.52.5/en-US/25a27ab5fb614b538bc44228fc...
Exploration
[
Before checking this out, if you don't know about the soft state, check the blog by Andre, that is mentioned in the above help link]
As I already mentioned in the intro the soft state in the earlier versions doesn't have the functionality to start and stop on demand. It used to start once the OData service entity operation is performed.
This was a big issue, as we usually need the soft state session on a particular entity or an action.
F
or e.g., before updating a Sales Order, we can activate the soft state and create a lock on the sales order, In the meanwhile UI can update the sales order. Once the operation is done, we will close the soft state, then all those locks or buffered data is deleted along with the session.
[
Of course, Soft state has other important purposes, but in the above example I am talking particularly about the locking use case]
To try this feature, I will take a small example as detailed below:
- An OData service with only one entity, let's call it as Carrier.
- Two Function Imports
- One for Starting the Soft State
- Other for Stoping the Soft State.
- We will use Carrier EntitySet to perform Count Operation
- This count doesn't count the records, but just a dummy count.
- I will have a static variable, that will be incremented every time the count operation is performed.
- But The static variable will only persist the incremented value only if there the soft state is activated else(makes sense) else it will always show the value '1'.
Step 1: Create a basic OData service with an entity as mentioned above.
Step 2: Redefine the MPC_EXT "DEFINE" method and call the below code
In MPC_EXT, pass the mode as "Delayed"
METHOD define.
super->define( ).
model->set_soft_state_enabled(
EXPORTING
iv_soft_state_enabled = abap_true " Enable Soft-State
iv_mode = /iwbep/if_mgw_core_types=>gcs_soft_state_mode-delayed " Enable delayed invocation
).
ENDMETHOD.
Step 3: Create two function imports in SEGW as described in the above steps and write the below code
METHOD /iwbep/if_mgw_appl_srv_runtime~execute_action.
CASE iv_action_name.
WHEN 'StartSoftState'.
/iwbep/if_mgw_conv_srv_runtime~soft_state_session_start( ).
WHEN 'StopSoftState'.
/iwbep/if_mgw_conv_srv_runtime~soft_state_session_end( ).
ENDCASE.
ENDMETHOD.
Here we will call the methods soft_state_session_start & end, to manipulate the soft session instance.
Step 4: And redefine the OPERATION_END & OPERATION_START which are required for the soft state.
Step 5: Now create a static variable "Count" in the DPC_EXT class of type integer.
Step 6: Increment the "Count" variable whenever the getEntitySet method is called.
METHOD carrierset_get_entityset.
count = count + 1.
es_response_context-count = count.
ENDMETHOD.
And Activate the soft state in the **MAINT_SERVICE and provide a timeout value in the SICF service.
Note: Even if you manually start the soft state using the function import as described above, once the timeout period is completed, the soft state session will be auto-deleted.
That's it, Now we just need to play with the requests:
Call the EntitySet with count param multiple times, but it will return only the value '1'.
/sap/opu/odata/sap/ZDEMOSS_SRV/CarrierSet/$count
But once you call the function import "StartSoftState" , then the soft state is opened
sap/opu/odata/sap/ZDEMOSS_SRV/StartSoftState
After that, if we call the EntitySet multiple times, the count value gets incremented.
If we want to close the soft state, call the StartSoftState function import, then the soft state will be deleted.
Thant's it, folks, thanks for reading this blog post. Let me know your thoughts
🙂
PS: This question of starting & stopping soft state has been asked many times at SAP Community and I was surprised to see this feature provided by SAP. In fact, one of my friend's colleague was having the same question, I said it is not possible
😄 😄 , but after some time, he came back saying that this feature is provided by SAP in the recent releases. I don't have his contact, but thanks for sharing this man
🙂
Cheers!!
Mahesh