如何为 Hybris 与 SAP ERP for B2B 应用程序设计定价集成
How do you design a Pricing Integration for Hybris with SAP ERP for B2B application
如果ERP中有一个定价模块,可以实时为每个用户计算价格,我们有没有办法在不牺牲性能的情况下得到它?
您可以维护Cache,避免多次调用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);
}
}
缓存 KEY -
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>
所以缓存是一种映射,您可以在其中定义键值对并从键本身获取缓存值。
最后在您的服务层,在调用 ERP 系统之前,只需检查特定客户(或您的情况下的其他情况)的数据是否在缓存中可用。如果可用,则直接从缓存中获取,否则调用 ERP 系统并将结果更新到缓存。
如果ERP中有一个定价模块,可以实时为每个用户计算价格,我们有没有办法在不牺牲性能的情况下得到它?
您可以维护Cache,避免多次调用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);
}
}
缓存 KEY -
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>
所以缓存是一种映射,您可以在其中定义键值对并从键本身获取缓存值。
最后在您的服务层,在调用 ERP 系统之前,只需检查特定客户(或您的情况下的其他情况)的数据是否在缓存中可用。如果可用,则直接从缓存中获取,否则调用 ERP 系统并将结果更新到缓存。