Search in sources :

Example 1 with CollectionArgumentResolver

use of org.springframework.integration.handler.support.CollectionArgumentResolver in project spring-integration by spring-projects.

the class MessageHandlerMethodFactoryCreatingFactoryBean method buildArgumentResolvers.

private List<HandlerMethodArgumentResolver> buildArgumentResolvers(boolean listCapable) {
    List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
    resolvers.add(new PayloadExpressionArgumentResolver());
    resolvers.add(new NullAwarePayloadArgumentResolver(this.argumentResolverMessageConverter));
    resolvers.add(new PayloadsArgumentResolver());
    if (listCapable) {
        resolvers.add(new CollectionArgumentResolver(true));
    }
    resolvers.add(new MapArgumentResolver());
    for (HandlerMethodArgumentResolver resolver : resolvers) {
        if (resolver instanceof BeanFactoryAware) {
            ((BeanFactoryAware) resolver).setBeanFactory(this.beanFactory);
        }
        if (resolver instanceof InitializingBean) {
            try {
                ((InitializingBean) resolver).afterPropertiesSet();
            } catch (Exception ex) {
                throw new BeanInitializationException("Cannot initialize 'HandlerMethodArgumentResolver'", ex);
            }
        }
    }
    return resolvers;
}
Also used : PayloadsArgumentResolver(org.springframework.integration.handler.support.PayloadsArgumentResolver) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) NullAwarePayloadArgumentResolver(org.springframework.integration.support.NullAwarePayloadArgumentResolver) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) PayloadExpressionArgumentResolver(org.springframework.integration.handler.support.PayloadExpressionArgumentResolver) CollectionArgumentResolver(org.springframework.integration.handler.support.CollectionArgumentResolver) ArrayList(java.util.ArrayList) MapArgumentResolver(org.springframework.integration.handler.support.MapArgumentResolver) InitializingBean(org.springframework.beans.factory.InitializingBean) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) BeansException(org.springframework.beans.BeansException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Example 2 with CollectionArgumentResolver

use of org.springframework.integration.handler.support.CollectionArgumentResolver in project spring-integration by spring-projects.

the class JdbcMessageStoreTests method testMessageGroupStreamNoConnectionPoolLeak.

@Test
@Transactional(propagation = Propagation.NEVER)
public void testMessageGroupStreamNoConnectionPoolLeak() throws NoSuchMethodException {
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(this.dataSource);
    PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, null);
    GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(2);
    config.setMaxWaitMillis(500);
    ObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(poolFactory, config);
    poolFactory.setPool(connPool);
    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(connPool);
    JdbcMessageStore pooledMessageStore = new JdbcMessageStore(poolingDataSource);
    CollectionArgumentResolver collectionArgumentResolver = new CollectionArgumentResolver(true);
    collectionArgumentResolver.setBeanFactory(new DefaultListableBeanFactory());
    Method methodForCollectionOfPayloads = getClass().getMethod("methodForCollectionOfPayloads", Collection.class);
    MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(methodForCollectionOfPayloads, 0);
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("test data").build();
    pooledMessageStore.addMessagesToGroup(groupId, message);
    // it failed with "Cannot get a connection, pool error Timeout waiting for idle object"
    for (int i = 0; i < 3; i++) {
        Object result = collectionArgumentResolver.resolveArgument(methodParameter, new GenericMessage<>(pooledMessageStore.getMessageGroup(groupId).getMessages()));
        assertThat(result).isInstanceOf(Collection.class).asList().hasSize(1).contains("test data");
    }
    pooledMessageStore.removeMessageGroup(groupId);
}
Also used : PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Method(java.lang.reflect.Method) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) CollectionArgumentResolver(org.springframework.integration.handler.support.CollectionArgumentResolver) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Collection(java.util.Collection) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) Test(org.junit.jupiter.api.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CollectionArgumentResolver (org.springframework.integration.handler.support.CollectionArgumentResolver)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 DataSourceConnectionFactory (org.apache.commons.dbcp2.DataSourceConnectionFactory)1 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)1 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)1 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)1 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 Test (org.junit.jupiter.api.Test)1 BeansException (org.springframework.beans.BeansException)1 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 MethodParameter (org.springframework.core.MethodParameter)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 MapArgumentResolver (org.springframework.integration.handler.support.MapArgumentResolver)1 PayloadExpressionArgumentResolver (org.springframework.integration.handler.support.PayloadExpressionArgumentResolver)1