Spring 集成与缓存 - 如何将对象从出站网关回复通道添加到缓存中?
Spring Integration & Caching - How to add objects into cache from outbound-gateway reply-channel?
我是 spring 集成和缓存的新手,想弄清楚如何将从出站网关接收到的对象添加到缓存中。无法找出所需的配置。
根据以下配置,我从 rest api 收到的对象正在被记录:
信息:com.domain.IpAddress@74589991
我打算使用 ehcache/caffiene,任何指点都会有所帮助。
<int:channel id="requestChannel"/>
<int:channel id="outboundChannel"/>
<int:channel id="replyChannel"/>
<int:channel id="autoCompleteJsonResponseChannel"/>
<int:gateway id="restResponseService" default-request-channel="requestChannel" service-interface="com.domain.service.RestResponseService" />
<int-http:outbound-gateway id="out" request-channel="requestChannel"
http-method="GET"
url="https://api.ipify.org/?format=json"
expected-response-type="java.lang.String"
reply-channel="autoCompleteJsonResponseChannel"
/>
<int:json-to-object-transformer
input-channel="autoCompleteJsonResponseChannel"
type="com.domain.IpAddress"
output-channel="outboundChannel" />
<int:logging-channel-adapter id="outboundChannel" level="INFO"/>
编辑 2:
现在我按照建议更改了出站网关:
<int-http:outbound-gateway id="out" request-channel="requestChannel"
http-method="GET"
url="https://api.ipify.org/?format=json"
expected-response-type="java.lang.String"
reply-channel="autoCompleteJsonResponseChannel">
<int-http:request-handler-advice-chain>
<cache:advice>
<cache:caching cache="myCache">
<cache:cacheable method="getCurrentIpAddress" />
</cache:caching>
</cache:advice>
</int-http:request-handler-advice-chain>
</int-http:outbound-gateway>
并定义ehcache配置如下:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >
<property name="cacheManager" ref="ehcache" />
</bean>
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true"/>
</bean>
在我的服务 class 中,定义了可缓存的方法:
package com.domain.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.domain.IpAddress;
import com.domain.RestResponse;
@Service("restResponseService")
public class RestResponseServiceImpl implements RestResponseService{
@Autowired
private IpAddress ipAddress;
@Cacheable("myCache")
public IpAddress getCurrentIpAddress(String name) {
// TODO Auto-generated method stub
return ipAddress;
}
@CacheEvict(value = "myCache", allEntries = true)
public void refreshAllResults() {
// TODO Auto-generated method stub
}
}
但缓存仍然不起作用。我在这里缺少什么。
您可以在网关中这样做:
<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>
换句话说,由于 Spring 中的缓存完全基于 AOP,因此要弄清楚 request-handler-advice-chain
可以使用任何 Advice
实现并不复杂。在这种情况下,我们提出 <cache:cacheable>
建议,通过适当的方法选择和缓存密钥作为传入有效负载为我们工作。
我是 spring 集成和缓存的新手,想弄清楚如何将从出站网关接收到的对象添加到缓存中。无法找出所需的配置。
根据以下配置,我从 rest api 收到的对象正在被记录:
信息:com.domain.IpAddress@74589991
我打算使用 ehcache/caffiene,任何指点都会有所帮助。
<int:channel id="requestChannel"/>
<int:channel id="outboundChannel"/>
<int:channel id="replyChannel"/>
<int:channel id="autoCompleteJsonResponseChannel"/>
<int:gateway id="restResponseService" default-request-channel="requestChannel" service-interface="com.domain.service.RestResponseService" />
<int-http:outbound-gateway id="out" request-channel="requestChannel"
http-method="GET"
url="https://api.ipify.org/?format=json"
expected-response-type="java.lang.String"
reply-channel="autoCompleteJsonResponseChannel"
/>
<int:json-to-object-transformer
input-channel="autoCompleteJsonResponseChannel"
type="com.domain.IpAddress"
output-channel="outboundChannel" />
<int:logging-channel-adapter id="outboundChannel" level="INFO"/>
编辑 2:
现在我按照建议更改了出站网关:
<int-http:outbound-gateway id="out" request-channel="requestChannel"
http-method="GET"
url="https://api.ipify.org/?format=json"
expected-response-type="java.lang.String"
reply-channel="autoCompleteJsonResponseChannel">
<int-http:request-handler-advice-chain>
<cache:advice>
<cache:caching cache="myCache">
<cache:cacheable method="getCurrentIpAddress" />
</cache:caching>
</cache:advice>
</int-http:request-handler-advice-chain>
</int-http:outbound-gateway>
并定义ehcache配置如下:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >
<property name="cacheManager" ref="ehcache" />
</bean>
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true"/>
</bean>
在我的服务 class 中,定义了可缓存的方法:
package com.domain.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.domain.IpAddress;
import com.domain.RestResponse;
@Service("restResponseService")
public class RestResponseServiceImpl implements RestResponseService{
@Autowired
private IpAddress ipAddress;
@Cacheable("myCache")
public IpAddress getCurrentIpAddress(String name) {
// TODO Auto-generated method stub
return ipAddress;
}
@CacheEvict(value = "myCache", allEntries = true)
public void refreshAllResults() {
// TODO Auto-generated method stub
}
}
但缓存仍然不起作用。我在这里缺少什么。
您可以在网关中这样做:
<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>
换句话说,由于 Spring 中的缓存完全基于 AOP,因此要弄清楚 request-handler-advice-chain
可以使用任何 Advice
实现并不复杂。在这种情况下,我们提出 <cache:cacheable>
建议,通过适当的方法选择和缓存密钥作为传入有效负载为我们工作。