Fixes JIRA issue SGF-259 involving circular dependencies between an Async Event Queue and corresponding, registered Async Event Listener.

This commit is contained in:
John Blum
2014-02-27 22:36:58 -08:00
parent bc7e284fbc
commit 2656a25056
3 changed files with 217 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.gemfire.config;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
@@ -46,6 +47,10 @@ public class AsyncEventQueueParser extends AbstractSingleBeanDefinitionParser {
builder.addPropertyValue("asyncEventListener", asyncEventListener);
if (asyncEventListener instanceof RuntimeBeanReference) {
builder.addDependsOn(((RuntimeBeanReference) asyncEventListener).getBeanName());
}
String cacheRefAttribute = element.getAttribute("cache-ref");
String cacheName = (StringUtils.hasText(cacheRefAttribute) ? cacheRefAttribute
@@ -79,6 +84,7 @@ public class AsyncEventQueueParser extends AbstractSingleBeanDefinitionParser {
int i = 0;
String name = regionName + ".asyncEventQueue#" + i;
while (parserContext.getRegistry().isBeanNameInUse(name)) {
i++;
name = regionName + ".asyncEventQueue#" + i;

View File

@@ -0,0 +1,163 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.wan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEvent;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventListener;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
/**
* The AsyncEvenntQueueWithListenerTest class is a test suite of test cases testing the circular references between
* an Async Event Queue and a registered AsyncEventListener that refers back to the Async Event Queue on which the
* listener registered.
* <p/>
* @author John Blum
* @see org.junit.Test
* @see org.junit.runner.RunWith
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @see com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue
* @since 1.0.0
*/
@ContextConfiguration("asyncEventQueueWithListener.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class AsyncEventQueueWithListenerIntegrationTest {
@Resource(name = "Q1")
private AsyncEventQueue queueOne;
@Resource(name = "Q2")
private AsyncEventQueue queueTwo;
@Resource(name = "Q3")
private AsyncEventQueue queueThree;
@Test
public void testAsyncEventQueueOneAndListenerConfiguration() {
assertNotNull(queueOne);
assertEquals("QueueOne", queueOne.getId());
assertFalse(queueOne.isPersistent());
assertFalse(queueOne.isParallel());
assertEquals(50, queueOne.getMaximumQueueMemory());
assertEquals(4, queueOne.getDispatcherThreads());
assertTrue(queueOne.getAsyncEventListener() instanceof TestAsyncEventListener);
assertSame(queueOne, ((TestAsyncEventListener) queueOne.getAsyncEventListener()).getQueue());
}
@Test
public void testAsyncEventQueueTwoAndListenerConfiguration() {
assertNotNull(queueTwo);
assertEquals("QueueTwo", queueTwo.getId());
assertFalse(queueTwo.isPersistent());
assertTrue(queueTwo.isParallel());
assertEquals(150, queueTwo.getMaximumQueueMemory());
assertEquals(1, queueTwo.getDispatcherThreads());
assertTrue(queueTwo.getAsyncEventListener() instanceof TestAsyncEventListener);
assertEquals("ListenerTwo", ((TestAsyncEventListener) queueTwo.getAsyncEventListener()).getName());
}
@Test
public void testAsyncEventQueueThreeAndListenerConfiguration() {
assertNotNull(queueThree);
assertEquals("QueueThree", queueThree.getId());
assertFalse(queueThree.isPersistent());
assertFalse(queueThree.isParallel());
assertEquals(25, queueThree.getMaximumQueueMemory());
assertEquals(2, queueThree.getDispatcherThreads());
assertTrue(queueThree.getAsyncEventListener() instanceof TestAsyncEventListener);
assertSame(queueThree, ((TestAsyncEventListener) queueThree.getAsyncEventListener()).getQueue());
}
/**
* The QueueAsyncEventListener class is an implementation of the AsyncEventListener interface that contains
* a reference to the AsyncEventQueue upon which it is registered.
* <p/>
* @see com.gemstone.gemfire.cache.asyncqueue.AsyncEvent
* @see com.gemstone.gemfire.cache.asyncqueue.AsyncEventListener
* @see com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue
*/
@SuppressWarnings("unused")
public static class TestAsyncEventListener implements AsyncEventListener {
private AsyncEventQueue queue;
private String name;
public TestAsyncEventListener() {
this.queue = null;
}
public TestAsyncEventListener(final AsyncEventQueue queue) {
this.queue = queue;
}
public void init() {
getQueue();
System.out.printf("%1$s initialized!%n", this);
}
public AsyncEventQueue getQueue() {
Assert.state(queue != null, String.format("A reference to the Async Event Queue on which this listener"
+ " (%1$s) has been registered was not properly configured!", this));
return queue;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public void setQueue(final AsyncEventQueue queue) {
this.queue = queue;
}
@Override
public boolean processEvents(final List<AsyncEvent> events) {
return false;
}
@Override
public void close() {
}
@Override
public String toString() {
return (StringUtils.hasText(getName()) ? getName() : getClass().getName());
}
}
}

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:p="http://www.springframework.org/schema/p"
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="peerCacheConfigurationSettings">
<prop key="name">springGemFirePeerCache</prop>
<prop key="log-level">config</prop>
<prop key="mcast-port">0</prop>
</util:properties>
<gfe:cache properties-ref="peerCacheConfigurationSettings"/>
<gfe:async-event-queue id="Q1" name="QueueOne" persistent="false" parallel="false" maximum-queue-memory="50"
dispatcher-threads="4">
<gfe:async-event-listener ref="asyncEventListener"/>
</gfe:async-event-queue>
<bean id="asyncEventListener" class="org.springframework.data.gemfire.wan.AsyncEventQueueWithListenerIntegrationTest$TestAsyncEventListener"
init-method="init">
<property name="queue" ref="Q1"/>
</bean>
<gfe:async-event-queue id="Q2" name="QueueTwo" persistent="false" parallel="true" maximum-queue-memory="150">
<gfe:async-event-listener>
<bean class="org.springframework.data.gemfire.wan.AsyncEventQueueWithListenerIntegrationTest$TestAsyncEventListener"
p:name="ListenerTwo"/>
</gfe:async-event-listener>
</gfe:async-event-queue>
<bean id="anotherAsyncEventListener" class="org.springframework.data.gemfire.wan.AsyncEventQueueWithListenerIntegrationTest$TestAsyncEventListener"
init-method="init">
<property name="queue" ref="Q3"/>
</bean>
<gfe:async-event-queue id="Q3" name="QueueThree" persistent="false" parallel="false" maximum-queue-memory="25"
dispatcher-threads="2">
<gfe:async-event-listener ref="anotherAsyncEventListener"/>
</gfe:async-event-queue>
</beans>