Example usage for org.springframework.context.support GenericApplicationContext GenericApplicationContext

List of usage examples for org.springframework.context.support GenericApplicationContext GenericApplicationContext

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext GenericApplicationContext.

Prototype

public GenericApplicationContext() 

Source Link

Document

Create a new GenericApplicationContext.

Usage

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java

private void testRecoverDeletedQueueGuts(boolean autoDeclare) throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    if (autoDeclare) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.getBeanFactory().registerSingleton("foo", new Queue(Q1));
        RabbitAdmin admin = new RabbitAdmin(cf);
        admin.setApplicationContext(context);
        context.getBeanFactory().registerSingleton("admin", admin);
        context.refresh();//from   w w  w  . j  a v  a  2 s.c o  m
        container.setApplicationContext(context);
    }
    container.setAutoDeclare(autoDeclare);
    container.setQueueNames(Q1, Q2);
    container.setConsumersPerQueue(2);
    container.setConsumersPerQueue(2);
    container.setMessageListener(m -> {
    });
    container.setFailedDeclarationRetryInterval(500);
    container.setBeanName("deleteQauto=" + autoDeclare);
    container.setConsumerTagStrategy(new Tag());
    container.afterPropertiesSet();
    container.start();
    assertTrue(consumersOnQueue(Q1, 2));
    assertTrue(consumersOnQueue(Q2, 2));
    assertTrue(activeConsumerCount(container, 4));
    brokerRunning.deleteQueues(Q1);
    assertTrue(consumersOnQueue(Q2, 2));
    assertTrue(activeConsumerCount(container, 2));
    assertTrue(restartConsumerCount(container, 2));
    RabbitAdmin admin = new RabbitAdmin(cf);
    if (!autoDeclare) {
        Thread.sleep(2000);
        admin.declareQueue(new Queue(Q1));
    }
    assertTrue(consumersOnQueue(Q1, 2));
    assertTrue(consumersOnQueue(Q2, 2));
    assertTrue(activeConsumerCount(container, 4));
    container.stop();
    assertTrue(consumersOnQueue(Q1, 0));
    assertTrue(consumersOnQueue(Q2, 0));
    assertTrue(activeConsumerCount(container, 0));
    assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size());
    cf.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java

private void testRecoverDeletedQueueGuts(boolean autoDeclare) throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    if (autoDeclare) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.getBeanFactory().registerSingleton("foo", new Queue(Q1));
        RabbitAdmin admin = new RabbitAdmin(cf);
        admin.setApplicationContext(context);
        context.getBeanFactory().registerSingleton("admin", admin);
        context.refresh();//from   w ww  .j a va  2 s .c  o  m
        container.setApplicationContext(context);
    }
    container.setAutoDeclare(autoDeclare);
    container.setQueueNames(Q1, Q2);
    container.setConsumersPerQueue(2);
    container.setConsumersPerQueue(2);
    container.setMessageListener(m -> {
    });
    container.setFailedDeclarationRetryInterval(500);
    container.setBeanName("deleteQauto=" + autoDeclare);
    container.setConsumerTagStrategy(new Tag());
    container.afterPropertiesSet();
    container.start();
    assertTrue(consumersOnQueue(Q1, 2));
    assertTrue(consumersOnQueue(Q2, 2));
    assertTrue(activeConsumerCount(container, 4));
    brokerRunning.getAdmin().deleteQueue(Q1);
    assertTrue(consumersOnQueue(Q2, 2));
    assertTrue(activeConsumerCount(container, 2));
    assertTrue(restartConsumerCount(container, 2));
    if (!autoDeclare) {
        Thread.sleep(2000);
        brokerRunning.getAdmin().declareQueue(new Queue(Q1));
    }
    assertTrue(consumersOnQueue(Q1, 2));
    assertTrue(consumersOnQueue(Q2, 2));
    assertTrue(activeConsumerCount(container, 4));
    container.stop();
    assertTrue(consumersOnQueue(Q1, 0));
    assertTrue(consumersOnQueue(Q2, 0));
    assertTrue(activeConsumerCount(container, 0));
    assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size());
    cf.destroy();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java

@Test
public void testListenFromAnonQueue() throws Exception {
    AnonymousQueue queue = new AnonymousQueue();
    CountDownLatch latch = new CountDownLatch(10);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(
            template.getConnectionFactory());
    container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch)));
    container.setQueueNames(queue.getName());
    container.setConcurrentConsumers(2);
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("foo", queue);
    context.refresh();//from ww  w .j av  a2  s .c  o  m
    container.setApplicationContext(context);
    RabbitAdmin admin = new RabbitAdmin(this.template.getConnectionFactory());
    admin.setApplicationContext(context);
    container.setRabbitAdmin(admin);
    container.afterPropertiesSet();
    container.start();
    for (int i = 0; i < 10; i++) {
        template.convertAndSend(queue.getName(), i + "foo");
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    container.stop();
    container.start();
    latch = new CountDownLatch(10);
    container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch)));
    for (int i = 0; i < 10; i++) {
        template.convertAndSend(queue.getName(), i + "foo");
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    container.stop();
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java

@Test
public void testExclusive() throws Exception {
    Log logger = spy(TestUtils.getPropertyValue(this.template.getConnectionFactory(), "logger", Log.class));
    doReturn(true).when(logger).isInfoEnabled();
    new DirectFieldAccessor(this.template.getConnectionFactory()).setPropertyValue("logger", logger);
    CountDownLatch latch1 = new CountDownLatch(1000);
    SimpleMessageListenerContainer container1 = new SimpleMessageListenerContainer(
            template.getConnectionFactory());
    container1.setMessageListener(new MessageListenerAdapter(new PojoListener(latch1)));
    container1.setQueueNames(queue.getName());
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("foo", queue);
    context.refresh();/*from   ww w . j a  v  a2s. co  m*/
    container1.setApplicationContext(context);
    container1.setExclusive(true);
    container1.afterPropertiesSet();
    container1.start();
    int n = 0;
    while (n++ < 100 && container1.getActiveConsumerCount() < 1) {
        Thread.sleep(100);
    }
    assertTrue(n < 100);
    CountDownLatch latch2 = new CountDownLatch(1000);
    SimpleMessageListenerContainer container2 = new SimpleMessageListenerContainer(
            template.getConnectionFactory());
    container2.setMessageListener(new MessageListenerAdapter(new PojoListener(latch2)));
    container2.setQueueNames(queue.getName());
    container2.setApplicationContext(context);
    container2.setRecoveryInterval(1000);
    container2.setExclusive(true); // not really necessary, but likely people will make all consumers exclusive.
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    container2.setApplicationEventPublisher(publisher);
    container2.afterPropertiesSet();
    Log containerLogger = spy(TestUtils.getPropertyValue(container2, "logger", Log.class));
    doReturn(true).when(containerLogger).isWarnEnabled();
    new DirectFieldAccessor(container2).setPropertyValue("logger", containerLogger);
    container2.start();
    for (int i = 0; i < 1000; i++) {
        template.convertAndSend(queue.getName(), i + "foo");
    }
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertEquals(1000, latch2.getCount());
    container1.stop();
    // container 2 should recover and process the next batch of messages
    for (int i = 0; i < 1000; i++) {
        template.convertAndSend(queue.getName(), i + "foo");
    }
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    container2.stop();
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger, atLeastOnce()).info(captor.capture());
    assertThat(captor.getAllValues(), contains(containsString("exclusive")));
    ArgumentCaptor<ListenerContainerConsumerFailedEvent> eventCaptor = ArgumentCaptor
            .forClass(ListenerContainerConsumerFailedEvent.class);
    verify(publisher).publishEvent(eventCaptor.capture());
    ListenerContainerConsumerFailedEvent event = eventCaptor.getValue();
    assertEquals("Consumer raised exception, attempting restart", event.getReason());
    assertFalse(event.isFatal());
    assertThat(event.getThrowable(), instanceOf(AmqpIOException.class));
    verify(containerLogger).warn(any());
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testAspectsAndAdvisorNotAppliedToManySingletonsIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);/*from  www  .  jav a 2 s.  c o  m*/
    GenericApplicationContext ac = new GenericApplicationContext();
    new XmlBeanDefinitionReader(ac)
            .loadBeanDefinitions(new ClassPathResource(qName("aspectsPlusAdvisor.xml"), getClass()));
    for (int i = 0; i < 10000; i++) {
        ac.registerBeanDefinition("singleton" + i, new RootBeanDefinition(NestedTestBean.class));
    }
    StopWatch sw = new StopWatch();
    sw.start("Singleton Creation");
    ac.refresh();
    sw.stop();

    // What's a reasonable expectation for _any_ server or developer machine load?
    // 8 seconds?
    assertStopWatchTimeLimit(sw, 8000);
}

From source file:org.springframework.boot.context.initializer.LoggingApplicationContextInitializerTests.java

@Test
public void testDefaultConfigLocation() {
    GenericApplicationContext context = new GenericApplicationContext();
    this.initializer.initialize(context);
    this.logger.info("Hello world");
    String output = this.outputCapture.toString().trim();
    assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
    assertFalse("Wrong output:\n" + output, output.contains("???"));
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListenerTests.java

@Test
public void closingChildContextDoesNotCleanUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils
            .getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    GenericApplicationContext childContext = new GenericApplicationContext();
    childContext.setParent(this.context);
    multicastEvent(new ContextClosedEvent(childContext));
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(loggingSystem.cleanedUp).isTrue();
    childContext.close();//from w  ww.  j  ava  2s  . co m
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListenerTests.java

@Test
public void applicationFailedEventCleansUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils
            .getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ApplicationFailedEvent(this.springApplication, new String[0],
            new GenericApplicationContext(), new Exception()));
    assertThat(loggingSystem.cleanedUp).isTrue();
}

From source file:org.springframework.cloud.stream.binder.AbstractBinderTests.java

protected DirectChannel createBindableChannel(String channelName, BindingProperties bindingProperties)
        throws Exception {
    BindingServiceProperties bindingServiceProperties = new BindingServiceProperties();
    bindingServiceProperties.getBindings().put(channelName, bindingProperties);
    ConfigurableApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.refresh();//  ww  w  . j a v  a  2  s  .  c  om
    bindingServiceProperties.setApplicationContext(applicationContext);
    bindingServiceProperties.setConversionService(new DefaultConversionService());
    bindingServiceProperties.afterPropertiesSet();
    DirectChannel channel = new DirectChannel();
    channel.setBeanName(channelName);
    MessageConverterConfigurer messageConverterConfigurer = new MessageConverterConfigurer(
            bindingServiceProperties, new CompositeMessageConverterFactory(null, null));
    messageConverterConfigurer.setBeanFactory(applicationContext.getBeanFactory());
    messageConverterConfigurer.afterPropertiesSet();
    messageConverterConfigurer.configureOutputChannel(channel, channelName);
    return channel;
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithResourcePropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();/*ww w .  j  ava 2  s.c  o m*/

    RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}