SPR-4716 AbstractJmsListeningContainer now "auto-starts" upon receiving a ContextRefreshedEvent rather than within afterPropertiesSet().
This commit is contained in:
parent
389ad03e84
commit
40720ab0f1
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2008 the original author or authors.
|
* Copyright 2002-2009 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -25,7 +25,10 @@ import javax.jms.JMSException;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.Lifecycle;
|
import org.springframework.context.Lifecycle;
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
import org.springframework.jms.JmsException;
|
import org.springframework.jms.JmsException;
|
||||||
import org.springframework.jms.connection.ConnectionFactoryUtils;
|
import org.springframework.jms.connection.ConnectionFactoryUtils;
|
||||||
import org.springframework.jms.support.JmsUtils;
|
import org.springframework.jms.support.JmsUtils;
|
||||||
|
@ -59,7 +62,7 @@ import org.springframework.util.ClassUtils;
|
||||||
* @see #doShutdown()
|
* @see #doShutdown()
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractJmsListeningContainer extends JmsDestinationAccessor
|
public abstract class AbstractJmsListeningContainer extends JmsDestinationAccessor
|
||||||
implements Lifecycle, BeanNameAware, DisposableBean {
|
implements Lifecycle, ApplicationListener<ApplicationEvent>, BeanNameAware, DisposableBean {
|
||||||
|
|
||||||
private String clientId;
|
private String clientId;
|
||||||
|
|
||||||
|
@ -134,6 +137,12 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
|
||||||
initialize();
|
initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onApplicationEvent(ApplicationEvent event) {
|
||||||
|
if (event instanceof ContextRefreshedEvent && this.autoStartup) {
|
||||||
|
this.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate the configuration of this container.
|
* Validate the configuration of this container.
|
||||||
* <p>The default implementation is empty. To be overridden in subclasses.
|
* <p>The default implementation is empty. To be overridden in subclasses.
|
||||||
|
@ -167,9 +176,6 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
|
||||||
this.active = true;
|
this.active = true;
|
||||||
this.lifecycleMonitor.notifyAll();
|
this.lifecycleMonitor.notifyAll();
|
||||||
}
|
}
|
||||||
if (this.autoStartup) {
|
|
||||||
doStart();
|
|
||||||
}
|
|
||||||
doInitialize();
|
doInitialize();
|
||||||
}
|
}
|
||||||
catch (JMSException ex) {
|
catch (JMSException ex) {
|
||||||
|
|
|
@ -34,6 +34,9 @@ import org.easymock.MockControl;
|
||||||
import org.easymock.internal.AlwaysMatcher;
|
import org.easymock.internal.AlwaysMatcher;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
|
import org.springframework.context.support.StaticApplicationContext;
|
||||||
import org.springframework.core.task.TaskExecutor;
|
import org.springframework.core.task.TaskExecutor;
|
||||||
import org.springframework.jms.StubQueue;
|
import org.springframework.jms.StubQueue;
|
||||||
import org.springframework.util.ErrorHandler;
|
import org.springframework.util.ErrorHandler;
|
||||||
|
@ -87,7 +90,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInitDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception {
|
public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception {
|
||||||
MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
|
MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
|
||||||
MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
|
MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
|
||||||
messageConsumer.setMessageListener(null);
|
messageConsumer.setMessageListener(null);
|
||||||
|
@ -127,6 +130,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
this.container.setMessageListener(new TestMessageListener());
|
this.container.setMessageListener(new TestMessageListener());
|
||||||
this.container.setAutoStartup(false);
|
this.container.setAutoStartup(false);
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.onApplicationEvent(new ContextRefreshedEvent(new StaticApplicationContext()));
|
||||||
|
|
||||||
mockMessageConsumer.verify();
|
mockMessageConsumer.verify();
|
||||||
mockSession.verify();
|
mockSession.verify();
|
||||||
|
@ -135,7 +139,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInitStartsTheConnectionByDefault() throws Exception {
|
public void testContextRefreshedEventStartsTheConnectionByDefault() throws Exception {
|
||||||
MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
|
MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
|
||||||
MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
|
MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
|
||||||
messageConsumer.setMessageListener(null);
|
messageConsumer.setMessageListener(null);
|
||||||
|
@ -177,6 +181,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
|
|
||||||
this.container.setMessageListener(new TestMessageListener());
|
this.container.setMessageListener(new TestMessageListener());
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.onApplicationEvent(new ContextRefreshedEvent(new StaticApplicationContext()));
|
||||||
|
|
||||||
mockMessageConsumer.verify();
|
mockMessageConsumer.verify();
|
||||||
mockSession.verify();
|
mockSession.verify();
|
||||||
|
@ -238,6 +243,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
});
|
});
|
||||||
|
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
|
|
||||||
MockControl mockMessage = MockControl.createControl(Message.class);
|
MockControl mockMessage = MockControl.createControl(Message.class);
|
||||||
final Message message = (Message) mockMessage.getMock();
|
final Message message = (Message) mockMessage.getMock();
|
||||||
|
@ -300,6 +306,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
|
|
||||||
MockControl mockMessage = MockControl.createControl(Message.class);
|
MockControl mockMessage = MockControl.createControl(Message.class);
|
||||||
final Message message = (Message) mockMessage.getMock();
|
final Message message = (Message) mockMessage.getMock();
|
||||||
|
@ -367,6 +374,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
|
|
||||||
this.container.setExceptionListener(exceptionListener);
|
this.container.setExceptionListener(exceptionListener);
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
|
|
||||||
// manually trigger an Exception with the above bad MessageListener...
|
// manually trigger an Exception with the above bad MessageListener...
|
||||||
MockControl mockMessage = MockControl.createControl(Message.class);
|
MockControl mockMessage = MockControl.createControl(Message.class);
|
||||||
|
@ -430,6 +438,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
EasyMock.replay(errorHandler);
|
EasyMock.replay(errorHandler);
|
||||||
this.container.setErrorHandler(errorHandler);
|
this.container.setErrorHandler(errorHandler);
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
|
|
||||||
// manually trigger an Exception with the above bad MessageListener...
|
// manually trigger an Exception with the above bad MessageListener...
|
||||||
Message message = EasyMock.createMock(Message.class);
|
Message message = EasyMock.createMock(Message.class);
|
||||||
|
@ -484,6 +493,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
|
|
||||||
// manually trigger an Exception with the above bad MessageListener...
|
// manually trigger an Exception with the above bad MessageListener...
|
||||||
MockControl mockMessage = MockControl.createControl(Message.class);
|
MockControl mockMessage = MockControl.createControl(Message.class);
|
||||||
|
@ -547,6 +557,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
|
|
||||||
// manually trigger an Exception with the above bad MessageListener...
|
// manually trigger an Exception with the above bad MessageListener...
|
||||||
MockControl mockMessage = MockControl.createControl(Message.class);
|
MockControl mockMessage = MockControl.createControl(Message.class);
|
||||||
|
@ -614,6 +625,7 @@ public class SimpleMessageListenerContainerTests extends AbstractMessageListener
|
||||||
|
|
||||||
this.container.setMessageListener(new TestMessageListener());
|
this.container.setMessageListener(new TestMessageListener());
|
||||||
this.container.afterPropertiesSet();
|
this.container.afterPropertiesSet();
|
||||||
|
this.container.start();
|
||||||
this.container.destroy();
|
this.container.destroy();
|
||||||
|
|
||||||
mockMessageConsumer.verify();
|
mockMessageConsumer.verify();
|
||||||
|
|
Loading…
Reference in New Issue