我正在尝试将我现有的应用程序代码移植到使用Spring-data-gemfire启动Gemfire。所以我基本上是将我的区域配置从cache. xml移动到Spring上下文。
使用的版本: Gemfire6.6.3.2Spring-data-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() });
}
}
我使用对等缓存子区域创建了一个简单的测试,该子区域具有一个注册的CacheListener,并配置了SDG,并且侦听器被按预期回调。可能还有其他因素或原因导致这对您不起作用…
2.我在测试中使用SDG 1.4.0. RELEASE和GemFire7.0.2.12和8。但是,您应该能够使用我的测试和示例SDGXML配置来测试您的设置…
@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);
}
}
}
以及相应的SDGXML配置…
<?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?)。
这应该从1.3.3开始工作,请参阅https://jira.spring.io/browse/SGF-219.如果您确定它不起作用,请向该JIRA添加注释。