SGF-571 - Remove explicit call to start() in ContinuousQueryListenerContainer.afterPropertiesSet().

This commit is contained in:
John Blum
2016-11-22 00:30:20 -08:00
parent 6d8bee2b34
commit aa28010b4e
2 changed files with 28 additions and 39 deletions

View File

@@ -77,7 +77,7 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
InitializingBean, DisposableBean, SmartLifecycle { InitializingBean, DisposableBean, SmartLifecycle {
// Default Thread name prefix is "ContinuousQueryListenerContainer-". // Default Thread name prefix is "ContinuousQueryListenerContainer-".
public static final String DEFAULT_THREAD_NAME_PREFIX = String.format("%1$s-", ClassUtils.getShortName( public static final String DEFAULT_THREAD_NAME_PREFIX = String.format("%s-", ClassUtils.getShortName(
ContinuousQueryListenerContainer.class)); ContinuousQueryListenerContainer.class));
private boolean autoStartup = true; private boolean autoStartup = true;
@@ -96,11 +96,11 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private Queue<CqQuery> continuousQueries = new ConcurrentLinkedQueue<CqQuery>(); private Queue<CqQuery> continuousQueries = new ConcurrentLinkedQueue<>();
private QueryService queryService; private QueryService queryService;
private Set<ContinuousQueryDefinition> continuousQueryDefinitions = new LinkedHashSet<ContinuousQueryDefinition>(); private Set<ContinuousQueryDefinition> continuousQueryDefinitions = new LinkedHashSet<>();
private String beanName; private String beanName;
private String poolName; private String poolName;
@@ -113,10 +113,6 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
Assert.state(queryService != null, "QueryService was not properly initialized"); Assert.state(queryService != null, "QueryService was not properly initialized");
initialized = true; initialized = true;
if (isAutoStartup()) {
start();
}
} }
/* (non-Javadoc) */ /* (non-Javadoc) */
@@ -140,7 +136,7 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
} }
} }
catch (BeansException ignore) { catch (BeansException ignore) {
Assert.notNull(PoolManager.find(poolName), String.format("No GemFire Pool with name [%1$s] was found", Assert.notNull(PoolManager.find(poolName), String.format("No GemFire Pool with name [%s] was found",
poolName)); poolName));
} }
@@ -176,7 +172,7 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
* @see org.springframework.core.task.SimpleAsyncTaskExecutor#SimpleAsyncTaskExecutor(String) * @see org.springframework.core.task.SimpleAsyncTaskExecutor#SimpleAsyncTaskExecutor(String)
*/ */
protected TaskExecutor createDefaultTaskExecutor() { protected TaskExecutor createDefaultTaskExecutor() {
return new SimpleAsyncTaskExecutor(beanName != null ? String.format("%1$s-", beanName) return new SimpleAsyncTaskExecutor(beanName != null ? String.format("%s-", beanName)
: DEFAULT_THREAD_NAME_PREFIX); : DEFAULT_THREAD_NAME_PREFIX);
} }
@@ -216,16 +212,19 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
try { try {
cq.execute(); cq.execute();
} }
catch (QueryException ex) { catch (QueryException e) {
throw new GemfireQueryException(String.format("Could not execute query [%1$s]; state is [%2$s].", throw new GemfireQueryException(String.format("Could not execute query [%1$s]; state is [%2$s].",
cq.getName(), cq.getState()), ex); cq.getName(), cq.getState()), e);
}
catch (RuntimeException ex) {
throw new GemfireQueryException(String.format("Could not execute query [%1$s]; state is [%2$s].",
cq.getName(), cq.getState()), ex);
} }
} }
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
@Override
public synchronized void stop() { public synchronized void stop() {
if (isRunning()) { if (isRunning()) {
doStop(); doStop();
@@ -237,11 +236,6 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
} }
} }
public void stop(final Runnable callback) {
stop();
callback.run();
}
private void doStop() { private void doStop() {
for (CqQuery cq : continuousQueries) { for (CqQuery cq : continuousQueries) {
try { try {
@@ -253,6 +247,7 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
} }
} }
@Override
public void destroy() throws Exception { public void destroy() throws Exception {
stop(); stop();
closeQueries(); closeQueries();
@@ -313,6 +308,7 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
* @return a boolean value indicating whether this CQ listener container automatically starts. * @return a boolean value indicating whether this CQ listener container automatically starts.
* @see org.springframework.context.SmartLifecycle#isAutoStartup() * @see org.springframework.context.SmartLifecycle#isAutoStartup()
*/ */
@Override
public boolean isAutoStartup() { public boolean isAutoStartup() {
return autoStartup; return autoStartup;
} }
@@ -345,6 +341,7 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
* *
* @param name the name of the bean in the factory. * @param name the name of the bean in the factory.
*/ */
@Override
public void setBeanName(String name) { public void setBeanName(String name) {
this.beanName = name; this.beanName = name;
} }
@@ -467,11 +464,8 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
return cq; return cq;
} }
catch (RuntimeException ex) { catch (QueryException e) {
throw new GemfireQueryException("Cannot create query ", ex); throw new GemfireQueryException("Cannot create query ", e);
}
catch (QueryException ex) {
throw new GemfireQueryException("Cannot create query ", ex);
} }
} }
@@ -553,5 +547,4 @@ public class ContinuousQueryListenerContainer implements BeanFactoryAware, BeanN
public void close() { public void close() {
} }
} }
} }

View File

@@ -50,21 +50,17 @@ import org.springframework.data.gemfire.TestUtils;
import org.springframework.data.gemfire.config.xml.GemfireConstants; import org.springframework.data.gemfire.config.xml.GemfireConstants;
/** /**
* The ContinuousQueryListenerContainerTest class is a test suite of test cases testing the contract and functionality * Unit tests for {@link ContinuousQueryListenerContainer}.
* of the {@link ContinuousQueryListenerContainer} class.
* *
* @author John Blum * @author John Blum
* @see org.junit.Rule * @see org.junit.Rule
* @see org.junit.Test * @see org.junit.Test
* @see org.junit.rules.ExpectedException * @see org.junit.rules.ExpectedException
* @see org.junit.runner.RunWith
* @see org.mockito.Mock
* @see org.mockito.Mockito * @see org.mockito.Mockito
* @see org.mockito.runners.MockitoJUnitRunner
* @see org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer * @see org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer
* @since 1.8.0 * @since 1.8.0
*/ */
public class ContinuousQueryListenerContainerTest { public class ContinuousQueryListenerContainerTests {
@Rule @Rule
public ExpectedException exception = ExpectedException.none(); public ExpectedException exception = ExpectedException.none();
@@ -77,7 +73,7 @@ public class ContinuousQueryListenerContainerTest {
} }
@Test @Test
public void afterPropertiesSetAutoStarts() throws Exception { public void afterPropertiesSetIsAutoStart() throws Exception {
BeanFactory mockBeanFactory = mock(BeanFactory.class); BeanFactory mockBeanFactory = mock(BeanFactory.class);
Pool mockPool = mock(Pool.class); Pool mockPool = mock(Pool.class);
QueryService mockQueryService = mock(QueryService.class); QueryService mockQueryService = mock(QueryService.class);
@@ -96,11 +92,11 @@ public class ContinuousQueryListenerContainerTest {
} }
finally { finally {
assertThat(PoolManagerImpl.getPMI().unregister(mockPool), is(true)); assertThat(PoolManagerImpl.getPMI().unregister(mockPool), is(true));
assertThat(listenerContainer.isAutoStartup(), is(true));
assertThat((QueryService) TestUtils.readField("queryService", listenerContainer), is(equalTo(mockQueryService)));
assertThat(TestUtils.readField("taskExecutor", listenerContainer), is(instanceOf(Executor.class)));
assertThat(listenerContainer.isActive(), is(true)); assertThat(listenerContainer.isActive(), is(true));
assertThat(listenerContainer.isRunning(), is(true)); assertThat(listenerContainer.isAutoStartup(), is(true));
assertThat(listenerContainer.isRunning(), is(false));
assertThat(TestUtils.readField("queryService", listenerContainer), is(equalTo(mockQueryService)));
assertThat(TestUtils.readField("taskExecutor", listenerContainer), is(instanceOf(Executor.class)));
verify(mockBeanFactory, times(1)).containsBean(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME)); verify(mockBeanFactory, times(1)).containsBean(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME));
verify(mockBeanFactory, times(1)).isTypeMatch(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME), eq(Pool.class)); verify(mockBeanFactory, times(1)).isTypeMatch(eq(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME), eq(Pool.class));
@@ -112,7 +108,7 @@ public class ContinuousQueryListenerContainerTest {
} }
@Test @Test
public void afterPropertiesSetStartsManually() { public void afterPropertiesSetIsManualStart() {
BeanFactory mockBeanFactory = mock(BeanFactory.class); BeanFactory mockBeanFactory = mock(BeanFactory.class);
QueryService mockQueryService = mock(QueryService.class); QueryService mockQueryService = mock(QueryService.class);
@@ -130,6 +126,7 @@ public class ContinuousQueryListenerContainerTest {
assertThat(listenerContainer.isAutoStartup(), is(false)); assertThat(listenerContainer.isAutoStartup(), is(false));
assertThat(listenerContainer.isRunning(), is(false)); assertThat(listenerContainer.isRunning(), is(false));
verify(mockBeanFactory, never()).containsBean(eq("TestPool"));
verify(mockBeanFactory, times(1)).isTypeMatch(eq("TestPool"), eq(Pool.class)); verify(mockBeanFactory, times(1)).isTypeMatch(eq("TestPool"), eq(Pool.class));
verify(mockBeanFactory, times(1)).getBean(eq("TestPool"), eq(Pool.class)); verify(mockBeanFactory, times(1)).getBean(eq("TestPool"), eq(Pool.class));
verify(poolManagerSpy, never()).find(anyString()); verify(poolManagerSpy, never()).find(anyString());
@@ -361,5 +358,4 @@ public class ContinuousQueryListenerContainerTest {
assertThat(listenerContainer.isAutoStartup(), is(true)); assertThat(listenerContainer.isAutoStartup(), is(true));
} }
} }