Skip to content

Commit 40720ab

Browse files
committed
SPR-4716 AbstractJmsListeningContainer now "auto-starts" upon receiving a ContextRefreshedEvent rather than within afterPropertiesSet().
1 parent 389ad03 commit 40720ab

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

org.springframework.jms/src/main/java/org/springframework/jms/listener/AbstractJmsListeningContainer.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,10 @@
2525

2626
import org.springframework.beans.factory.BeanNameAware;
2727
import org.springframework.beans.factory.DisposableBean;
28+
import org.springframework.context.ApplicationEvent;
29+
import org.springframework.context.ApplicationListener;
2830
import org.springframework.context.Lifecycle;
31+
import org.springframework.context.event.ContextRefreshedEvent;
2932
import org.springframework.jms.JmsException;
3033
import org.springframework.jms.connection.ConnectionFactoryUtils;
3134
import org.springframework.jms.support.JmsUtils;
@@ -59,7 +62,7 @@
5962
* @see #doShutdown()
6063
*/
6164
public abstract class AbstractJmsListeningContainer extends JmsDestinationAccessor
62-
implements Lifecycle, BeanNameAware, DisposableBean {
65+
implements Lifecycle, ApplicationListener<ApplicationEvent>, BeanNameAware, DisposableBean {
6366

6467
private String clientId;
6568

@@ -134,6 +137,12 @@ public void afterPropertiesSet() {
134137
initialize();
135138
}
136139

140+
public void onApplicationEvent(ApplicationEvent event) {
141+
if (event instanceof ContextRefreshedEvent && this.autoStartup) {
142+
this.start();
143+
}
144+
}
145+
137146
/**
138147
* Validate the configuration of this container.
139148
* <p>The default implementation is empty. To be overridden in subclasses.
@@ -167,9 +176,6 @@ public void initialize() throws JmsException {
167176
this.active = true;
168177
this.lifecycleMonitor.notifyAll();
169178
}
170-
if (this.autoStartup) {
171-
doStart();
172-
}
173179
doInitialize();
174180
}
175181
catch (JMSException ex) {

org.springframework.jms/src/test/java/org/springframework/jms/listener/SimpleMessageListenerContainerTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import org.easymock.internal.AlwaysMatcher;
3535
import org.junit.Before;
3636
import org.junit.Test;
37+
38+
import org.springframework.context.event.ContextRefreshedEvent;
39+
import org.springframework.context.support.StaticApplicationContext;
3740
import org.springframework.core.task.TaskExecutor;
3841
import org.springframework.jms.StubQueue;
3942
import org.springframework.util.ErrorHandler;
@@ -87,7 +90,7 @@ public void testSettingConcurrentConsumersToANegativeValueIsNotAllowed() throws
8790
}
8891

8992
@Test
90-
public void testInitDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception {
93+
public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception {
9194
MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
9295
MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
9396
messageConsumer.setMessageListener(null);
@@ -127,6 +130,7 @@ public void testInitDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Ex
127130
this.container.setMessageListener(new TestMessageListener());
128131
this.container.setAutoStartup(false);
129132
this.container.afterPropertiesSet();
133+
this.container.onApplicationEvent(new ContextRefreshedEvent(new StaticApplicationContext()));
130134

131135
mockMessageConsumer.verify();
132136
mockSession.verify();
@@ -135,7 +139,7 @@ public void testInitDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Ex
135139
}
136140

137141
@Test
138-
public void testInitStartsTheConnectionByDefault() throws Exception {
142+
public void testContextRefreshedEventStartsTheConnectionByDefault() throws Exception {
139143
MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
140144
MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
141145
messageConsumer.setMessageListener(null);
@@ -177,6 +181,7 @@ public void testInitStartsTheConnectionByDefault() throws Exception {
177181

178182
this.container.setMessageListener(new TestMessageListener());
179183
this.container.afterPropertiesSet();
184+
this.container.onApplicationEvent(new ContextRefreshedEvent(new StaticApplicationContext()));
180185

181186
mockMessageConsumer.verify();
182187
mockSession.verify();
@@ -238,6 +243,7 @@ public void onMessage(Message message, Session sess) {
238243
});
239244

240245
this.container.afterPropertiesSet();
246+
this.container.start();
241247

242248
MockControl mockMessage = MockControl.createControl(Message.class);
243249
final Message message = (Message) mockMessage.getMock();
@@ -300,6 +306,7 @@ public void execute(Runnable task) {
300306
}
301307
});
302308
this.container.afterPropertiesSet();
309+
this.container.start();
303310

304311
MockControl mockMessage = MockControl.createControl(Message.class);
305312
final Message message = (Message) mockMessage.getMock();
@@ -367,6 +374,7 @@ public void onMessage(Message message, Session session) throws JMSException {
367374

368375
this.container.setExceptionListener(exceptionListener);
369376
this.container.afterPropertiesSet();
377+
this.container.start();
370378

371379
// manually trigger an Exception with the above bad MessageListener...
372380
MockControl mockMessage = MockControl.createControl(Message.class);
@@ -430,6 +438,7 @@ public void onMessage(Message message, Session session) throws JMSException {
430438
EasyMock.replay(errorHandler);
431439
this.container.setErrorHandler(errorHandler);
432440
this.container.afterPropertiesSet();
441+
this.container.start();
433442

434443
// manually trigger an Exception with the above bad MessageListener...
435444
Message message = EasyMock.createMock(Message.class);
@@ -484,6 +493,7 @@ public void onMessage(Message message) {
484493
}
485494
});
486495
this.container.afterPropertiesSet();
496+
this.container.start();
487497

488498
// manually trigger an Exception with the above bad MessageListener...
489499
MockControl mockMessage = MockControl.createControl(Message.class);
@@ -547,6 +557,7 @@ public void onMessage(Message message) {
547557
}
548558
});
549559
this.container.afterPropertiesSet();
560+
this.container.start();
550561

551562
// manually trigger an Exception with the above bad MessageListener...
552563
MockControl mockMessage = MockControl.createControl(Message.class);
@@ -614,6 +625,7 @@ public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() throws
614625

615626
this.container.setMessageListener(new TestMessageListener());
616627
this.container.afterPropertiesSet();
628+
this.container.start();
617629
this.container.destroy();
618630

619631
mockMessageConsumer.verify();

0 commit comments

Comments
 (0)