简体   繁体   English

Spring集成应用程序和缓存

[英]Spring integration application and cache

Have spring integration application with inbound http gateway and outbound http gateway, between those i want to have cache, to avoid unnessesary requests. 具有入站http网关和出站http网关的Spring集成应用程序,在我想拥有缓存之间,以避免不必要的请求。 The only solution i have is to add interceptor with cache and router after it that routes cahced results back to reply channel, and non cached to outbound, but this solution seems tricky and ugly to me. 我唯一的解决方案是添加拦截器缓存和路由器后,它将cahced结果路由回到回复通道,并且非缓存到出站,但这个解决方案对我来说似乎很棘手和丑陋。 Interceptor with cache also works good when inbound gateway has same channel for request and reply, (when returns new message with same headers but different payload, its considered as reply) but its not the case I can use. 当入站网关具有相同的请求和回复通道时,具有缓存的拦截器也很有效(当返回具有相同标头但具有不同有效负载的新消息时,它被视为回复)但不是我可以使用的情况。

Any better ideas for this? 对此更好的想法?

More elegant solution can be achieved with <request-handler-advice-chain> and Spring Cache Advice . 使用<request-handler-advice-chain>Spring Cache Advice可以实现更优雅的解决方案。

So, your solution may be like this: 所以,你的解决方案可能是这样的:

<int-http:outbound-gateway>
   <int-http:request-handler-advice-chain>
        <cache:advice>
              <cache:caching cache="foo">
                    <cache:cacheable method="handle*Message" key="#a0.payload"/>
              </cache:caching>
        </cache:advice>
   </int-http:request-handler-advice-chain>
</int-http:outbound-gateway>

Where handle*Message is handleRequestMessage method of HttpRequestExecutingMessageHandler . 其中handle*MessageHttpRequestExecutingMessageHandler handleRequestMessage方法。 And exactly for this method Spring Integration applies his Advices (eg RequestHandlerRetryAdvice ). 正是对于这种方法,Spring Integration应用了他的建议(例如RequestHandlerRetryAdvice )。

Here you should configure a cacheManager bean, choose cache name and determine a key for cache entry. 在这里,您应该配置cacheManager bean,选择缓存名称并确定缓存条目的key In sample above #a0 is a Message object from handleRequestMessage arguments. 在上面的示例中, #a0是来自handleRequestMessage参数的Message对象。 So, you can specify any SpEL expression against message properties (payload and headers). 因此,您可以针对消息属性(有效负载和标头)指定任何SpEL表达式。 And the result of handleRequestMessage will be stored in the cache. handleRequestMessage的结果将存储在缓存中。

And when you provide the same parameters for HTTP reqeust, the result will be returned just from cache. 当您为HTTP reqeust提供相同的参数时,结果将仅从缓存返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM