List of usage examples for org.springframework.context.support GenericApplicationContext getBeanFactory
@Override public final ConfigurableListableBeanFactory getBeanFactory()
From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java
@Test public void testFailOnFirstUseWithMissingBroker() throws Exception { SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost"); connectionFactory.setPort(434343);/*from w w w . ja va 2 s . co m*/ GenericApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.getBeanFactory().registerSingleton("foo", new Queue("queue")); RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory); rabbitAdmin.setApplicationContext(applicationContext); rabbitAdmin.setAutoStartup(true); rabbitAdmin.afterPropertiesSet(); exception.expect(IllegalArgumentException.class); rabbitAdmin.declareQueue(); connectionFactory.destroy(); }
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();// w ww . j ava 2s. com 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();/*w w w . ja v a 2s. c om*/ 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();// www. j a va2s .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();// ww w . jav a2 s . c o 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.cloud.function.web.function.FunctionEndpointInitializer.java
private void registerEndpoint(GenericApplicationContext context) { context.registerBean(StringConverter.class, () -> new BasicStringConverter(context.getBean(FunctionInspector.class), context.getBeanFactory())); context.registerBean(RequestProcessor.class, () -> new RequestProcessor(context.getBean(FunctionInspector.class), context.getBeanProvider(JsonMapper.class), context.getBean(StringConverter.class), context.getBeanProvider(ServerCodecConfigurer.class))); context.registerBean(FunctionEndpointFactory.class, () -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class), context.getBean(FunctionInspector.class), context.getBean(RequestProcessor.class), context.getEnvironment())); context.registerBean(RouterFunction.class, () -> context.getBean(FunctionEndpointFactory.class).functionEndpoints()); }
From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java
@Test public void genericApplicationContext() throws Exception { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); ac.getBeanFactory().registerScope("myScope", new Scope() { @Override/* w ww .j a v a 2s . c o m*/ public Object get(String name, ObjectFactory<?> objectFactory) { return objectFactory.getObject(); } @Override public Object remove(String name) { return null; } @Override public void registerDestructionCallback(String name, Runnable callback) { } @Override public Object resolveContextualObject(String key) { if (key.equals("mySpecialAttr")) { return "42"; } else { return null; } } @Override public String getConversationId() { return null; } }); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties placeholders = new Properties(); placeholders.setProperty("code", "123"); ppc.setProperties(placeholders); ac.addBeanFactoryPostProcessor(ppc); GenericBeanDefinition bd0 = new GenericBeanDefinition(); bd0.setBeanClass(TestBean.class); bd0.getPropertyValues().add("name", "myName"); bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original")); ac.registerBeanDefinition("tb0", bd0); GenericBeanDefinition bd1 = new GenericBeanDefinition(); bd1.setBeanClassName("#{tb0.class}"); bd1.setScope("myScope"); bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ"); bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}"); ac.registerBeanDefinition("tb1", bd1); GenericBeanDefinition bd2 = new GenericBeanDefinition(); bd2.setBeanClassName("#{tb1.class.name}"); bd2.setScope("myScope"); bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }"); bd2.getPropertyValues().add("age", "#{mySpecialAttr}"); bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}"); ac.registerBeanDefinition("tb2", bd2); GenericBeanDefinition bd3 = new GenericBeanDefinition(); bd3.setBeanClass(ValueTestBean.class); bd3.setScope("myScope"); ac.registerBeanDefinition("tb3", bd3); GenericBeanDefinition bd4 = new GenericBeanDefinition(); bd4.setBeanClass(ConstructorValueTestBean.class); bd4.setScope("myScope"); ac.registerBeanDefinition("tb4", bd4); GenericBeanDefinition bd5 = new GenericBeanDefinition(); bd5.setBeanClass(MethodValueTestBean.class); bd5.setScope("myScope"); ac.registerBeanDefinition("tb5", bd5); GenericBeanDefinition bd6 = new GenericBeanDefinition(); bd6.setBeanClass(PropertyValueTestBean.class); bd6.setScope("myScope"); ac.registerBeanDefinition("tb6", bd6); System.getProperties().put("country", "UK"); try { ac.refresh(); TestBean tb0 = ac.getBean("tb0", TestBean.class); TestBean tb1 = ac.getBean("tb1", TestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb1.getName()); assertEquals(42, tb1.getAge()); TestBean tb2 = ac.getBean("tb2", TestBean.class); assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName()); assertEquals(42, tb2.getAge()); assertEquals("123 UK", tb2.getCountry()); ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb3.name); assertEquals(42, tb3.age); assertEquals(42, tb3.ageFactory.getObject().intValue()); assertEquals("123 UK", tb3.country); assertEquals("123 UK", tb3.countryFactory.getObject()); System.getProperties().put("country", "US"); assertEquals("123 UK", tb3.country); assertEquals("123 US", tb3.countryFactory.getObject()); System.getProperties().put("country", "UK"); assertEquals("123 UK", tb3.country); assertEquals("123 UK", tb3.countryFactory.getObject()); assertSame(tb0, tb3.tb); tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3); assertEquals("123 UK", tb3.countryFactory.getObject()); ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb4.name); assertEquals(42, tb4.age); assertEquals("123 UK", tb4.country); assertSame(tb0, tb4.tb); MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb5.name); assertEquals(42, tb5.age); assertEquals("123 UK", tb5.country); assertSame(tb0, tb5.tb); PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb6.name); assertEquals(42, tb6.age); assertEquals("123 UK", tb6.country); assertSame(tb0, tb6.tb); } finally { System.getProperties().remove("country"); } }
From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java
@Test public void prototypeCreationReevaluatesExpressions() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); GenericConversionService cs = new GenericConversionService(); cs.addConverter(String.class, String.class, new Converter<String, String>() { @Override/*from w ww. j a v a 2 s. c om*/ public String convert(String source) { return source.trim(); } }); ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs); RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("country", "#{systemProperties.country}"); rbd.getPropertyValues().add("country2", new TypedStringValue("-#{systemProperties.country}-")); ac.registerBeanDefinition("test", rbd); ac.refresh(); try { System.getProperties().put("name", "juergen1"); System.getProperties().put("country", " UK1 "); PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test"); assertEquals("juergen1", tb.getName()); assertEquals("UK1", tb.getCountry()); assertEquals("-UK1-", tb.getCountry2()); System.getProperties().put("name", "juergen2"); System.getProperties().put("country", " UK2 "); tb = (PrototypeTestBean) ac.getBean("test"); assertEquals("juergen2", tb.getName()); assertEquals("UK2", tb.getCountry()); assertEquals("-UK2-", tb.getCountry2()); } finally { System.getProperties().remove("name"); System.getProperties().remove("country"); } }
From source file:org.springframework.context.groovy.GroovyBeanDefinitionReader.java
/** * Register a set of beans with the given bean registry. Most * application contexts are bean registries. *//*from ww w .j a v a 2s . c o m*/ public void registerBeans(BeanDefinitionRegistry registry) { finalizeDeferredProperties(); if (registry instanceof GenericApplicationContext) { GenericApplicationContext ctx = (GenericApplicationContext) registry; ctx.setClassLoader(this.classLoader); ctx.getBeanFactory().setBeanClassLoader(this.classLoader); } springConfig.registerBeansWithRegistry(registry); }