简体   繁体   中英

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. 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. 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 .

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 . And exactly for this method Spring Integration applies his Advices (eg RequestHandlerRetryAdvice ).

Here you should configure a cacheManager bean, choose cache name and determine a key for cache entry. In sample above #a0 is a Message object from handleRequestMessage arguments. So, you can specify any SpEL expression against message properties (payload and headers). And the result of handleRequestMessage will be stored in the cache.

And when you provide the same parameters for HTTP reqeust, the result will be returned just from cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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