/** * 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 testInvalidMethodWithNoReturnValue() { MethodInvokingMessageSource source = new MethodInvokingMessageSource(); source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("invalidMethodWithNoReturnValue"); source.receive(); }
@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()); }
@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(); }
@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(); }
@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")); }
@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); }
source.setMethodName("isRunning");
/** * 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); }
/** * 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); }
/** * 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; }