org.springframework.context.SmartLifecycle Java Examples

The following examples show how to use org.springframework.context.SmartLifecycle. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && bean != this) {
		String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isTraceEnabled()) {
				logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
Example #2
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	Map<String, Lifecycle> beans = new LinkedHashMap<String, Lifecycle>();
	String[] beanNames = this.beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = this.beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((this.beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || Lifecycle.class.isAssignableFrom(this.beanFactory.getType(beanNameToCheck)))) ||
				SmartLifecycle.class.isAssignableFrom(this.beanFactory.getType(beanNameToCheck))) {
			Lifecycle bean = this.beanFactory.getBean(beanNameToCheck, Lifecycle.class);
			if (bean != this) {
				beans.put(beanNameToRegister, bean);
			}
		}
	}
	return beans;
}
 
Example #3
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && !this.equals(bean)) {
		String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isDebugEnabled()) {
				logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
Example #4
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void startBeans(boolean autoStartupOnly) {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();
	for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) {
		Lifecycle bean = entry.getValue();
		if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
			int phase = getPhase(bean);
			LifecycleGroup group = phases.get(phase);
			if (group == null) {
				group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
				phases.put(phase, group);
			}
			group.add(entry.getKey(), bean);
		}
	}
	if (phases.size() > 0) {
		List<Integer> keys = new ArrayList<Integer>(phases.keySet());
		Collections.sort(keys);
		for (Integer key : keys) {
			phases.get(key).start();
		}
	}
}
 
Example #5
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	Map<String, Lifecycle> beans = new LinkedHashMap<String, Lifecycle>();
	String[] beanNames = this.beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = this.beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((this.beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || Lifecycle.class.isAssignableFrom(this.beanFactory.getType(beanNameToCheck)))) ||
				SmartLifecycle.class.isAssignableFrom(this.beanFactory.getType(beanNameToCheck))) {
			Lifecycle bean = this.beanFactory.getBean(beanNameToCheck, Lifecycle.class);
			if (bean != this) {
				beans.put(beanNameToRegister, bean);
			}
		}
	}
	return beans;
}
 
Example #6
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && !this.equals(bean)) {
		String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isDebugEnabled()) {
				logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
Example #7
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void startBeans(boolean autoStartupOnly) {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();
	for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) {
		Lifecycle bean = entry.getValue();
		if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
			int phase = getPhase(bean);
			LifecycleGroup group = phases.get(phase);
			if (group == null) {
				group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
				phases.put(phase, group);
			}
			group.add(entry.getKey(), bean);
		}
	}
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<Integer>(phases.keySet());
		Collections.sort(keys);
		for (Integer key : keys) {
			phases.get(key).start();
		}
	}
}
 
Example #8
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	Map<String, Lifecycle> beans = new LinkedHashMap<>();
	String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
				matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
			Object bean = beanFactory.getBean(beanNameToCheck);
			if (bean != this && bean instanceof Lifecycle) {
				beans.put(beanNameToRegister, (Lifecycle) bean);
			}
		}
	}
	return beans;
}
 
Example #9
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void startBeans(boolean autoStartupOnly) {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<>();
	lifecycleBeans.forEach((beanName, bean) -> {
		if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
			int phase = getPhase(bean);
			LifecycleGroup group = phases.get(phase);
			if (group == null) {
				group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
				phases.put(phase, group);
			}
			group.add(beanName, bean);
		}
	});
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<>(phases.keySet());
		Collections.sort(keys);
		for (Integer key : keys) {
			phases.get(key).start();
		}
	}
}
 
Example #10
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void startBeans(boolean autoStartupOnly) {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<>();
	lifecycleBeans.forEach((beanName, bean) -> {
		if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
			int phase = getPhase(bean);
			LifecycleGroup group = phases.get(phase);
			if (group == null) {
				group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
				phases.put(phase, group);
			}
			group.add(beanName, bean);
		}
	});
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<>(phases.keySet());
		Collections.sort(keys);
		for (Integer key : keys) {
			phases.get(key).start();
		}
	}
}
 
Example #11
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && bean != this) {
		String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isTraceEnabled()) {
				logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
Example #12
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	Map<String, Lifecycle> beans = new LinkedHashMap<>();
	String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
				matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
			Object bean = beanFactory.getBean(beanNameToCheck);
			if (bean != this && bean instanceof Lifecycle) {
				beans.put(beanNameToRegister, (Lifecycle) bean);
			}
		}
	}
	return beans;
}
 
Example #13
Source File: JmsContainerManagerTest.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * For tests that assume the container manager is started.
 */
private void startContainerManager() {
  //init with no processes in cache; use mock cache to start correctly
  EasyMock.expect(mockProcessCache.getKeys()).andReturn(Collections.EMPTY_LIST);
  EasyMock.replay(mockProcessCache);
  ((JmsContainerManagerImpl) jmsContainerManager).init();
  ((SmartLifecycle) jmsContainerManager).start();
  EasyMock.reset(mockProcessCache);
}
 
Example #14
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
public void stop() {
	if (this.members.isEmpty()) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Stopping beans in phase " + this.phase);
	}
	this.members.sort(Collections.reverseOrder());
	CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
	Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());
	Set<String> lifecycleBeanNames = new HashSet<>(this.lifecycleBeans.keySet());
	for (LifecycleGroupMember member : this.members) {
		if (lifecycleBeanNames.contains(member.name)) {
			doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
		}
		else if (member.bean instanceof SmartLifecycle) {
			// Already removed: must have been a dependent bean from another phase
			latch.countDown();
		}
	}
	try {
		latch.await(this.timeout, TimeUnit.MILLISECONDS);
		if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isInfoEnabled()) {
			logger.info("Failed to shut down " + countDownBeanNames.size() + " bean" +
					(countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
					this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames);
		}
	}
	catch (InterruptedException ex) {
		Thread.currentThread().interrupt();
	}
}
 
Example #15
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void stop() {
	if (this.members.isEmpty()) {
		return;
	}
	if (logger.isInfoEnabled()) {
		logger.info("Stopping beans in phase " + this.phase);
	}
	Collections.sort(this.members, Collections.reverseOrder());
	CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
	Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<String>());
	for (LifecycleGroupMember member : this.members) {
		if (this.lifecycleBeans.containsKey(member.name)) {
			doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
		}
		else if (member.bean instanceof SmartLifecycle) {
			// already removed, must have been a dependent
			latch.countDown();
		}
	}
	try {
		latch.await(this.timeout, TimeUnit.MILLISECONDS);
		if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isWarnEnabled()) {
			logger.warn("Failed to shut down " + countDownBeanNames.size() + " bean" +
					(countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
					this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames);
		}
	}
	catch (InterruptedException ex) {
		Thread.currentThread().interrupt();
	}
}
 
Example #16
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public void stop() {
	if (this.members.isEmpty()) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Stopping beans in phase " + this.phase);
	}
	this.members.sort(Collections.reverseOrder());
	CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
	Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());
	Set<String> lifecycleBeanNames = new HashSet<>(this.lifecycleBeans.keySet());
	for (LifecycleGroupMember member : this.members) {
		if (lifecycleBeanNames.contains(member.name)) {
			doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
		}
		else if (member.bean instanceof SmartLifecycle) {
			// Already removed: must have been a dependent bean from another phase
			latch.countDown();
		}
	}
	try {
		latch.await(this.timeout, TimeUnit.MILLISECONDS);
		if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isInfoEnabled()) {
			logger.info("Failed to shut down " + countDownBeanNames.size() + " bean" +
					(countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
					this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames);
		}
	}
	catch (InterruptedException ex) {
		Thread.currentThread().interrupt();
	}
}
 
Example #17
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void stop() {
	if (this.members.isEmpty()) {
		return;
	}
	if (logger.isInfoEnabled()) {
		logger.info("Stopping beans in phase " + this.phase);
	}
	Collections.sort(this.members, Collections.reverseOrder());
	CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
	Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<String>());
	for (LifecycleGroupMember member : this.members) {
		if (this.lifecycleBeans.containsKey(member.name)) {
			doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
		}
		else if (member.bean instanceof SmartLifecycle) {
			// already removed, must have been a dependent
			latch.countDown();
		}
	}
	try {
		latch.await(this.timeout, TimeUnit.MILLISECONDS);
		if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isWarnEnabled()) {
			logger.warn("Failed to shut down " + countDownBeanNames.size() + " bean" +
					(countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
					this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames);
		}
	}
	catch (InterruptedException ex) {
		Thread.currentThread().interrupt();
	}
}
 
Example #18
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void add(String name, Lifecycle bean) {
	if (bean instanceof SmartLifecycle) {
		this.smartMemberCount++;
	}
	this.members.add(new LifecycleGroupMember(name, bean));
}
 
Example #19
Source File: JmsContainerManagerTest.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tests that all containers are subscribed at start-up to the
 * correct JMS queues (by sending message to expected subscribed).
 * @throws InterruptedException
 * @throws JMSException
 */
@Test
@Ignore("This test is broken")
public void testInitAtStartUp() throws InterruptedException, JMSException {
  ArrayList<Long> keys = new ArrayList<Long>();
  keys.add(0, 1L);
  keys.add(1, 2L);
  Process mockProcess1 = EasyMock.createMock(Process.class);
  Process mockProcess2 = EasyMock.createMock(Process.class);
  EasyMock.expect(mockProcessCache.getKeys()).andReturn(keys);
  EasyMock.expect(mockProcessCache.get(1L)).andReturn(mockProcess1);
  EasyMock.expect(mockProcessCache.get(2L)).andReturn(mockProcess2);
  long millis = System.currentTimeMillis();
  EasyMock.expect(mockProcess1.getName()).andReturn("Process-1-" + millis).times(2);
  EasyMock.expect(mockProcess2.getName()).andReturn("Process-2-" + millis).times(2);
  EasyMock.expect(mockProcess1.getId()).andReturn(1L);
  EasyMock.expect(mockProcess2.getId()).andReturn(2L);

  final CountDownLatch latch = new CountDownLatch(2);

  //expect one message from each
  mockListener.onMessage(EasyMock.isA(TextMessage.class), EasyMock.isA(Session.class));
  EasyMock.expectLastCall().andAnswer(() -> {
    latch.countDown();
    return null;
  });

  mockListener.onMessage(EasyMock.isA(TextMessage.class), EasyMock.isA(Session.class));
  EasyMock.expectLastCall().andAnswer(() -> {
    latch.countDown();
    return null;
  });

  //run test
  EasyMock.replay(mockProcessCache);
  EasyMock.replay(mockProcess1);
  EasyMock.replay(mockProcess2);
  EasyMock.replay(mockListener);

  //init subscriptions
  ((JmsContainerManagerImpl) jmsContainerManager).init();
  ((SmartLifecycle) jmsContainerManager).start();
  //check messages are picked up
  jmsSender.sendToQueue("test message from process 1", testTrunkName + ".Process-1-" + millis);
  jmsSender.sendToQueue("test message from process 2", testTrunkName + ".Process-2-" + millis);

  // wait for the listeners to fire
  latch.await();

  EasyMock.verify(mockProcessCache);
  EasyMock.verify(mockProcess1);
  EasyMock.verify(mockProcess2);
  EasyMock.verify(mockListener);
}
 
Example #20
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private static int getPhase(Lifecycle lifecycle) {
	return (lifecycle instanceof SmartLifecycle) ?
			((SmartLifecycle) lifecycle).getPhase() : 0;
}
 
Example #21
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void add(String name, Lifecycle bean) {
	if (bean instanceof SmartLifecycle) {
		this.smartMemberCount++;
	}
	this.members.add(new LifecycleGroupMember(name, bean));
}
 
Example #22
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Stop the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that depends on it are stopped first.
 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to stop
 */
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
		final CountDownLatch latch, final Set<String> countDownBeanNames) {

	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null) {
		String[] dependentBeans = this.beanFactory.getDependentBeans(beanName);
		for (String dependentBean : dependentBeans) {
			doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
		}
		try {
			if (bean.isRunning()) {
				if (bean instanceof SmartLifecycle) {
					if (logger.isDebugEnabled()) {
						logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop");
					}
					countDownBeanNames.add(beanName);
					((SmartLifecycle) bean).stop(new Runnable() {
						@Override
						public void run() {
							latch.countDown();
							countDownBeanNames.remove(beanName);
							if (logger.isDebugEnabled()) {
								logger.debug("Bean '" + beanName + "' completed its stop procedure");
							}
						}
					});
				}
				else {
					if (logger.isDebugEnabled()) {
						logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]");
					}
					bean.stop();
					if (logger.isDebugEnabled()) {
						logger.debug("Successfully stopped bean '" + beanName + "'");
					}
				}
			}
			else if (bean instanceof SmartLifecycle) {
				// don't wait for beans that aren't running
				latch.countDown();
			}
		}
		catch (Throwable ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Failed to stop bean '" + beanName + "'", ex);
			}
		}
	}
}
 
Example #23
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Stop the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that depends on it are stopped first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to stop
 */
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
		final CountDownLatch latch, final Set<String> countDownBeanNames) {

	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null) {
		String[] dependentBeans = getBeanFactory().getDependentBeans(beanName);
		for (String dependentBean : dependentBeans) {
			doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
		}
		try {
			if (bean.isRunning()) {
				if (bean instanceof SmartLifecycle) {
					if (logger.isTraceEnabled()) {
						logger.trace("Asking bean '" + beanName + "' of type [" +
								bean.getClass().getName() + "] to stop");
					}
					countDownBeanNames.add(beanName);
					((SmartLifecycle) bean).stop(() -> {
						latch.countDown();
						countDownBeanNames.remove(beanName);
						if (logger.isDebugEnabled()) {
							logger.debug("Bean '" + beanName + "' completed its stop procedure");
						}
					});
				}
				else {
					if (logger.isTraceEnabled()) {
						logger.trace("Stopping bean '" + beanName + "' of type [" +
								bean.getClass().getName() + "]");
					}
					bean.stop();
					if (logger.isDebugEnabled()) {
						logger.debug("Successfully stopped bean '" + beanName + "'");
					}
				}
			}
			else if (bean instanceof SmartLifecycle) {
				// Don't wait for beans that aren't running...
				latch.countDown();
			}
		}
		catch (Throwable ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Failed to stop bean '" + beanName + "'", ex);
			}
		}
	}
}
 
Example #24
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void add(String name, Lifecycle bean) {
	this.members.add(new LifecycleGroupMember(name, bean));
	if (bean instanceof SmartLifecycle) {
		this.smartMemberCount++;
	}
}
 
Example #25
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Stop the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that depends on it are stopped first.
 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to stop
 */
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
		final CountDownLatch latch, final Set<String> countDownBeanNames) {

	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null) {
		String[] dependentBeans = this.beanFactory.getDependentBeans(beanName);
		for (String dependentBean : dependentBeans) {
			doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
		}
		try {
			if (bean.isRunning()) {
				if (bean instanceof SmartLifecycle) {
					if (logger.isDebugEnabled()) {
						logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop");
					}
					countDownBeanNames.add(beanName);
					((SmartLifecycle) bean).stop(new Runnable() {
						@Override
						public void run() {
							latch.countDown();
							countDownBeanNames.remove(beanName);
							if (logger.isDebugEnabled()) {
								logger.debug("Bean '" + beanName + "' completed its stop procedure");
							}
						}
					});
				}
				else {
					if (logger.isDebugEnabled()) {
						logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]");
					}
					bean.stop();
					if (logger.isDebugEnabled()) {
						logger.debug("Successfully stopped bean '" + beanName + "'");
					}
				}
			}
			else if (bean instanceof SmartLifecycle) {
				// don't wait for beans that aren't running
				latch.countDown();
			}
		}
		catch (Throwable ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Failed to stop bean '" + beanName + "'", ex);
			}
		}
	}
}
 
Example #26
Source File: DefaultLifecycleProcessorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static int getPhase(Lifecycle lifecycle) {
	return (lifecycle instanceof SmartLifecycle) ?
			((SmartLifecycle) lifecycle).getPhase() : 0;
}
 
Example #27
Source File: DefaultLifecycleProcessorTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static int getPhase(Lifecycle lifecycle) {
	return (lifecycle instanceof SmartLifecycle) ?
			((SmartLifecycle) lifecycle).getPhase() : 0;
}
 
Example #28
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void add(String name, Lifecycle bean) {
	this.members.add(new LifecycleGroupMember(name, bean));
	if (bean instanceof SmartLifecycle) {
		this.smartMemberCount++;
	}
}
 
Example #29
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Stop the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that depends on it are stopped first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to stop
 */
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
		final CountDownLatch latch, final Set<String> countDownBeanNames) {

	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null) {
		String[] dependentBeans = getBeanFactory().getDependentBeans(beanName);
		for (String dependentBean : dependentBeans) {
			doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
		}
		try {
			if (bean.isRunning()) {
				if (bean instanceof SmartLifecycle) {
					if (logger.isTraceEnabled()) {
						logger.trace("Asking bean '" + beanName + "' of type [" +
								bean.getClass().getName() + "] to stop");
					}
					countDownBeanNames.add(beanName);
					((SmartLifecycle) bean).stop(() -> {
						latch.countDown();
						countDownBeanNames.remove(beanName);
						if (logger.isDebugEnabled()) {
							logger.debug("Bean '" + beanName + "' completed its stop procedure");
						}
					});
				}
				else {
					if (logger.isTraceEnabled()) {
						logger.trace("Stopping bean '" + beanName + "' of type [" +
								bean.getClass().getName() + "]");
					}
					bean.stop();
					if (logger.isDebugEnabled()) {
						logger.debug("Successfully stopped bean '" + beanName + "'");
					}
				}
			}
			else if (bean instanceof SmartLifecycle) {
				// Don't wait for beans that aren't running...
				latch.countDown();
			}
		}
		catch (Throwable ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Failed to stop bean '" + beanName + "'", ex);
			}
		}
	}
}