Skip to Content

Applies To: Jdk 1.6, CE 7.3 / 7.4

Author(s):      Biplab Ray

Company:      Tata Consultancy Services

Created on:    16th  February 2015


Author Bio:

Biplab Ray is working for Tata Consultancy Services as Assistant Consultant and development of SAP EP, composite applications using CAF, BPM, BRM, WebDynpro for Java. He also works in technologies like Java/J2EE and has depth understanding on eSOA, Webservice, Enterprise Service, XML.


Requirement:

Let say, we have to generate unique numbers by an application as well as we have to handle x no of treads at single point of time and also we have to consider duplicate number should not generate.


Implementation:

To solve the issue, we should implement the lock and unlock mechanism.

Let say, we have one CAF BO named: P2S Number Range, where only last number would persist. If we have multiple threads and all the thread have access to that particular table at single point of time, then we might get duplicate number from the application because threads would read the configuration of the business object. To solve the issue we have should introduce the lock and unlock mechanism on CAF Business Object.

At single point of time only one thread should have access to the table. After the thread complete its work then only application should trigger the unlock method and after that another thread should get access and do its work. In this way we would be able to stop generating the duplicate numbers.

We have to lock the business object, so we have to read GUID of the CAF BO.

Below are the imports which are required.

import com.sap.caf.rt.bol.IBusinessObjectNodeServiceBase;

import com.sap.caf.rt.exception.CAFLockException;

import com.sap.caf.rt.exception.CAFPessimisticLockException;

import com.example.bo.modeled.bonode.p2snumberrange.p2snumberrange.P2SNumberRangeServiceLocal;

We have to do one ejb injection into our implementation ejb as below for CAF Business Object.

@EJB(name=”com.example.bo.modeled.bonode.p2snumberrange.p2snumberrange.P2SNumberRange”)

  private P2SNumberRangeServiceLocal numberRangeServiceLocal;


We have to declare below line for the type of lock.

private static final char modeW = IBusinessObjectNodeServiceBase.MODE_WRITE;

Now we have to implement the unlock method as below:

public boolean unlock(String string_GUID){

            boolean b_Unlock = false;

            try {

                                    numberRangeServiceLocal.unlock(string_GUID, modeW);

                                    b_Unlock = true;

                        } catch (CAFPessimisticLockException e) {

                                    // TODO Auto-generated catch block

                                    e.printStackTrace();

                                    b_Unlock = false;

                        }

            return b_Unlock;

    }

Now we have to implement the lock method as below:

private boolean getLock(String string_GUID){

            boolean b_Lock = false;

         

            try {

                        if(string_GUID != null && !””.equals(string_GUID)){

                        numberRangeServiceLocal.lock(string_GUID, modeW);

                        b_Lock = true;      

                        }

                        } catch (CAFPessimisticLockException e) {

                                    // TODO Auto-generated catch block

                                    e.printStackTrace();

                                    b_Lock = false;

                        }

            return b_Lock;

    }

Now we have to implement the business method where we would call the lock and unlock methods.

public void businessMethod(){

To read the GUID we have to write below code.

P2SNumberRangeBean bean_GUID = new P2SNumberRangeBeanImpl();

String string_GUID = bean_GUID._objectGuid;

/*Get Lock*/

boolean b_GetLock = getLock(string_GUID);

if(b_GetLock){

  /*Write Business Logic to Update the CAF BO & If any other business logic*/ 

// Now we have to unlock the CAF BO

unlock(string_GUID);

}

}

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply