我正在尝试使用Spring-data-gemfire将我现有的应用程序代码移植到启动Gemfire。所以我基本上将我的区域配置从cache.xml移到spring上下文。
使用的版本: Gemfire 6.6.3.2 弹簧数据的GemFire-1.3.4 Jdk 7
这一切都很好,直到我需要配置缓存监听器。区域上的一个简单的缓存侦听器可以工作,但我无法获得缓存侦听器来处理子区域。
作为一个例子,我有以下地区。我希望在/ User / Details / Address区域更新时通知CacheUpdateListener(实现CacheListener接口)。我知道Gemfire支持它,因为我已经使用了cache.xml方式。但有没有人知道我是否可以使用Spring-data-gemfire。这是我尝试过的,也没有用。
<gfe:replicated-region id="VCCache" name="User" scope="distributed-no-ack">
<gfe:replicated-region name="Details" scope="distributed-no-ack">
<gfe:replicated-region name="Address" scope="distributed-ack">
<gfe:cache-listener>
<bean class="com.vc.cache.CacheUpdateListener" />
</gfe:cache-listener>
</gfe:replicated-region>
</gfe:replicated-region>
</gfe:replicated-region>
**编辑:添加了侦听器代码 这是听众。我没有把所有过度使用的功能放在这里只是为了简洁。
public class CacheUpdateListener implements CacheListener<Object, Object>
{
private static final Logger LOGGER = LoggerFactory.getLogger(CacheUpdateListener.class);
private String name = "defaultName";
@Override
public void afterCreate(EntryEvent<Object, Object> event)
{
LOGGER.info("[afterCreate] region [{}] key [{}] created remote [{}] with value [{}]",
new Object[] { event.getRegion().getFullPath(), event.getKey(), event.isOriginRemote(), event.getNewValue() });
}
@Override
public void afterUpdate(EntryEvent<Object, Object> event)
{
LOGGER.info("[afterUpdate] region [{}] key [{}] updated remote [{}] with value [{}] old value [{}]",
new Object[] { event.getRegion().getFullPath(), event.getKey(), event.isOriginRemote(), event.getNewValue(), event.getOldValue() });
}
}
答案 0 :(得分:0)
这应该从1.3.3开始,见https://jira.spring.io/browse/SGF-219。如果您确定它不起作用,请向该JIRA添加评论。
答案 1 :(得分:0)
我使用Peer Cache Subregion创建了一个简单的测试,其中注册的CacheListener配置了SDG,并且按预期调用了侦听器。可能还有其他因素或原因导致这对你不起作用......
2.我在测试中使用SDG 1.4.0.RELEASE和GemFire 7.0.2.12和8。但是,您应该能够使用我的测试和示例SDG XML配置来测试您的设置...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/subRegionCacheListener.xml")
@SuppressWarnings("unused")
public class SubRegionCacheListenerTest {
private static final Stack<EntryEvent<Integer, String>> entryEvents = new Stack<EntryEvent<Integer, String>>();
@Resource(name = "/Parent/Child")
private Region<Integer, String> child;
@Test
public void testCacheListenerCallback() {
assertNotNull("The '/Parent/Child' Cache Sub-Region was not properly configured and initialized!", child);
assertEquals("Child", child.getName());
assertEquals("/Parent/Child", child.getFullPath());
assertTrue(child.isEmpty());
assertTrue(entryEvents.isEmpty());
child.put(1, "TEST");
assertFalse(child.isEmpty());
assertEquals(1, child.size());
assertEquals("TEST", child.get(1));
assertFalse(entryEvents.isEmpty());
EntryEvent event = entryEvents.pop();
assertNotNull(event);
assertEquals(1, event.getKey());
assertNull(event.getOldValue());
assertEquals("TEST", event.getNewValue());
assertTrue(entryEvents.isEmpty());
child.put(1, "TESTING");
assertFalse(child.isEmpty());
assertEquals(1, child.size());
assertEquals("TESTING", child.get(1));
assertFalse(entryEvents.isEmpty());
event = entryEvents.pop();
assertNotNull(event);
assertEquals(1, event.getKey());
assertEquals("TEST", event.getOldValue());
assertEquals("TESTING", event.getNewValue());
assertTrue(entryEvents.isEmpty());
child.remove(1);
assertTrue(child.isEmpty());
event = entryEvents.pop();
assertNotNull(event);
assertEquals(1, event.getKey());
assertEquals("TESTING", event.getOldValue());
assertNull(event.getNewValue());
}
public static final class SubRegionCacheListener extends CacheListenerAdapter<Integer, String> {
@Override
public void afterCreate(final EntryEvent<Integer, String> event) {
entryEvents.push(event);
}
@Override
public void afterDestroy(final EntryEvent<Integer, String> event) {
entryEvents.push(event);
}
@Override
public void afterUpdate(final EntryEvent<Integer, String> event) {
entryEvents.push(event);
}
}
}
相应的SDG XML配置......
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<util:properties id="gemfireProperties">
<prop key="name">SpringGemFirePeerCacheSubRegionCacheListenerTest</prop>
<prop key="mcast-port">0</prop>
<prop key="log-level">config</prop>
</util:properties>
<gfe:cache properties-ref="gemfireProperties"/>
<gfe:replicated-region id="Parent">
<gfe:replicated-region name="Child">
<gfe:cache-listener>
<bean class="org.spring.data.gemfire.cache.SubRegionCacheListenerTest$SubRegionCacheListener"/>
</gfe:cache-listener>
</gfe:replicated-region>
</gfe:replicated-region>
</beans>
希望这有帮助。
最有可能的是,这里可能存在配置问题,或者GemFire 6.x可能存在问题(尽管你声明它适用于GemFire的cache.xml?)。