protected void setupMessagingTemplate() { PollableChannel pollableChannel = this.context.getBean("output", PollableChannel.class); this.messagingTemplate = new MessagingTemplate(); this.messagingTemplate.setDefaultDestination(pollableChannel); this.messagingTemplate.setReceiveTimeout(10000); }
protected void setupMessagingTemplate() { PollableChannel pollableChannel = this.appCtx.getBean("target", PollableChannel.class); this.messagingTemplate = new MessagingTemplate(); this.messagingTemplate.setDefaultDestination(pollableChannel); this.messagingTemplate.setReceiveTimeout(10000); }
private Object sendAndReceive(MessageChannel channel, Object payload) { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(channel); return template.convertSendAndReceive(payload, Object.class); }
private MessageHandler unwrapObjectMessageAndEchoHandler() { return message -> { assertThat(message.getPayload(), instanceOf(javax.jms.ObjectMessage.class)); MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel()); Message<?> origMessage = null; try { origMessage = (Message<?>) ((javax.jms.ObjectMessage) message.getPayload()).getObject(); } catch (JMSException e) { fail("failed to deserialize message"); } template.send(origMessage); }; }
private MessageHandler echoInboundStringHandler() { return message -> { assertTrue(message.getPayload() instanceof String); MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel()); template.send(message); }; }
private MessageHandler unwrapTextMessageAndEchoHandler() { return message -> { assertThat(message.getPayload(), instanceOf(javax.jms.TextMessage.class)); MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel()); String payload = null; try { payload = ((javax.jms.TextMessage) message.getPayload()).getText(); } catch (JMSException e) { fail("failed to deserialize message"); } template.send(new GenericMessage<String>(payload)); }; }
@Test public void literalValue() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(literalValueInput); Message<?> result = template.sendAndReceive(new GenericMessage<>("foo")); Map<String, Object> headers = result.getHeaders(); assertEquals("http://test", headers.get(WebServiceHeaders.SOAP_ACTION)); }
@Test public void expression() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(expressionInput); Message<?> result = template.sendAndReceive(new GenericMessage<>("foo")); Map<String, Object> headers = result.getHeaders(); assertEquals("http://foo", headers.get(WebServiceHeaders.SOAP_ACTION)); }
@Test public void expressions() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(this.expressionsInput); Message<?> result = template.sendAndReceive(new GenericMessage<>("foo")); Map<String, Object> headers = result.getHeaders(); assertEquals("foo.to", headers.get(MailHeaders.TO)); assertEquals("foo.cc", headers.get(MailHeaders.CC)); assertEquals("foo.bcc", headers.get(MailHeaders.BCC)); assertEquals("foo.from", headers.get(MailHeaders.FROM)); assertEquals("foo.reply-to", headers.get(MailHeaders.REPLY_TO)); assertEquals("foo.subject", headers.get(MailHeaders.SUBJECT)); }
@Test public void literalValues() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(this.literalValuesInput); Message<?> result = template.sendAndReceive(new GenericMessage<>("test")); Map<String, Object> headers = result.getHeaders(); assertEquals("test.to", headers.get(MailHeaders.TO)); assertEquals("test.cc", headers.get(MailHeaders.CC)); assertEquals("test.bcc", headers.get(MailHeaders.BCC)); assertEquals("test.from", headers.get(MailHeaders.FROM)); assertEquals("test.reply-to", headers.get(MailHeaders.REPLY_TO)); assertEquals("test.subject", headers.get(MailHeaders.SUBJECT)); assertEquals("foo.txt", headers.get(MailHeaders.ATTACHMENT_FILENAME)); assertEquals("1", headers.get(MailHeaders.MULTIPART_MODE)); }
@Test public void testReplaceTtl() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(this.inputTtl); Message<?> reply = template.sendAndReceive(new GenericMessage<String>("ttl")); assertNotNull(reply); assertEquals("echo:ttl", reply.getPayload()); String stringReplyChannel = reply.getHeaders().get("stringReplyChannel", String.class); assertThat(TestUtils.getPropertyValue( TestUtils.getPropertyValue(registry, "channels", Map.class) .get(stringReplyChannel), "expireAt", Long.class) - System.currentTimeMillis(), greaterThan(100000L)); }
@Test public void priorityExplicitOverwriteTrue() { MessageChannel channel = this.context.getBean("priorityExplicitOverwriteTrueInput", MessageChannel.class); MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(channel); Message<?> result = template.sendAndReceive(new GenericMessage<String>("test")); assertNotNull(result); assertEquals(new Integer(42), new IntegrationMessageHeaderAccessor(result).getPriority()); }
@Test public void gatewayMaintainsReplyChannelAndInboundHistory() { ActiveMqTestUtils.prepare(); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "gatewayMaintainsReplyChannel.xml", this.getClass()); SampleGateway gateway = context.getBean("gateway", SampleGateway.class); SubscribableChannel jmsInput = context.getBean("jmsInput", SubscribableChannel.class); MessageHandler handler = message -> { MessageHistory history = MessageHistory.read(message); assertNotNull(history); Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "inboundGateway", 0); assertNotNull(componentHistoryRecord); assertEquals("jms:inbound-gateway", componentHistoryRecord.get("type")); MessagingTemplate messagingTemplate = new MessagingTemplate(); messagingTemplate.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel()); messagingTemplate.send(message); }; jmsInput.subscribe(handler); String result = gateway.echo("hello"); assertEquals("hello", result); JmsOutboundGateway gw1 = context.getBean("chain1$child.gateway.handler", JmsOutboundGateway.class); MessageChannel out = TestUtils.getPropertyValue(gw1, "outputChannel", MessageChannel.class); assertThat(out.getClass().getSimpleName(), equalTo("ReplyForwardingMessageChannel")); JmsOutboundGateway gw2 = context.getBean("chain2$child.gateway.handler", JmsOutboundGateway.class); out = TestUtils.getPropertyValue(gw2, "outputChannel", MessageChannel.class); assertThat(out.getClass().getName(), containsString("MessageHandlerChain$")); context.close(); }
@Test public void testReplace() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(this.input); Message<?> reply = template.sendAndReceive(new GenericMessage<String>("foo")); assertNotNull(reply); assertEquals("echo:foo", reply.getPayload()); String stringReplyChannel = reply.getHeaders().get("stringReplyChannel", String.class); assertThat(TestUtils.getPropertyValue( TestUtils.getPropertyValue(registry, "channels", Map.class) .get(stringReplyChannel), "expireAt", Long.class) - System.currentTimeMillis(), lessThan(61000L)); }
/** * MessagingTemplate sets the errorChannel to the replyChannel so it gets any async * exceptions via the default {@link MessagePublishingErrorHandler}. */ @Test public void testReplaceError() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(this.inputPolled); Message<?> reply = template.sendAndReceive(new GenericMessage<String>("bar")); assertNotNull(reply); assertTrue(reply instanceof ErrorMessage); assertNotNull(((ErrorMessage) reply).getOriginalMessage()); assertThat(reply.getPayload(), not(instanceOf(MessagingExceptionWrapper.class))); }
/** * Test uses the Ignore Mode of the File OutboundGateway. When persisting * a payload using the File Outbound Gateway and the mode is set to IGNORE, * then the destination file will be created and written if it does not yet exist, * BUT if it exists it will not be overwritten. Instead the Message Payload will * be silently ignored. The reply message will contain the pre-existing destination * {@link File} as its payload. * */ @Test public void gatewayWithIgnoreMode() throws Exception { final MessagingTemplate messagingTemplate = new MessagingTemplate(); messagingTemplate.setDefaultDestination(this.gatewayWithIgnoreModeChannel); final String expectedFileContent = "Initial File Content:"; final File testFile = new File("test/fileToAppend.txt"); if (testFile.exists()) { testFile.delete(); } messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:")); Message<?> replyMessage = messagingTemplate.sendAndReceive(new GenericMessage<>("String content:")); String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile)); assertEquals(expectedFileContent, actualFileContent); assertTrue(replyMessage.getPayload() instanceof File); File replyPayload = (File) replyMessage.getPayload(); assertEquals(expectedFileContent, new String(FileCopyUtils.copyToByteArray(replyPayload))); }
/** * Test uses the Append Mode of the File Outbound Gateway. When persisting * a payload using the File Outbound Gateway and the mode is set to APPEND, * then the destination file will be created and written, if it does not yet * exist. BUT if it exists it will be appended to the existing file. * * The reply message will contain the concatenated destination * {@link File} as its payload. * */ @Test public void gatewayWithAppendMode() throws Exception { final MessagingTemplate messagingTemplate = new MessagingTemplate(); messagingTemplate.setDefaultDestination(this.gatewayWithAppendModeChannel); String expectedFileContent = "Initial File Content:String content:"; File testFile = new File("test/fileToAppend.txt"); if (testFile.exists()) { testFile.delete(); } messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:")); Message<?> m = messagingTemplate.sendAndReceive(new GenericMessage<>("String content:")); String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile)); assertEquals(expectedFileContent, actualFileContent); assertTrue(m.getPayload() instanceof File); File replyPayload = (File) m.getPayload(); assertEquals(expectedFileContent, new String(FileCopyUtils.copyToByteArray(replyPayload))); }
@Test public void testOutboundInboundDefaultIsTx() { this.outboundGateway.setExtractRequestPayload(true); this.outboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractRequestPayload(true); final AtomicBoolean failOnce = new AtomicBoolean(); MessageHandler handler = message -> { assertTrue(message.getPayload() instanceof String); if (failOnce.compareAndSet(false, true)) { throw new RuntimeException("test tx"); } MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination((MessageChannel) message.getHeaders().getReplyChannel()); template.send(message); }; this.jmsInputChannel.subscribe(handler); this.outboundChannel.send(new GenericMessage<String>("Hello " + this.testName.getMethodName())); Message<?> replyMessage = this.replyChannel.receive(10000); assertTrue(replyMessage.getPayload() instanceof String); this.jmsInputChannel.unsubscribe(handler); }
@Test public void testReplaceCustomTtl() { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(this.inputCustomTtl); Message<String> requestMessage = MessageBuilder.withPayload("ttl") .setHeader("channelTTL", 180000) .build(); Message<?> reply = template.sendAndReceive(requestMessage); assertNotNull(reply); assertEquals("echo:ttl", reply.getPayload()); String stringReplyChannel = reply.getHeaders().get("stringReplyChannel", String.class); assertThat(TestUtils.getPropertyValue( TestUtils.getPropertyValue(registry, "channels", Map.class) .get(stringReplyChannel), "expireAt", Long.class) - System.currentTimeMillis(), allOf(greaterThan(160000L), lessThan(181000L))); // Now for Elvis... reply = template.sendAndReceive(new GenericMessage<String>("ttl")); assertNotNull(reply); assertEquals("echo:ttl", reply.getPayload()); stringReplyChannel = reply.getHeaders().get("stringReplyChannel", String.class); assertThat(TestUtils.getPropertyValue( TestUtils.getPropertyValue(registry, "channels", Map.class) .get(stringReplyChannel), "expireAt", Long.class) - System.currentTimeMillis(), greaterThan(220000L)); }
/** * Test is exactly the same as {@link #gatewayWithFailMode()}. However, the * mode is provided in lower-case ensuring that the mode can be provided * in an case-insensitive fashion. * * Instead a {@link MessageHandlingException} will be thrown. * */ @Test public void gatewayWithFailModeLowercase() throws Exception { final MessagingTemplate messagingTemplate = new MessagingTemplate(); messagingTemplate.setDefaultDestination(this.gatewayWithFailModeLowercaseChannel); String expectedFileContent = "Initial File Content:"; File testFile = new File("test/fileToAppend.txt"); if (testFile.exists()) { testFile.delete(); } messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:")); final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile)); assertEquals(expectedFileContent, actualFileContent); try { messagingTemplate.sendAndReceive(new GenericMessage<>("String content:")); } catch (MessageHandlingException e) { assertThat(e.getMessage(), startsWith("The destination file already exists at '")); return; } fail("Was expecting a MessageHandlingException to be thrown."); }