Java Code Examples for org.springframework.context.Lifecycle#start()

The following examples show how to use org.springframework.context.Lifecycle#start() . 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 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 3
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 4
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 5
Source File: DefaultLifecycleProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void singleLifecycleShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.refresh();
	assertFalse(bean.isRunning());
	bean.start();
	assertTrue(bean.isRunning());
	context.stop();
	assertEquals(1, stoppedBeans.size());
	assertFalse(bean.isRunning());
	assertEquals(bean, stoppedBeans.get(0));
}
 
Example 6
Source File: SockJsClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void start() {
	if (!isRunning()) {
		this.running = true;
		for (Transport transport : this.transports) {
			if (transport instanceof Lifecycle) {
				Lifecycle lifecycle = (Lifecycle) transport;
				if (!lifecycle.isRunning()) {
					lifecycle.start();
				}
			}
		}
	}
}
 
Example 7
Source File: DefaultLifecycleProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void singleLifecycleShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.refresh();
	assertFalse(bean.isRunning());
	bean.start();
	assertTrue(bean.isRunning());
	context.stop();
	assertEquals(1, stoppedBeans.size());
	assertFalse(bean.isRunning());
	assertEquals(bean, stoppedBeans.get(0));
}
 
Example 8
Source File: SockJsClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void start() {
	if (!isRunning()) {
		this.running = true;
		for (Transport transport : this.transports) {
			if (transport instanceof Lifecycle) {
				Lifecycle lifecycle = (Lifecycle) transport;
				if (!lifecycle.isRunning()) {
					lifecycle.start();
				}
			}
		}
	}
}
 
Example 9
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void singleLifecycleShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<Lifecycle>();
	Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.refresh();
	assertFalse(bean.isRunning());
	bean.start();
	assertTrue(bean.isRunning());
	context.stop();
	assertEquals(1, stoppedBeans.size());
	assertFalse(bean.isRunning());
	assertEquals(bean, stoppedBeans.get(0));
}
 
Example 10
Source File: DefaultLifecycleProcessorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void mixedShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
	Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
	Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
	Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean3", bean3);
	context.getBeanFactory().registerSingleton("bean4", bean4);
	context.getBeanFactory().registerSingleton("bean5", bean5);
	context.getBeanFactory().registerSingleton("bean6", bean6);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.refresh();
	assertTrue(bean2.isRunning());
	assertTrue(bean3.isRunning());
	assertTrue(bean5.isRunning());
	assertTrue(bean6.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean4.isRunning());
	bean1.start();
	bean4.start();
	assertTrue(bean1.isRunning());
	assertTrue(bean4.isRunning());
	context.stop();
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean3.isRunning());
	assertFalse(bean4.isRunning());
	assertFalse(bean5.isRunning());
	assertFalse(bean6.isRunning());
	assertFalse(bean7.isRunning());
	assertEquals(7, stoppedBeans.size());
	assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
	assertEquals(500, getPhase(stoppedBeans.get(1)));
	assertEquals(1, getPhase(stoppedBeans.get(2)));
	assertEquals(0, getPhase(stoppedBeans.get(3)));
	assertEquals(0, getPhase(stoppedBeans.get(4)));
	assertEquals(-1, getPhase(stoppedBeans.get(5)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}
 
Example 11
Source File: DefaultLifecycleProcessorTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void mixedShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
	Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
	Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
	Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean3", bean3);
	context.getBeanFactory().registerSingleton("bean4", bean4);
	context.getBeanFactory().registerSingleton("bean5", bean5);
	context.getBeanFactory().registerSingleton("bean6", bean6);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.refresh();
	assertTrue(bean2.isRunning());
	assertTrue(bean3.isRunning());
	assertTrue(bean5.isRunning());
	assertTrue(bean6.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean4.isRunning());
	bean1.start();
	bean4.start();
	assertTrue(bean1.isRunning());
	assertTrue(bean4.isRunning());
	context.stop();
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean3.isRunning());
	assertFalse(bean4.isRunning());
	assertFalse(bean5.isRunning());
	assertFalse(bean6.isRunning());
	assertFalse(bean7.isRunning());
	assertEquals(7, stoppedBeans.size());
	assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
	assertEquals(500, getPhase(stoppedBeans.get(1)));
	assertEquals(1, getPhase(stoppedBeans.get(2)));
	assertEquals(0, getPhase(stoppedBeans.get(3)));
	assertEquals(0, getPhase(stoppedBeans.get(4)));
	assertEquals(-1, getPhase(stoppedBeans.get(5)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}
 
Example 12
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void mixedShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<Lifecycle>();
	Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
	Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
	Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
	Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean3", bean3);
	context.getBeanFactory().registerSingleton("bean4", bean4);
	context.getBeanFactory().registerSingleton("bean5", bean5);
	context.getBeanFactory().registerSingleton("bean6", bean6);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.refresh();
	assertTrue(bean2.isRunning());
	assertTrue(bean3.isRunning());
	assertTrue(bean5.isRunning());
	assertTrue(bean6.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean4.isRunning());
	bean1.start();
	bean4.start();
	assertTrue(bean1.isRunning());
	assertTrue(bean4.isRunning());
	context.stop();
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean3.isRunning());
	assertFalse(bean4.isRunning());
	assertFalse(bean5.isRunning());
	assertFalse(bean6.isRunning());
	assertFalse(bean7.isRunning());
	assertEquals(7, stoppedBeans.size());
	assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
	assertEquals(500, getPhase(stoppedBeans.get(1)));
	assertEquals(1, getPhase(stoppedBeans.get(2)));
	assertEquals(0, getPhase(stoppedBeans.get(3)));
	assertEquals(0, getPhase(stoppedBeans.get(4)));
	assertEquals(-1, getPhase(stoppedBeans.get(5)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}