Search in sources :

Example 51 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class FileOutboundGatewayParserTests method gatewayWithIgnoreMode.

/**
 * 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<String>("Initial File Content:"));
    Message<?> replyMessage = messagingTemplate.sendAndReceive(new GenericMessage<String>("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)));
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) File(java.io.File) Test(org.junit.Test)

Example 52 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class StoredProcJavaConfigTests method test.

@Test
public void test() {
    Message<?> received = fooChannel.receive(10000);
    assertNotNull(received);
    Collection<?> primes = (Collection<?>) received.getPayload();
    assertThat(primes, Matchers.<Object>contains(2, 3, 5, 7));
    received = fooChannel.receive(100);
    // verify maxMessagesPerPoll == 1
    assertNull(received);
    MessagingTemplate template = new MessagingTemplate(this.control);
    template.convertAndSend("@'storedProcJavaConfigTests.Config.storedProc.inboundChannelAdapter'.stop()");
    assertFalse(template.convertSendAndReceive("@'storedProcJavaConfigTests.Config.storedProc.inboundChannelAdapter'.isRunning()", Boolean.class));
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Collection(java.util.Collection) Test(org.junit.Test)

Example 53 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class StoredProcOutboundGatewayParserTests method testReplyTimeoutIsSet.

@Test
public void testReplyTimeoutIsSet() throws Exception {
    setUp("storedProcOutboundGatewayParserTest.xml", getClass());
    DirectFieldAccessor accessor = new DirectFieldAccessor(this.outboundGateway);
    Object source = accessor.getPropertyValue("handler");
    accessor = new DirectFieldAccessor(source);
    source = accessor.getPropertyValue("messagingTemplate");
    MessagingTemplate messagingTemplate = (MessagingTemplate) source;
    accessor = new DirectFieldAccessor(messagingTemplate);
    Long sendTimeout = (Long) accessor.getPropertyValue("sendTimeout");
    assertEquals("Wrong sendTimeout", Long.valueOf(555L), sendTimeout);
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 54 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class JdbcOutboundGatewayParserTests method setupMessagingTemplate.

protected void setupMessagingTemplate() {
    PollableChannel pollableChannel = this.context.getBean("output", PollableChannel.class);
    this.messagingTemplate = new MessagingTemplate();
    this.messagingTemplate.setDefaultDestination(pollableChannel);
    this.messagingTemplate.setReceiveTimeout(10000);
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) PollableChannel(org.springframework.messaging.PollableChannel)

Example 55 with MessagingTemplate

use of org.springframework.integration.core.MessagingTemplate in project spring-integration by spring-projects.

the class MonitorTests method testStats.

@Test
public void testStats() throws InterruptedException {
    final CountDownLatch afterSendLatch = new CountDownLatch(1);
    DefaultMessageChannelMetrics channelMetrics = TestUtils.getPropertyValue(this.next, "channelMetrics", DefaultMessageChannelMetrics.class);
    channelMetrics = Mockito.spy(channelMetrics);
    Mockito.doAnswer(invocation -> {
        Object result = invocation.callRealMethod();
        afterSendLatch.countDown();
        return result;
    }).when(channelMetrics).afterSend(Mockito.any(MetricsContext.class), Mockito.eq(Boolean.TRUE));
    new DirectFieldAccessor(this.next).setPropertyValue("channelMetrics", channelMetrics);
    MessagingTemplate messagingTemplate = new MessagingTemplate(this.input);
    messagingTemplate.setReceiveTimeout(10000);
    Integer active = messagingTemplate.convertSendAndReceive("foo", Integer.class);
    assertEquals(1, active.intValue());
    assertTrue(afterSendLatch.await(10, TimeUnit.SECONDS));
    assertEquals(0, this.handler.getActiveCount());
    assertEquals(1, this.handler.getHandleCount());
    assertThat(this.handler.getDuration().getMax(), greaterThan(99.0));
    assertThat(this.handler.getDuration().getMax(), lessThan(10000.0));
    assertEquals(1, this.input.getSendCount());
    assertEquals(1, this.input.getReceiveCount());
    assertEquals(1, this.next.getSendCount());
    assertThat(this.next.getSendDuration().getMax(), greaterThan(99.0));
    assertThat(this.next.getSendDuration().getMax(), lessThan(10000.0));
    Message<?> fromInbound = this.output.receive(10000);
    assertNotNull(fromInbound);
    assertEquals(0, fromInbound.getPayload());
    fromInbound = this.output.receive(10000);
    assertNotNull(fromInbound);
    assertEquals(1, fromInbound.getPayload());
    assertThat(this.source.getMessageCount(), greaterThanOrEqualTo(2));
    assertThat(this.nullChannel.getSendCount(), greaterThanOrEqualTo(2));
    assertThat(this.pubsub.getSendCount(), greaterThanOrEqualTo(2));
}
Also used : DefaultMessageChannelMetrics(org.springframework.integration.support.management.DefaultMessageChannelMetrics) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) MetricsContext(org.springframework.integration.support.management.MetricsContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

MessagingTemplate (org.springframework.integration.core.MessagingTemplate)63 Test (org.junit.Test)58 MessageChannel (org.springframework.messaging.MessageChannel)22 PollableChannel (org.springframework.messaging.PollableChannel)12 GenericMessage (org.springframework.messaging.support.GenericMessage)11 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 Message (org.springframework.messaging.Message)6 File (java.io.File)5 Map (java.util.Map)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Assert.assertThat (org.junit.Assert.assertThat)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Assert.fail (org.junit.Assert.fail)4 MessageHandler (org.springframework.messaging.MessageHandler)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)3 Assert.assertEquals (org.junit.Assert.assertEquals)3