List of usage examples for org.springframework.messaging Message getPayload
T getPayload();
From source file:org.springframework.integration.config.xml.GatewayParserTests.java
private void startResponder(final PollableChannel requestChannel, final MessageChannel replyChannel) { Executors.newSingleThreadExecutor().execute(() -> { Message<?> request = requestChannel.receive(60000); assertNotNull("Request not received", request); Message<?> reply = MessageBuilder.fromMessage(request).setCorrelationId(request.getHeaders().getId()) .build();/*from www .jav a2 s .c om*/ Object payload = null; if (request.getPayload().equals("futureSync")) { payload = new AsyncResult<Message<?>>(reply); } else if (request.getPayload().equals("flowCompletable")) { payload = CompletableFuture.<String>completedFuture("SYNC_COMPLETABLE"); } else if (request.getPayload().equals("flowCustomCompletable")) { MyCompletableFuture myCompletableFuture1 = new MyCompletableFuture(); myCompletableFuture1.complete("SYNC_CUSTOM_COMPLETABLE"); payload = myCompletableFuture1; } else if (request.getPayload().equals("flowCompletableM")) { payload = CompletableFuture.<Message<?>>completedFuture(reply); } else if (request.getPayload().equals("flowCustomCompletableM")) { MyCompletableMessageFuture myCompletableFuture2 = new MyCompletableMessageFuture(); myCompletableFuture2.complete(reply); payload = myCompletableFuture2; } if (payload != null) { reply = MessageBuilder.withPayload(payload).copyHeaders(reply.getHeaders()).build(); } replyChannel.send(reply); }); }
From source file:org.springframework.integration.configuration.EnableIntegrationTests.java
@Test public void testAnnotatedServiceActivator() throws Exception { this.serviceActivatorEndpoint.setReceiveTimeout(10000); this.serviceActivatorEndpoint.start(); assertTrue(this.inputReceiveLatch.await(10, TimeUnit.SECONDS)); assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll")); Trigger trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(100L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); assertTrue(this.annotationTestService.isRunning()); Log logger = spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", Log.class)); when(logger.isDebugEnabled()).thenReturn(true); final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1); doAnswer(invocation -> {//w w w . ja v a2 s . com pollerInterruptedLatch.countDown(); invocation.callRealMethod(); return null; }).when(logger).debug("Received no Message during the poll, returning 'false'"); new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger); this.serviceActivatorEndpoint.stop(); assertFalse(this.annotationTestService.isRunning()); // wait until the service activator's poller is interrupted. assertTrue(pollerInterruptedLatch.await(10, TimeUnit.SECONDS)); this.serviceActivatorEndpoint.start(); assertTrue(this.annotationTestService.isRunning()); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(100L, TestUtils.getPropertyValue(trigger, "period")); assertTrue(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(CronTrigger.class)); assertEquals("0 5 7 * * *", TestUtils.getPropertyValue(trigger, "sequenceGenerator.expression")); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(11L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(1000L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); assertSame(this.myTrigger, trigger); trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(10L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); this.input.send(MessageBuilder.withPayload("Foo").build()); Message<?> interceptedMessage = this.wireTapChannel.receive(10000); assertNotNull(interceptedMessage); assertEquals("Foo", interceptedMessage.getPayload()); Message<?> receive = this.output.receive(10000); assertNotNull(receive); assertEquals("FOO", receive.getPayload()); receive = this.wireTapFromOutput.receive(10000); assertNotNull(receive); assertEquals("FOO", receive.getPayload()); MessageHistory messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class); assertNotNull(messageHistory); String messageHistoryString = messageHistory.toString(); assertThat(messageHistoryString, Matchers.containsString("input")); assertThat(messageHistoryString, Matchers.containsString("annotationTestService.handle.serviceActivator.handler")); assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output"))); receive = this.publishedChannel.receive(10000); assertNotNull(receive); assertEquals("foo", receive.getPayload()); messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class); assertNotNull(messageHistory); messageHistoryString = messageHistory.toString(); assertThat(messageHistoryString, Matchers.not(Matchers.containsString("input"))); assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output"))); assertThat(messageHistoryString, Matchers.containsString("publishedChannel")); assertNull(this.wireTapChannel.receive(0)); assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThan(0)); assertThat(this.fbInterceptorCounter.get(), Matchers.greaterThan(0)); assertTrue(this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source")); Object messageSource = this.context.getBean("annotationTestService.count.inboundChannelAdapter.source"); assertThat(messageSource, Matchers.instanceOf(MethodInvokingMessageSource.class)); assertNull(this.counterChannel.receive(10)); SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter", SmartLifecycle.class); assertFalse(countSA.isAutoStartup()); assertEquals(23, countSA.getPhase()); countSA.start(); for (int i = 0; i < 10; i++) { Message<?> message = this.counterChannel.receive(1000); assertNotNull(message); assertEquals(i + 1, message.getPayload()); } Message<?> message = this.fooChannel.receive(1000); assertNotNull(message); assertEquals("foo", message.getPayload()); message = this.fooChannel.receive(1000); assertNotNull(message); assertEquals("foo", message.getPayload()); assertNull(this.fooChannel.receive(10)); message = this.messageChannel.receive(1000); assertNotNull(message); assertEquals("bar", message.getPayload()); assertTrue(message.getHeaders().containsKey("foo")); assertEquals("FOO", message.getHeaders().get("foo")); MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel); assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class)); this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.start()")); assertTrue(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class)); this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.stop()")); assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class)); }
From source file:org.springframework.integration.configuration.EnableIntegrationTests.java
@Test public void testSourcePollingChannelAdapterOutputChannelLateBinding() { QueueChannel testChannel = new QueueChannel(); ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) this.context .getAutowireCapableBeanFactory(); beanFactory.registerSingleton("lateBindingChannel", testChannel); beanFactory.initializeBean(testChannel, "lateBindingChannel"); this.autoCreatedChannelMessageSourceAdapter.start(); Message<?> receive = testChannel.receive(10000); assertNotNull(receive);//from w ww. j a v a2 s . c om assertEquals("bar", receive.getPayload()); this.autoCreatedChannelMessageSourceAdapter.stop(); }
From source file:org.springframework.integration.dsl.test.ftp.FtpTests.java
@Test public void testFtpInboundFlow() { this.controlBus.send("@ftpInboundAdapter.start()"); Message<?> message = this.ftpInboundResultChannel.receive(1000); assertNotNull(message);/*w w w. ja v a 2 s . c o m*/ Object payload = message.getPayload(); assertThat(payload, instanceOf(File.class)); File file = (File) payload; assertThat(file.getName(), isOneOf("FTPSOURCE1.TXT.a", "FTPSOURCE2.TXT.a")); assertThat(file.getAbsolutePath(), containsString("ftpTest")); message = this.ftpInboundResultChannel.receive(1000); assertNotNull(message); file = (File) message.getPayload(); assertThat(file.getName(), isOneOf("FTPSOURCE1.TXT.a", "FTPSOURCE2.TXT.a")); assertThat(file.getAbsolutePath(), containsString("ftpTest")); this.controlBus.send("@ftpInboundAdapter.stop()"); }
From source file:org.springframework.integration.dsl.test.ftp.FtpTests.java
@Test @SuppressWarnings("unchecked") public void testFtpMgetFlow() { String dir = "ftpSource/"; this.ftpMgetInputChannel.send(new GenericMessage<>(dir + "*")); Message<?> result = this.remoteFileOutputChannel.receive(1000); assertNotNull(result);/*from w ww.jav a 2 s. c o m*/ List<File> localFiles = (List<File>) result.getPayload(); // should have filtered ftpSource2.txt assertEquals(2, localFiles.size()); for (File file : localFiles) { assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir)); } assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir + "subFtpSource")); }
From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java
@Test public void testPollingFlow() { this.controlBus.send("@integerEndpoint.start()"); assertThat(this.beanFactory.getBean("integerChannel"), instanceOf(FixedSubscriberChannel.class)); for (int i = 0; i < 5; i++) { Message<?> message = this.outputChannel.receive(20000); assertNotNull(message);//from w w w . j ava2 s . c o m assertEquals("" + i, message.getPayload()); } this.controlBus.send("@integerEndpoint.stop()"); assertTrue(((ChannelInterceptorAware) this.outputChannel).getChannelInterceptors() .contains(this.testChannelInterceptor)); assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThanOrEqualTo(5)); }
From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java
@Test public void testDirectFlow() { assertTrue(this.beanFactory.containsBean("filter")); assertTrue(this.beanFactory.containsBean("filter.handler")); QueueChannel replyChannel = new QueueChannel(); Message<String> message = MessageBuilder.withPayload("100").setReplyChannel(replyChannel).build(); try {//from w w w . j av a2 s. co m this.inputChannel.send(message); fail("Expected MessageDispatchingException"); } catch (Exception e) { assertThat(e, instanceOf(MessageDeliveryException.class)); assertThat(e.getCause(), instanceOf(MessageDispatchingException.class)); assertThat(e.getMessage(), containsString("Dispatcher has no subscribers")); } this.controlBus.send("@payloadSerializingTransformer.start()"); final AtomicBoolean used = new AtomicBoolean(); this.foo.subscribe(m -> used.set(true)); this.inputChannel.send(message); Message<?> reply = replyChannel.receive(5000); assertNotNull(reply); assertEquals(200, reply.getPayload()); Message<?> successMessage = this.successChannel.receive(5000); assertNotNull(successMessage); assertEquals(100, successMessage.getPayload()); assertTrue(used.get()); }
From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java
@Test public void testBridge() { GenericMessage<String> message = new GenericMessage<>("test"); this.bridgeFlowInput.send(message); Message<?> reply = this.bridgeFlowOutput.receive(5000); assertNotNull(reply);/*from ww w . ja v a 2 s . co m*/ assertEquals("test", reply.getPayload()); assertTrue(this.beanFactory.containsBean("bridgeFlow2.channel#0")); assertThat(this.beanFactory.getBean("bridgeFlow2.channel#0"), instanceOf(FixedSubscriberChannel.class)); try { this.bridgeFlow2Input.send(message); fail("Expected MessageDispatchingException"); } catch (Exception e) { assertThat(e, instanceOf(MessageDeliveryException.class)); assertThat(e.getCause(), instanceOf(MessageDispatchingException.class)); assertThat(e.getMessage(), containsString("Dispatcher has no subscribers")); } this.controlBus.send("@bridge.start()"); this.bridgeFlow2Input.send(message); reply = this.bridgeFlow2Output.receive(5000); assertNotNull(reply); assertEquals("test", reply.getPayload()); assertTrue(this.delayedAdvice.getInvoked()); }
From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java
@Test public void testMethodInvokingMessageHandler() { QueueChannel replyChannel = new QueueChannel(); Message<?> message = MessageBuilder.withPayload("world") .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build(); this.methodInvokingInput.send(message); Message<?> receive = replyChannel.receive(5000); assertNotNull(receive);/*from w w w . ja va 2 s . co m*/ assertEquals("Hello World and world", receive.getPayload()); }
From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java
@Test public void testLambdas() { QueueChannel replyChannel = new QueueChannel(); Message<?> message = MessageBuilder.withPayload("World") .setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel).build(); this.lamdasInput.send(message); Message<?> receive = replyChannel.receive(5000); assertNotNull(receive);//from w w w. j a v a2 s.c o m assertEquals("Hello World", receive.getPayload()); message = MessageBuilder.withPayload("Spring").setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel) .build(); this.lamdasInput.send(message); assertNull(replyChannel.receive(10)); }