protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel, List<Annotation> annotations) { AbstractEndpoint endpoint; if (inputChannel instanceof PollableChannel) { PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler); configurePollingEndpoint(pollingConsumer, annotations); endpoint = pollingConsumer; } else { Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class); Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based " + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable)."); if (inputChannel instanceof Publisher) { endpoint = new ReactiveStreamsConsumer(inputChannel, handler); } else { endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler); } } return endpoint; }
@Bean public EventDrivenConsumer foreignMessageHandlerNoStats() { return new EventDrivenConsumer(three(), new BareHandler()); }
@Test public void splitWithMassiveReplyMessages_allMessagesSent() { final int messageQuantity = 100000; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); DirectChannel replyChannel = new DirectChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); final AtomicInteger receivedMessageCounter = new AtomicInteger(0); new EventDrivenConsumer(replyChannel, message -> { assertThat("Failure with msg: " + message, message.getPayload(), is(notNullValue())); receivedMessageCounter.incrementAndGet(); }).start(); splitter.handleMessage(this.message); assertThat(receivedMessageCounter.get(), is(messageQuantity)); }
@Test public void splitToIterator_allMessagesContainSequenceNumber() { final int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); DirectChannel replyChannel = new DirectChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); new EventDrivenConsumer(replyChannel, message -> assertThat("Failure with msg: " + message, message.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Integer.class), is(Integer.valueOf((String) message.getPayload())))).start(); splitter.handleMessage(this.message); }
@Test public void testGatewayWithException() throws Exception { this.whichTest = 0; GatewayProxyFactoryBean gpfb = new GatewayProxyFactoryBean(Foo.class); gpfb.setBeanFactory(mock(BeanFactory.class)); DirectChannel input = new DirectChannel(); gpfb.setDefaultRequestChannel(input); gpfb.setDefaultReplyTimeout(10000L); gpfb.afterPropertiesSet(); Foo foo = (Foo) gpfb.getObject(); this.handler.setOutputChannel(null); EventDrivenConsumer consumer = new EventDrivenConsumer(input, this.handler); consumer.afterPropertiesSet(); consumer.start(); this.latch.countDown(); try { foo.exchange("foo"); } catch (MessagingException e) { assertThat(e.getClass().getSimpleName(), equalTo("RuntimeException")); assertThat(e.getMessage(), equalTo("foo")); } }
@Test(expected = MessagingException.class) public void exceptionThrownFromRegisteredEndpoint() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override public Object handleRequestMessage(Message<?> message) { throw new RuntimeException("intentional test failure"); } }; handler.setOutputChannel(targetChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(sourceChannel, handler); this.context.registerEndpoint("testEndpoint", endpoint); this.context.refresh(); this.sourceChannel.send(new GenericMessage<>("foo")); }
@Test public void filterAcceptsWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> true); filter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertEquals(message.getPayload(), reply.getPayload()); }
@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()); }
@Test public void testGateway() throws Exception { this.whichTest = 0; GatewayProxyFactoryBean gpfb = new GatewayProxyFactoryBean(Foo.class); gpfb.setBeanFactory(mock(BeanFactory.class)); DirectChannel input = new DirectChannel(); gpfb.setDefaultRequestChannel(input); gpfb.setDefaultReplyTimeout(10000L); gpfb.afterPropertiesSet(); Foo foo = (Foo) gpfb.getObject(); this.handler.setOutputChannel(null); EventDrivenConsumer consumer = new EventDrivenConsumer(input, this.handler); consumer.afterPropertiesSet(); consumer.start(); this.latch.countDown(); String result = foo.exchange("foo"); assertEquals("reply", result); }
@Test(expected = TestException.class) public void testCheckedExceptionRethrownAsIs() throws Exception { GatewayProxyFactoryBean proxyFactory = new GatewayProxyFactoryBean(); DirectChannel channel = new DirectChannel(); EventDrivenConsumer consumer = new EventDrivenConsumer(channel, new MessageHandler() { @Override public void handleMessage(Message<?> message) { Method method = ReflectionUtils.findMethod( GatewayProxyFactoryBeanTests.class, "throwTestException"); ReflectionUtils.invokeMethod(method, this); } }); consumer.start(); proxyFactory.setDefaultRequestChannel(channel); proxyFactory.setServiceInterface(TestExceptionThrowingInterface.class); proxyFactory.setBeanName("testGateway"); proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestExceptionThrowingInterface proxy = (TestExceptionThrowingInterface) proxyFactory.getObject(); proxy.throwCheckedException("test"); }
@Test(expected = MessageRejectedException.class) public void filterThrowsExceptionWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); filter.setThrowExceptionOnRejection(true); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); }
@Test public void correlationIdCopiedFromMessageId() { Message<String> message = MessageBuilder.withPayload("test").build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); DefaultMessageSplitter splitter = new DefaultMessageSplitter(); splitter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, splitter); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void jdkProxy() { DirectChannel input = new DirectChannel(); QueueChannel output = new QueueChannel(); GreetingService testBean = new GreetingBean(); ProxyFactory proxyFactory = new ProxyFactory(testBean); proxyFactory.setProxyTargetClass(true); testBean = (GreetingService) proxyFactory.getProxy(); MethodInvokingMessageGroupProcessor aggregator = new MethodInvokingMessageGroupProcessor(testBean); AggregatingMessageHandler handler = new AggregatingMessageHandler(aggregator); handler.setReleaseStrategy(new MessageCountReleaseStrategy()); handler.setOutputChannel(output); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(input, handler); endpoint.start(); Message<?> message = MessageBuilder.withPayload("proxy").setCorrelationId("abc").build(); input.send(message); assertEquals("hello proxy", output.receive(0).getPayload()); }
@Test public void filterRejectsSilentlyWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); assertNull(outputChannel.receive(0)); }
@Test public void cglibProxy() { DirectChannel input = new DirectChannel(); QueueChannel output = new QueueChannel(); GreetingService testBean = new GreetingBean(); ProxyFactory proxyFactory = new ProxyFactory(testBean); proxyFactory.setProxyTargetClass(true); testBean = (GreetingService) proxyFactory.getProxy(); MethodInvokingMessageGroupProcessor aggregator = new MethodInvokingMessageGroupProcessor(testBean); AggregatingMessageHandler handler = new AggregatingMessageHandler(aggregator); handler.setReleaseStrategy(new MessageCountReleaseStrategy()); handler.setOutputChannel(output); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(input, handler); endpoint.start(); Message<?> message = MessageBuilder.withPayload("proxy").setCorrelationId("abc").build(); input.send(message); assertEquals("hello proxy", output.receive(0).getPayload()); }
@Test public void testCorrelationNotCopiedFromRequestMessgeIdIfAlreadySetByHandler() throws Exception { Message<?> message = new GenericMessage<String>("test"); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "createMessage"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals("456-XYZ", new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationIdPassedIfAvailable() { Object correlationId = "123-ABC"; Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId(correlationId).build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "upperCase"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(correlationId, new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationNotPassedFromRequestHeaderIfAlreadySetByHandler() { Object correlationId = "123-ABC"; Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId(correlationId).build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "createMessage"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals("456-XYZ", new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationIdCopiedFromMessageCorrelationIdIfAvailable() { Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId("correlationId").build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "upperCase"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(new IntegrationMessageHeaderAccessor(message).getCorrelationId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void filterDiscardsMessage() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); QueueChannel discardChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); filter.setDiscardChannel(discardChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); Message<?> reply = discardChannel.receive(0); assertNotNull(reply); assertEquals(message, reply); assertNull(outputChannel.receive(0)); }