org.springframework.context.Lifecycle Java Examples

The following examples show how to use org.springframework.context.Lifecycle. 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 java-technology-stack with MIT License 6 votes vote down vote up
private void stopBeans() {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<>();
	lifecycleBeans.forEach((beanName, bean) -> {
		int shutdownPhase = getPhase(bean);
		LifecycleGroup group = phases.get(shutdownPhase);
		if (group == null) {
			group = new LifecycleGroup(shutdownPhase, this.timeoutPerShutdownPhase, lifecycleBeans, false);
			phases.put(shutdownPhase, group);
		}
		group.add(beanName, bean);
	});
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<>(phases.keySet());
		keys.sort(Collections.reverseOrder());
		for (Integer key : keys) {
			phases.get(key).stop();
		}
	}
}
 
Example #2
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void singleSmartLifecycleAutoStartupWithNonAutoStartupDependency() throws Exception {
	CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
	TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans);
	bean.setAutoStartup(true);
	TestSmartLifecycleBean dependency = TestSmartLifecycleBean.forStartupTests(1, startedBeans);
	dependency.setAutoStartup(false);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.getBeanFactory().registerSingleton("dependency", dependency);
	context.getBeanFactory().registerDependentBean("dependency", "bean");
	assertFalse(bean.isRunning());
	assertFalse(dependency.isRunning());
	context.refresh();
	assertTrue(bean.isRunning());
	assertFalse(dependency.isRunning());
	context.stop();
	assertFalse(bean.isRunning());
	assertFalse(dependency.isRunning());
	assertEquals(1, startedBeans.size());
}
 
Example #3
Source File: AbstractWebSocketIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	logger.debug("Setting up '" + this.testName.getMethodName() + "', client=" +
			this.webSocketClient.getClass().getSimpleName() + ", server=" +
			this.server.getClass().getSimpleName());

	this.wac = new AnnotationConfigWebApplicationContext();
	this.wac.register(getAnnotatedConfigClasses());
	this.wac.register(upgradeStrategyConfigTypes.get(this.server.getClass()));

	if (this.webSocketClient instanceof Lifecycle) {
		((Lifecycle) this.webSocketClient).start();
	}

	this.server.setup();
	this.server.deployConfig(this.wac);
	this.server.start();

	this.wac.setServletContext(this.server.getServletContext());
	this.wac.refresh();
}
 
Example #4
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 #5
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 #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: WebSocketIntegrationTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
	this.client = new ReactorNettyWebSocketClient();

	this.server = new ReactorHttpServer();
	this.server.setHandler(createHttpHandler());
	this.server.afterPropertiesSet();
	this.server.start();

	// Set dynamically chosen port
	this.serverPort = this.server.getPort();

	if (this.client instanceof Lifecycle) {
		((Lifecycle) this.client).start();
	}

	this.gatewayContext = new SpringApplicationBuilder(GatewayConfig.class)
			.properties("ws.server.port:" + this.serverPort, "server.port=0",
					"spring.jmx.enabled=false")
			.run();

	ConfigurableEnvironment env = this.gatewayContext
			.getBean(ConfigurableEnvironment.class);
	this.gatewayPort = Integer.valueOf(env.getProperty("local.server.port"));
}
 
Example #8
Source File: DefaultLifecycleProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void stopBeans() {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();
	for (Map.Entry<String, Lifecycle> entry : lifecycleBeans.entrySet()) {
		Lifecycle bean = entry.getValue();
		int shutdownOrder = getPhase(bean);
		LifecycleGroup group = phases.get(shutdownOrder);
		if (group == null) {
			group = new LifecycleGroup(shutdownOrder, this.timeoutPerShutdownPhase, lifecycleBeans, false);
			phases.put(shutdownOrder, group);
		}
		group.add(entry.getKey(), bean);
	}
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<Integer>(phases.keySet());
		Collections.sort(keys, Collections.reverseOrder());
		for (Integer key : keys) {
			phases.get(key).stop();
		}
	}
}
 
Example #9
Source File: DefaultLifecycleProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void stopBeans() {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<Integer, LifecycleGroup>();
	for (Map.Entry<String, Lifecycle> entry : lifecycleBeans.entrySet()) {
		Lifecycle bean = entry.getValue();
		int shutdownOrder = getPhase(bean);
		LifecycleGroup group = phases.get(shutdownOrder);
		if (group == null) {
			group = new LifecycleGroup(shutdownOrder, this.timeoutPerShutdownPhase, lifecycleBeans, false);
			phases.put(shutdownOrder, group);
		}
		group.add(entry.getKey(), bean);
	}
	if (phases.size() > 0) {
		List<Integer> keys = new ArrayList<Integer>(phases.keySet());
		Collections.sort(keys, Collections.reverseOrder());
		for (Integer key : keys) {
			phases.get(key).stop();
		}
	}
}
 
Example #10
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 #11
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void singleSmartLifecycleWithoutAutoStartup() throws Exception {
	CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
	TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans);
	bean.setAutoStartup(false);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	assertFalse(bean.isRunning());
	context.refresh();
	assertFalse(bean.isRunning());
	assertEquals(0, startedBeans.size());
	context.start();
	assertTrue(bean.isRunning());
	assertEquals(1, startedBeans.size());
	context.stop();
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void stopBeans() {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<>();
	lifecycleBeans.forEach((beanName, bean) -> {
		int shutdownPhase = getPhase(bean);
		LifecycleGroup group = phases.get(shutdownPhase);
		if (group == null) {
			group = new LifecycleGroup(shutdownPhase, this.timeoutPerShutdownPhase, lifecycleBeans, false);
			phases.put(shutdownPhase, group);
		}
		group.add(beanName, bean);
	});
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<>(phases.keySet());
		keys.sort(Collections.reverseOrder());
		for (Integer key : keys) {
			phases.get(key).stop();
		}
	}
}
 
Example #18
Source File: DefaultLifecycleProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void singleSmartLifecycleAutoStartup() throws Exception {
	CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<Lifecycle>();
	TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans);
	bean.setAutoStartup(true);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	assertFalse(bean.isRunning());
	context.refresh();
	assertTrue(bean.isRunning());
	context.stop();
	assertFalse(bean.isRunning());
	assertEquals(1, startedBeans.size());
}
 
Example #19
Source File: WebSocketHttpRequestHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void start() {
	if (!isRunning()) {
		this.running = true;
		if (this.handshakeHandler instanceof Lifecycle) {
			((Lifecycle) this.handshakeHandler).start();
		}
	}
}
 
Example #20
Source File: DefaultLifecycleProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void dependentShutdownFirstButNotSmartLifecycle() throws Exception {
	Assume.group(TestGroup.PERFORMANCE);

	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	TestSmartLifecycleBean bean1 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	TestLifecycleBean simpleBean = TestLifecycleBean.forShutdownTests(stoppedBeans);
	TestSmartLifecycleBean bean2 = TestSmartLifecycleBean.forShutdownTests(2, 300, stoppedBeans);
	TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forShutdownTests(7, 400, stoppedBeans);
	TestSmartLifecycleBean beanMin = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 400, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("beanMin", beanMin);
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.getBeanFactory().registerSingleton("simpleBean", simpleBean);
	context.getBeanFactory().registerDependentBean("bean2", "simpleBean");
	context.refresh();
	assertTrue(beanMin.isRunning());
	assertTrue(bean1.isRunning());
	assertTrue(bean2.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(simpleBean.isRunning());
	simpleBean.start();
	assertTrue(simpleBean.isRunning());
	context.stop();
	assertFalse(beanMin.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean7.isRunning());
	assertFalse(simpleBean.isRunning());
	assertEquals(5, stoppedBeans.size());
	assertEquals(7, getPhase(stoppedBeans.get(0)));
	assertEquals(0, getPhase(stoppedBeans.get(1)));
	assertEquals(2, getPhase(stoppedBeans.get(2)));
	assertEquals(1, getPhase(stoppedBeans.get(3)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(4)));
}
 
Example #21
Source File: TransportHandlingSockJsService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void stop() {
	if (isRunning()) {
		this.running = false;
		for (TransportHandler handler : this.handlers.values()) {
			if (handler instanceof Lifecycle) {
				((Lifecycle) handler).stop();
			}
		}
	}
}
 
Example #22
Source File: WebSocketTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
	if (!isRunning()) {
		if (this.webSocketClient instanceof Lifecycle) {
			((Lifecycle) this.webSocketClient).start();
		}
		else {
			this.running = true;
		}
	}
}
 
Example #23
Source File: WebSocketConnectionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void stopInternal() throws Exception {
	if (this.client instanceof Lifecycle && ((Lifecycle) client).isRunning()) {
		((Lifecycle) client).stop();
	}
	super.stopInternal();
}
 
Example #24
Source File: WebSocketConnectionManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void startInternal() {
	if (this.client instanceof Lifecycle && !((Lifecycle) this.client).isRunning()) {
		((Lifecycle) this.client).start();
	}
	super.startInternal();
}
 
Example #25
Source File: WebSocketHttpRequestHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void stop() {
	if (isRunning()) {
		this.running = false;
		if (this.handshakeHandler instanceof Lifecycle) {
			((Lifecycle) this.handshakeHandler).stop();
		}
	}
}
 
Example #26
Source File: WebSocketHttpRequestHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void start() {
	if (!isRunning()) {
		this.running = true;
		if (this.handshakeHandler instanceof Lifecycle) {
			((Lifecycle) this.handshakeHandler).start();
		}
	}
}
 
Example #27
Source File: SockJsHttpRequestHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void stop() {
	if (isRunning()) {
		this.running = false;
		if (this.sockJsService instanceof Lifecycle) {
			((Lifecycle) this.sockJsService).stop();
		}
	}
}
 
Example #28
Source File: WebSocketConnectionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void startInternal() {
	if (this.client instanceof Lifecycle && !((Lifecycle) this.client).isRunning()) {
		((Lifecycle) this.client).start();
	}
	super.startInternal();
}
 
Example #29
Source File: DataStoreWriterFactoryBean.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
	try {
		storeWriter.close();
	} catch (IOException e) {
		throw new IllegalStateException("Error while closing StoreWriter", e);
	}
	if (storeWriter instanceof Lifecycle) {
		((Lifecycle) storeWriter).stop();
	}
}
 
Example #30
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));
}