How to configure the EHCache with CXF in Mule

孤街醉人 提交于 2019-12-11 11:18:53

问题


I have a SOAP webservice as follow :-

<spring:bean id="cacheManager" name="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
        <spring:bean id="cache" name="cache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
            <spring:property name="cacheManager" ref="cacheManager"/>
            <spring:property name="cacheName" value="dbCache"/>
            <spring:property name="maxElementsInMemory" value="10000"/>
            <spring:property name="eternal" value="false"/>
            <spring:property name="timeToIdle" value="${timeToIdle}"/>
            <spring:property name="timeToLive" value="${timeToLive}"/>
            <spring:property name="overflowToDisk" value="true"/>
            <spring:property name="maxElementsOnDisk" value="10000000"/>
            <spring:property name="diskPersistent" value="false"/>
            <spring:property name="diskExpiryThreadIntervalSeconds" value="5"/>
            <spring:property name="memoryStoreEvictionPolicy" value="LRU"/>
            <!-- Cache Expiry -->
        </spring:bean>  
    </spring:beans>


<ee:object-store-caching-strategy name="cachingStrategy" doc:name="cachingStrategy">
    <custom-object-store class="com.anirban.EHCatche.EhcacheObjectStore">
        <spring:property name="cache" ref="cache"/>
    </custom-object-store>
</ee:object-store-caching-strategy>

<!-- Catch Strategy ends -->    



<flow name="ServiceFlow" doc:name="ServiceFlow">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" path="mainData" doc:name="HTTP" />

<ee:cache doc:name="Cache" cachingStrategy-ref="cachingStrategy">
 <cxf:jaxws-service  serviceClass="com.test.services.schema.maindata.v1.MainData"  doc:name="SOAP"/>
 <component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl" doc:name="JavaMain_ServiceImpl"/>
</ee:cache>

</flow>

Now as you can see .. I have wrapped the cxf:jaxws-service and component class under ee:cache block ..

The Webservice fetch a set of Data from DB

My main intention is if I trigger the webservice ,it will fetch the Data from DB from the first time and then for the same request it will fetch from cache for a particular time ... In between if I trigger the service with different request it will again fetch data from DB for the first time for that request and then retrieve from the cache for the subsequent same request ..

It will hold the data in cache for a particular time ..

Now the issue is If I trigger the service .. it's always fetching the Data from DB and it's not holding any data in cache for the same request .. Every time I hit the service it hits the Database directly and the data is not fetched from cache for the same request .. Please help .. how to configure the cache with CXF in Mule


回答1:


Break your service flow in two, using a VM endpoint and serializing both the request and response to string. This will make the event cacheable.

<flow name="ServiceFlow" doc:name="ServiceFlow">
  <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" path="mainData" doc:name="HTTP" />
  <object-to-string-transformer />
  <ee:cache doc:name="Cache" cachingStrategy-ref="cachingStrategy">
    <vm:outbound-endpoint path="cxf.service" exchange-pattern="request-response" />
    <object-to-string-transformer />
  </ee:cache>
</flow>

<flow name="CXFFlow" doc:name="CXFFlow">
  <vm:inbound-endpoint path="cxf.service" exchange-pattern="request-response" />
  <cxf:jaxws-service  serviceClass="com.test.services.schema.maindata.v1.MainData"  doc:name="SOAP"/>
  <component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl" doc:name="JavaMain_ServiceImpl"/>
</flow>



回答2:


So, the final solution is as David suggested, by breaking the flow into two and also using <object-to-string-transformer /> for serializing both the request and response which is very important for a Cache to perform



来源:https://stackoverflow.com/questions/26639270/how-to-configure-the-ehcache-with-cxf-in-mule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!