@Test(expected = MessagingException.class) public void testNoMatchingMethodName() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("noMatchingMethod"); source.receive(); }
@Override protected Object doReceive() { try { if (!this.initialized) { this.afterPropertiesSet(); } return ReflectionUtils.invokeMethod(this.method, this.object); } catch (Exception e) { throw new MessagingException("Failed to invoke method", e); } }
MethodInvokingMessageSource methodInvokingMessageSource = new MethodInvokingMessageSource(); methodInvokingMessageSource.setObject(bean); methodInvokingMessageSource.setMethod(method); String messageSourceBeanName = this.generateHandlerBeanName(beanName, method); this.beanFactory.registerSingleton(messageSourceBeanName, methodInvokingMessageSource);
/** * Populate the provided {@link MethodInvokingMessageSource} for the method of the provided service. * The {@link org.springframework.integration.dsl.IntegrationFlow} {@code startMessageSource}. * @param service the service to use. * @param methodName the method to invoke. * @param endpointConfigurer the {@link Consumer} to provide more options for the * {@link org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean}. * @return new {@link IntegrationFlowBuilder}. * @since 1.1 * @see MethodInvokingMessageSource */ public static IntegrationFlowBuilder from(Object service, String methodName, Consumer<SourcePollingChannelAdapterSpec> endpointConfigurer) { Assert.notNull(service, "'service' must not be null"); Assert.hasText(methodName, "'methodName' must not be empty"); MethodInvokingMessageSource messageSource = new MethodInvokingMessageSource(); messageSource.setObject(service); messageSource.setMethodName(methodName); return from(messageSource, endpointConfigurer); }
@Bean @InboundChannelAdapter(value = "flow1.input", autoStartup = "false", poller = @Poller(fixedRate = "100")) public MessageSource<?> integerMessageSource() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setObject(new AtomicInteger()); source.setMethodName("getAndIncrement"); return source; }
@Test(expected = MessagingException.class) public void testInvalidMethodWithArg() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("invalidMethodWithArg"); source.receive(); }
MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setObject(new Lifecycle() { source.setMethodName("isRunning");
MethodInvokingMessageSource methodInvokingMessageSource = new MethodInvokingMessageSource(); methodInvokingMessageSource.setObject(bean); methodInvokingMessageSource.setMethod(method); String messageSourceBeanName = this.generateHandlerBeanName(beanName, method); this.beanFactory.registerSingleton(messageSourceBeanName, methodInvokingMessageSource);
@Override protected Object doReceive() { try { if (!this.initialized) { this.afterPropertiesSet(); } return ReflectionUtils.invokeMethod(this.method, this.object); } catch (Exception e) { throw new MessagingException("Failed to invoke method", e); } }
@Test(expected = MessagingException.class) public void testInvalidMethodWithNoReturnValue() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("invalidMethodWithNoReturnValue"); source.receive(); }
/** * Populate the provided {@link MethodInvokingMessageSource} for the method of the provided service. * The {@link org.springframework.integration.dsl.IntegrationFlow} {@code startMessageSource}. * @param service the service to use. * @param methodName the method to invoke. * @param endpointConfigurer the {@link Consumer} to provide more options for the * {@link org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean}. * @return new {@link IntegrationFlowBuilder}. * @since 1.1 * @see MethodInvokingMessageSource */ public static IntegrationFlowBuilder from(Object service, String methodName, Consumer<SourcePollingChannelAdapterSpec> endpointConfigurer) { Assert.notNull(service, "'service' must not be null"); Assert.hasText(methodName, "'methodName' must not be empty"); MethodInvokingMessageSource messageSource = new MethodInvokingMessageSource(); messageSource.setObject(service); messageSource.setMethodName(methodName); return from(messageSource, endpointConfigurer); }
@Test public void testNullReturningMethodReturnsNullMessage() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("nullReturningMethod"); Message<?> message = source.receive(); assertNull(message); }
/** * Populate the provided {@link MethodInvokingMessageSource} for the method of the provided service. * The {@link org.springframework.integration.dsl.IntegrationFlow} {@code startMessageSource}. * @param service the service to use. * @param methodName the method to invoke. * @param endpointConfigurer the {@link Consumer} to provide more options for the * {@link org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean}. * @return new {@link IntegrationFlowBuilder}. * @since 1.1 * @see MethodInvokingMessageSource */ public static IntegrationFlowBuilder from(Object service, String methodName, Consumer<SourcePollingChannelAdapterSpec> endpointConfigurer) { Assert.notNull(service, "'service' must not be null"); Assert.hasText(methodName, "'methodName' must not be empty"); MethodInvokingMessageSource messageSource = new MethodInvokingMessageSource(); messageSource.setObject(service); messageSource.setMethodName(methodName); return from(messageSource, endpointConfigurer); }
@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()); }
/** * Populate the provided {@link MethodInvokingMessageSource} for the method of the provided service. * The {@link org.springframework.integration.dsl.IntegrationFlow} {@code startMessageSource}. * @param service the service to use. * @param methodName the method to invoke. * @param endpointConfigurer the {@link Consumer} to provide more options for the * {@link org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean}. * @return new {@link IntegrationFlowBuilder}. * @since 1.1 * @see MethodInvokingMessageSource */ public static IntegrationFlowBuilder from(Object service, String methodName, Consumer<SourcePollingChannelAdapterSpec> endpointConfigurer) { Assert.notNull(service, "'service' must not be null"); Assert.hasText(methodName, "'methodName' must not be empty"); MethodInvokingMessageSource messageSource = new MethodInvokingMessageSource(); messageSource.setObject(service); messageSource.setMethodName(methodName); return from(messageSource, endpointConfigurer); }
@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")); }
@Bean @InboundChannelAdapter(value = "flow1.input", autoStartup = "false", poller = @Poller(fixedRate = "100")) public MessageSource<?> integerMessageSource() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setObject(new AtomicInteger()); source.setMethodName("getAndIncrement"); return source; }