List of usage examples for org.springframework.context ConfigurableApplicationContext close
@Override
void close();
From source file:org.springframework.boot.SpringApplication.java
private static void close(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext closable = (ConfigurableApplicationContext) context; closable.close(); }// ww w . j a va 2 s . c o m }
From source file:org.springframework.cloud.config.server.environment.NativeEnvironmentRepository.java
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment);/*from ww w . jav a 2s . c o m*/ builder.web(false).bannerMode(Mode.OFF); if (!logger.isDebugEnabled()) { // Make the mini-application startup less verbose builder.logStartupInfo(false); } String[] args = getArgs(config, profile, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application().setListeners(Arrays.asList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
From source file:org.springframework.cloud.config.server.NativeEnvironmentRepository.java
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment);// w ww .ja v a 2 s .c om builder.web(false).showBanner(false); String[] args = getArgs(config, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application().setListeners(Collections.singletonList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test(expected = NoSuchBeanDefinitionException.class) public void checkDisabledConfiguration() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false"); try {// www . j ava2 s. c om applicationContext.getBean(Emitter.class); } catch (Exception e) { throw e; } finally { applicationContext.close(); } }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test public void defaultIncludes() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500", "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo"); Emitter emitterSource = applicationContext.getBean(Emitter.class); MessageCollector collector = applicationContext.getBean(MessageCollector.class); Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS); Assert.assertNotNull(message);//w w w . jav a2 s . co m ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class); ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(), ApplicationMetrics.class); Assert.assertTrue( contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics())); Assert.assertEquals("application", applicationMetrics.getName()); Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties())); applicationContext.close(); }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test public void customAppNameAndIndex() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500", "--spring.application.name=foo", "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo"); Emitter emitterSource = applicationContext.getBean(Emitter.class); MessageCollector collector = applicationContext.getBean(MessageCollector.class); Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS); Assert.assertNotNull(message);//from w w w .j a va 2s. co m ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class); ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(), ApplicationMetrics.class); Assert.assertTrue( contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics())); Assert.assertTrue(contains("mem", applicationMetrics.getMetrics())); Assert.assertEquals("foo", applicationMetrics.getName()); Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties())); applicationContext.close(); }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test public void usingPrefix() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500", "--spring.cloud.stream.metrics.prefix=foo", "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo"); Emitter emitterSource = applicationContext.getBean(Emitter.class); MessageCollector collector = applicationContext.getBean(MessageCollector.class); Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS); Assert.assertNotNull(message);//w w w . jav a 2 s.c o m ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class); ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(), ApplicationMetrics.class); Assert.assertTrue( contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics())); Assert.assertTrue(contains("mem", applicationMetrics.getMetrics())); Assert.assertEquals("foo.application", applicationMetrics.getName()); Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties())); applicationContext.close(); }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test public void includesExcludesDefaultConfig() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500", "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo", "--spring.metrics.export.includes=mem**", "--spring.metrics.export.excludes=integration**"); Emitter emitterSource = applicationContext.getBean(Emitter.class); MessageCollector collector = applicationContext.getBean(MessageCollector.class); Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS); Assert.assertNotNull(message);/*from w w w . j a va 2s . c o m*/ ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class); ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(), ApplicationMetrics.class); Assert.assertFalse( contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics())); Assert.assertTrue(contains("mem", applicationMetrics.getMetrics())); Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties())); applicationContext.close(); }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test public void includesExcludesWithApplicationMetricsConfiguration() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.triggers.application.delay-millis=500", "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo", "--spring.metrics.export.triggers.application.includes=mem**", "--spring.metrics.export.triggers.application.excludes=integration**"); Emitter emitterSource = applicationContext.getBean(Emitter.class); MessageCollector collector = applicationContext.getBean(MessageCollector.class); Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS); Assert.assertNotNull(message);/*from w ww. j a v a 2s . c o m*/ ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class); ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(), ApplicationMetrics.class); Assert.assertFalse( contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics())); Assert.assertTrue(contains("mem", applicationMetrics.getMetrics())); Assert.assertTrue(CollectionUtils.isEmpty(applicationMetrics.getProperties())); applicationContext.close(); }
From source file:org.springframework.cloud.stream.metrics.ApplicationMetricsExporterTests.java
@Test public void includesExcludesWithProperties() throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(BinderExporterApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.metrics.export.delay-millis=500", "--spring.cloud.stream.bindings." + Emitter.APPLICATION_METRICS + ".destination=foo", "--spring.metrics.export.includes=integration**", "--spring.cloud.stream.metrics.properties=java**,spring.test.env**"); Emitter emitterSource = applicationContext.getBean(Emitter.class); MessageCollector collector = applicationContext.getBean(MessageCollector.class); Message<?> message = collector.forChannel(emitterSource.applicationMetrics()).poll(10, TimeUnit.SECONDS); Assert.assertNotNull(message);// w w w . j a v a 2s . c o m ObjectMapper mapper = applicationContext.getBean(ObjectMapper.class); ApplicationMetrics applicationMetrics = mapper.readValue((String) message.getPayload(), ApplicationMetrics.class); Assert.assertFalse(contains("mem", applicationMetrics.getMetrics())); Assert.assertTrue( contains("integration.channel.errorChannel.errorRate.mean", applicationMetrics.getMetrics())); Assert.assertFalse(CollectionUtils.isEmpty(applicationMetrics.getProperties())); Assert.assertTrue(applicationMetrics.getProperties().get("spring.test.env.syntax").equals("testing")); applicationContext.close(); }