@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Queued Message Count") public int getQueuedMessageCount() { int count = 0; for (MessageChannelMetrics monitor : this.channels) { if (monitor instanceof QueueChannel) { count += ((QueueChannel) monitor).getQueueSize(); } } return count; }
@Test public void testStreaming() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(StreamingConfig.class); StandardConfig config = ctx.getBean(StandardConfig.class); ctx.getBean(StandardIntegrationFlow.class).stop(); assertThat(config.latch.await(10, TimeUnit.SECONDS)).isTrue(); List<Integer> sfCalls = config.sessionSources.stream().limit(17).collect(Collectors.toList()); // there's an extra getSession() with this adapter in listFiles assertThat(sfCalls).containsExactly(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 2, 2, 3); assertThat(ctx.getBean("files", QueueChannel.class).getQueueSize()).isEqualTo(3); ctx.close(); }
@Test public void testStandard() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(StandardConfig.class); StandardConfig config = ctx.getBean(StandardConfig.class); ctx.getBean(StandardIntegrationFlow.class).stop(); assertThat(config.latch.await(10, TimeUnit.SECONDS)).isTrue(); List<Integer> sfCalls = config.sessionSources.stream().limit(17).collect(Collectors.toList()); assertThat(sfCalls).containsExactly(1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1); File f1 = new File(tmpDir + File.separator + "standard" + File.separator + "f1"); assertThat(f1.exists()).isTrue(); File f2 = new File(tmpDir + File.separator + "standard" + File.separator + "f2"); assertThat(f2.exists()).isTrue(); File f3 = new File(tmpDir + File.separator + "standard" + File.separator + "f3"); assertThat(f3.exists()).isTrue(); assertThat(ctx.getBean("files", QueueChannel.class).getQueueSize()).isEqualTo(3); ctx.close(); }
@Test public void testFair() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FairConfig.class); StandardConfig config = ctx.getBean(StandardConfig.class); ctx.getBean(StandardIntegrationFlow.class).stop(); assertThat(config.latch.await(10, TimeUnit.SECONDS)).isTrue(); List<Integer> sfCalls = config.sessionSources.stream().limit(17).collect(Collectors.toList()); assertThat(sfCalls).containsExactly(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3); File f1 = new File(tmpDir + File.separator + "fair" + File.separator + "f1"); assertThat(f1.exists()).isTrue(); File f2 = new File(tmpDir + File.separator + "fair" + File.separator + "f2"); assertThat(f2.exists()).isTrue(); File f3 = new File(tmpDir + File.separator + "fair" + File.separator + "f3"); assertThat(f3.exists()).isTrue(); assertThat(ctx.getBean("files", QueueChannel.class).getQueueSize()).isEqualTo(3); ctx.close(); }
@Test public void testVariableLocalDir() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(VariableLocalConfig.class); StandardConfig config = ctx.getBean(StandardConfig.class); assertThat(config.latch.await(10, TimeUnit.SECONDS)).isTrue(); ctx.getBean(StandardIntegrationFlow.class).stop(); List<Integer> sfCalls = config.sessionSources.stream().limit(17).collect(Collectors.toList()); assertThat(sfCalls).containsExactly(1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1); File f1 = new File(tmpDir + File.separator + "variable" + File.separator + "foo" + File.separator + "f1"); assertThat(f1.exists()).isTrue(); File f2 = new File(tmpDir + File.separator + "variable" + File.separator + "baz" + File.separator + "f2"); assertThat(f2.exists()).isTrue(); File f3 = new File(tmpDir + File.separator + "variable" + File.separator + "fiz" + File.separator + "f3"); assertThat(f3.exists()).isTrue(); assertThat(ctx.getBean("files", QueueChannel.class).getQueueSize()).isEqualTo(3); ctx.close(); }
@Test public void testStringExpressionWithNestedNamespaceMap() throws Exception { Document doc = XmlTestUtil.getDocumentForString( "<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); StringBuffer buffer = new StringBuffer( "<si-xml:xpath-router id='router' input-channel='test-input'><si-xml:xpath-expression expression='/ns1:name/ns2:type'> "); buffer.append("<map><entry key='ns1' value='www.example.org' /> <entry key='ns2' value='www.example.org2'/></map>"); buffer.append("</si-xml:xpath-expression></si-xml:xpath-router>"); buildContext(buffer.toString()); inputChannel.send(docMessage); assertEquals("Wrong number of messages", 1, outputChannel.getQueueSize()); }
@Test public void testStringExpressionWithReferenceToNamespaceMap() throws Exception { Document doc = XmlTestUtil.getDocumentForString( "<ns1:name xmlns:ns1='www.example.org' xmlns:ns2='www.example.org2'><ns2:type>outputOne</ns2:type></ns1:name>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); StringBuffer buffer = new StringBuffer( "<si-xml:xpath-router id='router' input-channel='test-input'><si-xml:xpath-expression expression='/ns1:name/ns2:type' namespace-map='nsMap'/>"); buffer.append("</si-xml:xpath-router>"); buffer.append("<util:map id='nsMap'><entry key='ns1' value='www.example.org' /><entry key='ns2' value='www.example.org2' /></util:map>"); buildContext(buffer.toString()); inputChannel.send(docMessage); assertEquals("Wrong number of messages", 1, outputChannel.getQueueSize()); }
@Test public void testAggregationWithMessageGroupProcessorAndStrategies() { QueueChannel output = this.context.getBean("outputChannel", QueueChannel.class); output.purge(null); MessageChannel input = (MessageChannel) context.getBean("aggregatorWithCustomMGPReferenceInput"); List<Message<?>> outboundMessages = new ArrayList<>(); outboundMessages.add(createMessage("123", "id1", 3, 1, null)); outboundMessages.add(createMessage("789", "id1", 3, 3, null)); outboundMessages.add(createMessage("456", "id1", 3, 2, null)); outboundMessages.forEach(input::send); assertEquals(3, output.getQueueSize()); output.purge(null); }
@Test public void testAggregationWithMessageGroupProcessor() { QueueChannel output = this.context.getBean("outputChannel", QueueChannel.class); output.purge(null); MessageChannel input = (MessageChannel) context.getBean("aggregatorWithMGPReferenceInput"); List<Message<?>> outboundMessages = new ArrayList<>(); outboundMessages.add(createMessage("123", "id1", 3, 1, null)); outboundMessages.add(createMessage("789", "id1", 3, 3, null)); outboundMessages.add(createMessage("456", "id1", 3, 2, null)); outboundMessages.forEach(input::send); assertEquals(3, output.getQueueSize()); output.purge(null); assertFalse(TestUtils.getPropertyValue(context.getBean("aggregatorWithMGPReference.handler"), "releaseLockBeforeSend", Boolean.class)); }
@Test @SuppressWarnings("unchecked") public void testRecipientListRouterOrder() { this.recipientListOrderFlowInput.send(new GenericMessage<>(new AtomicReference<>(""))); Message<?> receive = this.recipientListOrderResult.receive(10000); assertNotNull(receive); AtomicReference<String> result = (AtomicReference<String>) receive.getPayload(); assertEquals("Hello World", result.get()); receive = this.recipientListOrderResult.receive(10000); assertNotNull(receive); result = (AtomicReference<String>) receive.getPayload(); assertEquals("Hello World", result.get()); assertEquals(1, this.alwaysRecipient.getQueueSize()); }
@Test public void testSimpleStringExpression() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<name>outputOne</name>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); buildContext("<si-xml:xpath-router id='router' input-channel='test-input'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>"); inputChannel.send(docMessage); assertEquals("Wrong number of messages", 1, outputChannel.getQueueSize()); }
@Test public void testNamespacedStringExpression() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); buildContext("<si-xml:xpath-router id='router' input-channel='test-input'><si-xml:xpath-expression expression='/ns2:name' ns-prefix='ns2' ns-uri='www.example.org' /></si-xml:xpath-router>"); inputChannel.send(docMessage); assertEquals("Wrong number of messages", 1, outputChannel.getQueueSize()); }
@Test public void testSetDefaultOutputChannel() throws Exception { StringBuffer contextBuffer = new StringBuffer("<si-xml:xpath-router id='router' default-output-channel='defaultOutput' input-channel='test-input'><si-xml:xpath-expression expression='/name'/></si-xml:xpath-router>"); EventDrivenConsumer consumer = buildContext(contextBuffer.toString()); DirectFieldAccessor accessor = new DirectFieldAccessor(consumer); Object handler = accessor.getPropertyValue("handler"); accessor = new DirectFieldAccessor(handler); Object defaultOutputChannelValue = accessor.getPropertyValue("defaultOutputChannel"); assertEquals("Default output channel not correctly set ", defaultOutput, defaultOutputChannelValue); inputChannel.send(MessageBuilder.withPayload("<unrelated/>").build()); assertEquals("Wrong count of messages on default output channel", 1, defaultOutput.getQueueSize()); }
@Test public void testCounts() { Message<?> message = new GenericMessage<String>("foo"); int count = 2000; for (int i = 0; i < count; i++) { input.send(message); } assertEquals(count, this.output.getQueueSize()); assertEquals(count, this.output.getSendCount()); assertEquals(Long.valueOf(count / 1000).longValue(), this.output.getSendDuration().getCountLong()); assertEquals(count, this.handler.getHandleCount()); assertEquals(Long.valueOf(count / 1000).longValue(), this.handler.getDuration().getCountLong()); }
@Test public void testSendAndActivateWithRollback() throws Exception { Service.fail = true; input.send(new GenericMessage<>("foo")); Service.await(10000); assertThat(Service.messages.size(), Matchers.greaterThanOrEqualTo(1)); // After a rollback in the poller the message is still waiting to be delivered // but unless we use a transaction here there is a chance that the queue will // appear empty.... new TransactionTemplate(transactionManager).execute(status -> { synchronized (storeLock) { assertEquals(1, input.getQueueSize()); assertNotNull(input.receive(100L)); } return null; }); }
@Test public void splitToIterator_allMessagesSent() { int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); splitter.handleMessage(this.message); assertThat(replyChannel.getQueueSize(), is(messageQuantity)); }
@Test public void splitToIterable_allMessagesSent() { int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IterableTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); splitter.handleMessage(this.message); assertThat(replyChannel.getQueueSize(), is(messageQuantity)); }
@Test public void testSimpleStringExpressionWithCreateDocuments() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); TestXmlApplicationContext ctx = TestXmlApplicationContextHelper .getTestAppContext(channelDefinitions + "<si-xml:xpath-splitter id='splitter' input-channel='test-input' output-channel='test-output' create-documents='true'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>"); EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter"); consumer.start(); ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); inputChannel.send(docMessage); assertEquals("Wrong number of split messages ", 2, outputChannel.getQueueSize()); assertTrue("Splitter failed to create documents ", ((Message<?>) outputChannel.receive(1000)).getPayload() instanceof Document); assertTrue("Splitter failed to create documents ", ((Message<?>) outputChannel.receive(1000)).getPayload() instanceof Document); }
@Test public void testSimpleStringExpression() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); TestXmlApplicationContext ctx = TestXmlApplicationContextHelper .getTestAppContext(channelDefinitions + "<si-xml:xpath-splitter id='splitter' " + "order='2' send-timeout='123' auto-startup='false' phase='-1' " + "input-channel='test-input' output-channel='test-output'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>"); EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter"); assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order")); assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout")); assertEquals(-1, TestUtils.getPropertyValue(consumer, "phase")); assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class)); consumer.start(); ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); inputChannel.send(docMessage); assertEquals("Wrong number of split messages ", 2, outputChannel.getQueueSize()); }
@Test @Ignore("Until 5.2 with new 'owner' feature on groups") public void testDontReapMessageOfOtherHandler() { MessageGroupStore groupStore = new SimpleMessageStore(); AggregatingMessageHandler handler1 = new AggregatingMessageHandler(group -> group, groupStore); AggregatingMessageHandler handler2 = new AggregatingMessageHandler(group -> group, groupStore); QueueChannel handler1DiscardChannel = new QueueChannel(); handler1.setDiscardChannel(handler1DiscardChannel); QueueChannel handler2DiscardChannel = new QueueChannel(); handler2.setDiscardChannel(handler2DiscardChannel); handler1.setReleaseStrategy(group -> false); handler2.setReleaseStrategy(group -> false); handler1.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build()); handler1.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build()); handler2.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("bar").build()); groupStore.expireMessageGroups(0); assertEquals(2, handler1DiscardChannel.getQueueSize()); assertEquals(1, handler2DiscardChannel.getQueueSize()); }