Search in sources :

Example 81 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class FeedEntryMessageSource method onInit.

@Override
protected void onInit() throws Exception {
    if (this.metadataStore == null) {
        // first try to look for a 'messageStore' in the context
        BeanFactory beanFactory = this.getBeanFactory();
        if (beanFactory != null) {
            this.metadataStore = IntegrationContextUtils.getMetadataStore(beanFactory);
        }
        // if no 'messageStore' in context, fall back to in-memory Map-based default
        if (this.metadataStore == null) {
            this.metadataStore = new SimpleMetadataStore();
        }
    }
    String lastTimeValue = this.metadataStore.get(this.metadataKey);
    if (StringUtils.hasText(lastTimeValue)) {
        this.lastTime = Long.parseLong(lastTimeValue);
    }
    this.initialized = true;
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore)

Example 82 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class MessagingMethodInvokerHelper method initialize.

private synchronized void initialize() throws Exception {
    if (!this.initialized) {
        BeanFactory beanFactory = getBeanFactory();
        if (beanFactory != null && beanFactory.containsBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)) {
            try {
                MessageConverter messageConverter = beanFactory.getBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME, MessageConverter.class);
                this.messageHandlerMethodFactory.setMessageConverter(messageConverter);
                HandlerMethodArgumentResolversHolder handlerMethodArgumentResolversHolder = beanFactory.getBean(this.canProcessMessageList ? IntegrationContextUtils.LIST_ARGUMENT_RESOLVERS_BEAN_NAME : IntegrationContextUtils.ARGUMENT_RESOLVERS_BEAN_NAME, HandlerMethodArgumentResolversHolder.class);
                this.messageHandlerMethodFactory.setCustomArgumentResolvers(handlerMethodArgumentResolversHolder.getResolvers());
            } catch (NoSuchBeanDefinitionException e) {
                configureLocalMessageHandlerFactory();
            }
        } else {
            configureLocalMessageHandlerFactory();
        }
        this.messageHandlerMethodFactory.afterPropertiesSet();
        prepareEvaluationContext();
        this.initialized = true;
    }
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) MessageConverter(org.springframework.messaging.converter.MessageConverter) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 83 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class ServiceActivatorEndpointTests method testBeanFactoryPopulation.

@Test
public void testBeanFactoryPopulation() {
    ServiceActivatingHandler endpoint = this.createEndpoint();
    BeanFactory mock = mock(BeanFactory.class);
    endpoint.setBeanFactory(mock);
    endpoint.afterPropertiesSet();
    Object beanFactory = TestUtils.getPropertyValue(endpoint, "processor.beanFactory");
    assertNotNull(beanFactory);
    assertSame(mock, beanFactory);
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 84 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class IdempotentReceiverTests method testIdempotentReceiverInterceptor.

@Test
public void testIdempotentReceiverInterceptor() {
    ConcurrentMetadataStore store = new SimpleMetadataStore();
    ExpressionEvaluatingMessageProcessor<String> idempotentKeyStrategy = new ExpressionEvaluatingMessageProcessor<>(new SpelExpressionParser().parseExpression("payload"));
    BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
    idempotentKeyStrategy.setBeanFactory(beanFactory);
    IdempotentReceiverInterceptor idempotentReceiverInterceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(idempotentKeyStrategy, store));
    idempotentReceiverInterceptor.setThrowExceptionOnRejection(true);
    AtomicReference<Message<?>> handled = new AtomicReference<>();
    MessageHandler idempotentReceiver = handled::set;
    ProxyFactory proxyFactory = new ProxyFactory(idempotentReceiver);
    proxyFactory.addAdvice(idempotentReceiverInterceptor);
    idempotentReceiver = (MessageHandler) proxyFactory.getProxy();
    idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
    assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
    assertNotNull(store.get("foo"));
    try {
        idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
        fail("MessageRejectedException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageRejectedException.class));
    }
    idempotentReceiverInterceptor.setThrowExceptionOnRejection(false);
    idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
    assertTrue(handled.get().getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
    assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
}
Also used : ExpressionEvaluatingMessageProcessor(org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) ConcurrentMetadataStore(org.springframework.integration.metadata.ConcurrentMetadataStore) MessageRejectedException(org.springframework.integration.MessageRejectedException) MetadataStoreSelector(org.springframework.integration.selector.MetadataStoreSelector) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 85 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-integration by spring-projects.

the class RouterConcurrencyTest method test.

@Test
public void test() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    final Semaphore semaphore = new Semaphore(1);
    final AbstractMessageRouter router = new AbstractMessageRouter() {

        @Override
        protected Collection<MessageChannel> determineTargetChannels(Message<?> message) {
            return null;
        }

        @Override
        protected void setConversionService(ConversionService conversionService) {
            try {
                if (count.incrementAndGet() > 1) {
                    Thread.sleep(2000);
                }
                super.setConversionService(conversionService);
                semaphore.release();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    };
    final AtomicInteger beanCounter = new AtomicInteger();
    BeanFactory beanFactory = mock(BeanFactory.class);
    doAnswer(invocation -> {
        if (beanCounter.getAndIncrement() < 2) {
            semaphore.tryAcquire(4, TimeUnit.SECONDS);
        }
        return false;
    }).when(beanFactory).containsBean(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME);
    router.setBeanFactory(beanFactory);
    ExecutorService exec = Executors.newFixedThreadPool(2);
    final List<ConversionService> returns = Collections.synchronizedList(new ArrayList<ConversionService>());
    Runnable runnable = () -> {
        ConversionService requiredConversionService = router.getRequiredConversionService();
        returns.add(requiredConversionService);
    };
    exec.execute(runnable);
    exec.execute(runnable);
    exec.shutdown();
    exec.awaitTermination(10, TimeUnit.SECONDS);
    assertEquals(2, returns.size());
    assertNotNull(returns.get(0));
    assertNotNull(returns.get(1));
}
Also used : Message(org.springframework.messaging.Message) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversionService(org.springframework.core.convert.ConversionService) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)121 Test (org.junit.jupiter.api.Test)30 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)25 Test (org.junit.Test)20 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)16 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)15 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutorService (java.util.concurrent.ExecutorService)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)4 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)4