Friday, September 25, 2020

Creating a new Registry to cache data from particular Table in WCS

Creating a new Registry to cache data from particular Table in WCS

Let's consider a Scenario:
We have a Table from which the data (data is not being changed frequently) is being fetched an enormous amount of time which leads to too many DB hits.

For this scenario/requirement, we can create a WCS registry which has the complete table data cached to the registry we are about to create.

In our case, 
Let's consider a table X_SHIPPINGPRICEEXCEPTION which has the shipping price of some items/products, The shipping price of these products is fixed (products shipping is not variable based on weight) and it is persisted in this X_SHIPPINGPRICEEXCEPTION table.

This table has only two fields ie PARTNUMBER and PRICE.

The requirement was to store the complete details of this table to the newly created REGISTRY and The data can be retrieved from this registry to avoid the DB Hits.

The registry name I have chosen will be XXXXShippingExceptionRegistry and the registry classname be XShippingExceptionRegistry.

Let's start creating a Registry class.

Create a Registry class (XShippingExceptionRegistry) that implements interface com.ibm.commerce.registry.Registry

The Registry class has below listed methods which we override and write our logic based on the requirements:
initialize() -- gets called when the server starts.
refresh() -- Gets called when user or admin tries to clear/refres/update the registry from admin console
Which imports to import?

import com.ibm.commerce.registry.Registry;
import com.ibm.commerce.registry.RegistryManager;

In my case the methods look like below: 

private Map mappingData ;
public Map getMappingData() {
    return mappingData;
}

@Override public void initialize() throws Exception { 
RegistryManager.singleton().addRegistry("XShippingExceptionRegistry", this); 
//This is the Custom method that is used to get the table contents using 
JDBC Helper loadContents(); 

private void loadContents() {
if(mappingData == null){ 
mappingData = new HashMap<>(); 
ServerJDBCHelperAccessBean jdbcHelper = new ServerJDBCHelperAccessBean();
List shippingExceptionPriceInfoList =null;
String getShippingExceptionPriceSql = "SELECT PARTNUMBER, PRICE FROM X_SHIPPINGPRICEEXCEPTION";
try {
shippingExceptionPriceInfoList = jdbcHelper.executeQuery(getShippingExceptionPriceSql); 
} catch (RemoteException | NamingException | SQLException | CreateException e) { 
//Add loggers here
}
processSqlResult(shippingExceptionPriceDBList); 
}

private void processSqlResult(List shippingExceptionPriceDBList) {
if (shippingExceptionPriceDBList != null && !shippingExceptionPriceDBList.isEmpty()) {
java.util.Iterator shipPriceIterator = shippingExceptionPriceDBList.iterator(); 
String skunumber = null; 
String shipExceptionPrice = null; 
while (shipPriceIterator.hasNext()) {
List vObj = (List) shipPriceIterator.next(); 
if (vObj.get(0) != null) { 
skunumber = vObj.get(0).toString();

if (vObj.get(1) != null) { 
shipExceptionPrice = vObj.get(1).toString(); 
if (skunumber != null && shipExceptionPrice != null) { 
mappingData.put(skunumber, shipExceptionPrice); 

//Add Logger to print the size of the list if required: "Populating registry is completed for total count : "+ mappingData.size()" 
}

The refresh method would look like below which clears and populates the data to the registry. @Override 
public void refresh() throws Exception { 
mappingData.clear(); 
loadContents();

We need to add entry in WC-server.xml for the Registry.

<Registries>
<registry name="XXXXShippingExceptionRegistry" regClassName="com.academy.commerce.shipping.registry.XShippingExceptionRegistry"/>
</Registries>

How to clear the registry or the cached data?

If we have to clear or refresh the Registry data, we can do it from the admin console with "registry refresh" option -> select the registry -> update the registry.

How to use this registry to retrive the data from shipping registry and use it?

XShippingExceptionRegistry XShippingExceptionRegistry = (XShippingExceptionRegistry) RegistryManager.singleton().getRegistry("XXXXShippingExceptionRegistry"); 
Map shippingExceptionMapping = academyXShippingExceptionRegistry.getMappingData(); 
xShippingExceptionPrice = new BigDecimal(shippingExceptionMapping.get(partnumber));


Thanks a lot for reading my Blog. 

Sharing Is Caring!!!!!!

No comments:

Post a Comment