Saturday, 23 September 2017

Working with Statelessness in JAX-RS

Before Pitching in to the "How the Stateless works with the JAX-RS" , I want to discuss here about the basics of the stateless and how it can be created and what are the configurations required .


So What is Statelessness ? It is said that , while processing the request , the server does not maintain any state or the Information that is required , If it is not maintaining it, how can be proceed yes the client sends the information that is required to process the request this behavior is called as the statelessness .

JAX-RS implementation With ATG Commerce has support for this statelessness and we can see how we are going to enable it .

We all know that, we are generating the Ear for the ATG Application from the RunAssembler , and we are passing all the arguments while executing the RunAssembler.So to enable the Statelessness we have to pass the stateless argument for the RunAssembler . Once it if passed then the Required Modules are built along with it . I am just pasting the Snippet you can use the same for the ant build .For other Maven you see their manuals before using it .

               <arg line="stateless"/>

Now you have enabled the stateless mode .So the Beauty of the Statelessnessis that, all the session scope components in ATG will be converted in to the Request Scope Components .

So now its our time to See more about the functional aspects .

So what is x-ocstatedata ? I was mentioning about the required data must be passed as part of the Request from the client , this will be carried by this x-ocstatedata .

Did I need to create this x-ocstatedata every time? . No you need to create it OOTB takes care of creating it , but it is your responsiblity to put the data in the way OOTB expects then the x-ocstatedata is created automatically, So How to put it ? let see in the below .

Create a class extends GenericService implements MapLoadableService . When you create like this
there should be two methods that needs to implemented .

1))@Override
public void loadService(Map<String, Object> paramMap) {
}

//This method is the place where we have to read the previous state from the x-ocstatedata and used for the current request .

2)@Override
public Map<String,Object>getProperties() {
}
// This is the method where we need to write a logic so that it returns the request data that has to be put as part of the X-ocstatedata . Which is of type Map<String,Object>. This method will be called after each endpoint finishes . 


For More Information on the Implementation details you can refer the ProfileLoadableService OOTB class  . Where they have used the Profile component from the x-ocstatedata .

You can inject the created component in the RequestStateManager as below 

loadableServicePaths=\
                                       /atg/userprofiling/ProfileLoadableService


Once it is declared in the component path it will be called before it hits any service and end of the service calls .

By Following the above way you can Handle the stateless data in the JAX-RS.


No comments:
Write comments