我使用对等缓存子区域创建了一个简单的测试,该子区域具有配置了 SDG 的已注册 CacheListener,并且侦听器按预期被回调。可能还有其他因素或原因导致这对您不起作用...
- 不知道你的整个 SDG XML 配置,但也许如果有竞争的 GemFire 配置在发挥作用,例如使用本地 GemFire cache.xml 和具有冲突区域定义的 Spring 配置(但你确实声明 afterRegionCreate 正在被调用),例如......
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 一起使用?)。