Fix Auto-Startup for @JmsListener
Ignore container's auto-startup once the context is refreshed. Issue: SPR-14015
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -26,10 +26,15 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.jms.listener.MessageListenerContainer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -53,7 +58,8 @@ import org.springframework.util.Assert;
|
||||
* @see MessageListenerContainer
|
||||
* @see JmsListenerContainerFactory
|
||||
*/
|
||||
public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecycle {
|
||||
public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecycle,
|
||||
ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -62,6 +68,15 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
|
||||
|
||||
private int phase = Integer.MAX_VALUE;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private boolean contextRefreshed;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the {@link MessageListenerContainer} with the specified id or
|
||||
@@ -181,6 +196,13 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
if (event.getApplicationContext().equals(this.applicationContext)) {
|
||||
this.contextRefreshed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Delegating implementation of SmartLifecycle
|
||||
|
||||
@Override
|
||||
@@ -228,11 +250,11 @@ public class JmsListenerEndpointRegistry implements DisposableBean, SmartLifecyc
|
||||
|
||||
/**
|
||||
* Start the specified {@link MessageListenerContainer} if it should be started
|
||||
* on startup.
|
||||
* on startup or when start is called explicitly after startup.
|
||||
* @see MessageListenerContainer#isAutoStartup()
|
||||
*/
|
||||
private static void startIfNecessary(MessageListenerContainer listenerContainer) {
|
||||
if (listenerContainer.isAutoStartup()) {
|
||||
private void startIfNecessary(MessageListenerContainer listenerContainer) {
|
||||
if (this.contextRefreshed || listenerContainer.isAutoStartup()) {
|
||||
listenerContainer.start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user