@Test(expected = ReplyRequiredException.class) public void noReplyMessageWithRequiresReply() { QueueChannel channel = new QueueChannel(1); ServiceActivatingHandler endpoint = new ServiceActivatingHandler(new TestNullReplyBean(), "handle"); endpoint.setRequiresReply(true); endpoint.setOutputChannel(channel); endpoint.setBeanFactory(mock(BeanFactory.class)); endpoint.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("foo").build(); endpoint.handleMessage(message); }
@Override protected MessageHandler createHandler() { ExpressionCommandMessageProcessor processor = new ExpressionCommandMessageProcessor(methodFilter, this.getBeanFactory()); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); if (this.sendTimeout != null) { handler.setSendTimeout(this.sendTimeout); } return handler; }
@Override protected void doInit() { if (this.processor instanceof AbstractMessageProcessor) { ConversionService conversionService = getConversionService(); if (conversionService != null) { ((AbstractMessageProcessor<?>) this.processor).setConversionService(conversionService); } } if (this.processor instanceof BeanFactoryAware && this.getBeanFactory() != null) { ((BeanFactoryAware) this.processor).setBeanFactory(this.getBeanFactory()); } }
private static ServiceActivatingHandler createConsumer(Object object) { ServiceActivatingHandler handler = new ServiceActivatingHandler(object); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); return handler; }
/** * Populate a {@link ServiceActivatingHandler} instance to perform {@link MessageTriggerAction} * and endpoint options from {@link GenericEndpointSpec}. * @param triggerAction the {@link MessageTriggerAction}. * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options. * @return the current {@link IntegrationFlowDefinition}. */ public B trigger(MessageTriggerAction triggerAction, Consumer<GenericEndpointSpec<ServiceActivatingHandler>> endpointConfigurer) { return handle(new ServiceActivatingHandler(triggerAction, "trigger"), endpointConfigurer); }
processor.setUseSpelInvoker(true); processor.setBeanFactory(beanFactory); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); QueueChannel replyChannel = new QueueChannel(); handler.setOutputChannel(replyChannel); handler.handleMessage(new GenericMessage<>(foos)); Message<?> message = replyChannel.receive(0); assertNotNull(message);
@Test public void sendAndReceiveForRegisteredEndpoint() { ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "handle"); serviceActivator.setOutputChannel(this.targetChannel); context.registerBean("testServiceActivator", serviceActivator); EventDrivenConsumer endpoint = new EventDrivenConsumer(this.sourceChannel, serviceActivator); context.registerEndpoint("testEndpoint", endpoint); context.refresh(); this.sourceChannel.send(new GenericMessage<>("foo")); Message<?> response = this.targetChannel.receive(); assertEquals("foo!", response.getPayload()); }
@Override protected MessageHandler createExpressionEvaluatingHandler(Expression expression) { ExpressionEvaluatingMessageProcessor<Object> processor = new ExpressionEvaluatingMessageProcessor<Object>(expression); processor.setBeanFactory(this.getBeanFactory()); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); handler.setPrimaryExpression(expression); return this.configureHandler(handler); }
@Test public void testRequestReplyExchanger() { RequestReplyExchanger testBean = request -> request; final Message<?> test = new GenericMessage<Object>("foo"); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean) { @Override protected Object handleRequestMessage(Message<?> message) { Object o = super.handleRequestMessage(message); assertSame(test, o); return null; } }; serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); serviceActivator.handleMessage(test); }
private ServiceActivatingHandler configureHandler(ServiceActivatingHandler handler) { if (this.sendTimeout != null) { handler.setSendTimeout(this.sendTimeout); } return handler; }
@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); }
@Override public String toString() { return "ServiceActivator for [" + this.processor + "]" + (this.getComponentName() == null ? "" : " (" + this.getComponentName() + ")"); }
@Override protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) { MessageHandler handler = null; handler = createDirectHandlerIfPossible(targetObject, targetMethodName); if (handler == null) { handler = configureHandler( StringUtils.hasText(targetMethodName) ? new ServiceActivatingHandler(targetObject, targetMethodName) : new ServiceActivatingHandler(targetObject)); } return handler; }
private ServiceActivatingHandler getHandler(String methodName, Class<?>... types) throws Exception { ServiceActivatingHandler serviceActivatingHandler = new ServiceActivatingHandler(bean, TestBean.class.getMethod(methodName, types)); serviceActivatingHandler.setBeanFactory(applicationContext); serviceActivatingHandler.afterPropertiesSet(); return serviceActivatingHandler; }
@Test public void testCollectionIsConverted() { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new Converter<Foo, Bar>() { // Must be explicit type with generics @Override public Bar convert(Foo source) { return new Bar(); } }); BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter(conversionService); beanFactory.setConversionService(conversionService); typeConverter.setBeanFactory(beanFactory); Service service = new Service(); MethodInvokingMessageProcessor<Service> processor = new MethodInvokingMessageProcessor<>(service, "handle"); processor.setConversionService(conversionService); processor.setUseSpelInvoker(true); processor.setBeanFactory(beanFactory); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); QueueChannel replyChannel = new QueueChannel(); handler.setOutputChannel(replyChannel); handler.handleMessage(new GenericMessage<Collection<Foo>>(Collections.singletonList(new Foo()))); Message<?> message = replyChannel.receive(10000); assertNotNull(message); assertEquals("baz", message.getPayload()); }
@Override protected MessageHandler createHandler(Object bean, Method method, ServiceActivator annotation) { ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(bean, method); String outputChannelName = annotation.outputChannel(); if (StringUtils.hasText(outputChannelName)) { serviceActivator.setOutputChannel(this.channelResolver.resolveChannelName(outputChannelName)); } return serviceActivator; }
@Override protected MessageHandler createExpressionEvaluatingHandler(Expression expression) { ExpressionEvaluatingMessageProcessor<Object> processor = new ExpressionEvaluatingMessageProcessor<Object>(expression); processor.setBeanFactory(this.getBeanFactory()); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); handler.setPrimaryExpression(expression); return this.configureHandler(handler); }
private ServiceActivatingHandler configureHandler(ServiceActivatingHandler handler) { if (this.sendTimeout != null) { handler.setSendTimeout(this.sendTimeout); } return handler; }
@Override public String toString() { return "ServiceActivator for [" + this.processor + "]" + (this.getComponentName() == null ? "" : " (" + this.getComponentName() + ")"); }
@Test public void singlePublicMethodMatches() { SinglePublicMethodTestBean testBean = new SinglePublicMethodTestBean(); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); QueueChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); serviceActivator.handleMessage(new GenericMessage<>("foo")); Message<?> result = outputChannel.receive(0); assertEquals("FOO", result.getPayload()); }