您如何使用SAP ERP为B2B应用程序设计Hybris的定价集成

时间:2017-05-22 13:30:25

标签: hybris

如果ERP中有一个定价模块可以实时计算每个用户的价格,那么我们是否有任何方法可以在没有性能权衡的情况下获得它?

1 个答案:

答案 0 :(得分:0)

您可以维护缓存以避免多次调用ERP系统。

以下是您可以尝试实现缓存的示例代码 -

<强> CustomCache.java

public class CustomCache
{

    @Resource(name = customCacheRegion)
    protected CacheAccess customCacheAccess;

    //Fetch result from cache
    public ResultData readCachedData(final B2BUnitModel customer, final Date date)
    {
       return (ResultData) customCacheAccess.get(createCacheKey(customer, date));
    }

    //Update result to cache
    public void cacheResult(final B2BUnitModel customer, final Date date,
        final ResultData resultData)
    {

       try
       {
          customCacheAccess.put(createCacheKey(customer, date), resultData);

       }
       catch (final SAPHybrisCacheException e)
       {
        //error
       }
    }

    protected CustomCacheKey createCacheKey(final B2BUnitModel customer, final Date date)
    {
       return new CustomCacheKey(customer, date);
    }

}

缓存密钥 -

  public class CustomCacheKey extends AbstractCacheKey
  {
        private final B2BUnitModel customer;

        private final Date date; 

        @Override
        public int hashCode()
        {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + ((customer == null) ? 0 : customer.hashCode());
            return result;
        }

        @Override
        public boolean equals(final Object obj)
        {
           if (obj == null)
           {
              return false;
           }

           if (!super.equals(obj))
           {
               return false;
           }

           final CustomCacheKey customCacheKey = (CustomCacheKey ) obj;
           if (customer == null)
           {
                if (customCacheKey.customer != null)
                {
                   return false;
                }
           }
           else if (!customer.equals(customCacheKey.customer))
           {
               return false;
           }

           if (date == null)
           {
               if (customCacheKey.date != null)
               {
                 return false;
           }
        }
        else if (!DateUtils.isSameDay(date, customCacheKey.date))
        {
           return false;
        }

        return true;
     }
}

* - spring.xml -

 <bean id="customCacheRegion" parent="sapCoreCacheRegion">
    <constructor-arg name="name"
        value="customCacheRegion" />
    <constructor-arg name="maxEntries" value="10000" />
    <constructor-arg name="evictionPolicy" value="FIFO" />
    <constructor-arg name="statsEnabled" value="true" />
    <constructor-arg name="exclusiveComputation" value="false" />
    <constructor-arg name="ttlSeconds" value="300" />
</bean>

<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="cacheRegionsList" />
    <property name="targetMethod" value="add" />
    <property name="arguments">
        <ref bean="customCacheRegion" />
    </property>
</bean>

因此,Cache是​​一种映射,您可以在其中定义键值对并从键本身获取缓存值。

最后在您的服务层,在调用ERP系统之前,只需检查特定客户(或您的情况下的某些其他条件),数据是否在缓存中可用。如果它可用,只需从缓存中直接获取它,否则调用ERP系统并将结果更新到缓存。