Search in sources :

Example 6 with MethodInvokingMessageSource

use of org.springframework.integration.endpoint.MethodInvokingMessageSource in project spring-integration by spring-projects.

the class MethodInvokingMessageSourceTests method testHeaderExpressions.

@Test
public void testHeaderExpressions() {
    Map<String, Expression> headerExpressions = new HashMap<String, Expression>();
    headerExpressions.put("foo", new LiteralExpression("abc"));
    headerExpressions.put("bar", new SpelExpressionParser().parseExpression("new Integer(123)"));
    MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setBeanFactory(mock(BeanFactory.class));
    source.setObject(new TestBean());
    source.setMethodName("validMethod");
    source.setHeaderExpressions(headerExpressions);
    Message<?> result = source.receive();
    assertNotNull(result);
    assertNotNull(result.getPayload());
    assertEquals("valid", result.getPayload());
    assertEquals("abc", result.getHeaders().get("foo"));
    assertEquals(123, result.getHeaders().get("bar"));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MethodInvokingMessageSource(org.springframework.integration.endpoint.MethodInvokingMessageSource) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) HashMap(java.util.HashMap) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 7 with MethodInvokingMessageSource

use of org.springframework.integration.endpoint.MethodInvokingMessageSource in project spring-integration by spring-projects.

the class MethodInvokingMessageSourceTests method testValidMethod.

@Test
public void testValidMethod() {
    MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setBeanFactory(mock(BeanFactory.class));
    source.setObject(new TestBean());
    source.setMethodName("validMethod");
    Message<?> result = source.receive();
    assertNotNull(result);
    assertNotNull(result.getPayload());
    assertEquals("valid", result.getPayload());
}
Also used : MethodInvokingMessageSource(org.springframework.integration.endpoint.MethodInvokingMessageSource) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 8 with MethodInvokingMessageSource

use of org.springframework.integration.endpoint.MethodInvokingMessageSource in project spring-integration by spring-projects.

the class InboundChannelAdapterAnnotationPostProcessor method createMessageSource.

private MessageSource<?> createMessageSource(Object bean, String beanName, Method method) {
    MessageSource<?> messageSource = null;
    if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        Object target = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
        Assert.isTrue(target instanceof MessageSource || target instanceof Supplier, "The '" + this.annotationType + "' on @Bean method " + "level is allowed only for: " + MessageSource.class.getName() + " or " + Supplier.class.getName() + " beans");
        if (target instanceof MessageSource<?>) {
            messageSource = (MessageSource<?>) target;
        } else {
            method = ReflectionUtils.findMethod(Supplier.class, "get");
            bean = target;
        }
    }
    if (messageSource == null) {
        MethodInvokingMessageSource methodInvokingMessageSource = new MethodInvokingMessageSource();
        methodInvokingMessageSource.setObject(bean);
        methodInvokingMessageSource.setMethod(method);
        String messageSourceBeanName = this.generateHandlerBeanName(beanName, method);
        this.beanFactory.registerSingleton(messageSourceBeanName, methodInvokingMessageSource);
        messageSource = (MessageSource<?>) this.beanFactory.initializeBean(methodInvokingMessageSource, messageSourceBeanName);
    }
    return messageSource;
}
Also used : MethodInvokingMessageSource(org.springframework.integration.endpoint.MethodInvokingMessageSource) MessageSource(org.springframework.integration.core.MessageSource) MethodInvokingMessageSource(org.springframework.integration.endpoint.MethodInvokingMessageSource) Supplier(java.util.function.Supplier) Bean(org.springframework.context.annotation.Bean)

Aggregations

MethodInvokingMessageSource (org.springframework.integration.endpoint.MethodInvokingMessageSource)8 Test (org.junit.Test)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 HashMap (java.util.HashMap)1 Supplier (java.util.function.Supplier)1 Bean (org.springframework.context.annotation.Bean)1 Expression (org.springframework.expression.Expression)1 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 MessageSource (org.springframework.integration.core.MessageSource)1