Search in sources :

Example 46 with MessagingTemplate

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

the class ManualFlowTests method testManualFlowRegistration.

@Test
public void testManualFlowRegistration() throws InterruptedException {
    IntegrationFlow myFlow = f -> f.<String, String>transform(String::toUpperCase).channel(MessageChannels.queue()).transform("Hello, "::concat, e -> e.poller(p -> p.fixedDelay(10).maxMessagesPerPoll(1).receiveTimeout(10))).handle(new BeanFactoryHandler());
    BeanFactoryHandler additionalBean = new BeanFactoryHandler();
    IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(myFlow).addBean(additionalBean).register();
    BeanFactoryHandler bean = this.beanFactory.getBean(flowRegistration.getId() + BeanFactoryHandler.class.getName() + "#0", BeanFactoryHandler.class);
    assertSame(additionalBean, bean);
    assertSame(this.beanFactory, bean.beanFactory);
    MessagingTemplate messagingTemplate = flowRegistration.getMessagingTemplate();
    messagingTemplate.setReceiveTimeout(10000);
    assertEquals("Hello, FOO", messagingTemplate.convertSendAndReceive("foo", String.class));
    assertEquals("Hello, BAR", messagingTemplate.convertSendAndReceive("bar", String.class));
    try {
        messagingTemplate.receive();
        fail("UnsupportedOperationException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(UnsupportedOperationException.class));
        assertThat(e.getMessage(), containsString("The 'receive()/receiveAndConvert()' isn't supported"));
    }
    flowRegistration.destroy();
    assertFalse(this.beanFactory.containsBean(flowRegistration.getId()));
    assertFalse(this.beanFactory.containsBean(flowRegistration.getId() + ".input"));
    assertFalse(this.beanFactory.containsBean(flowRegistration.getId() + BeanFactoryHandler.class.getName() + "#0"));
    ThreadPoolTaskScheduler taskScheduler = this.beanFactory.getBean(ThreadPoolTaskScheduler.class);
    Thread.sleep(100);
    assertEquals(0, taskScheduler.getActiveCount());
    assertTrue(additionalBean.destroyed);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Date(java.util.Date) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowAdapter(org.springframework.integration.dsl.IntegrationFlowAdapter) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) SpringRunner(org.springframework.test.context.junit4.SpringRunner) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) MessageProducerSpec(org.springframework.integration.dsl.MessageProducerSpec) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Objects(java.util.Objects) Configuration(org.springframework.context.annotation.Configuration) IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) DisposableBean(org.springframework.beans.factory.DisposableBean) Matchers.containsString(org.hamcrest.Matchers.containsString) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) MessageChannels(org.springframework.integration.dsl.MessageChannels) Scope(org.springframework.context.annotation.Scope) Assert.assertSame(org.junit.Assert.assertSame) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Flux(reactor.core.publisher.Flux) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 47 with MessagingTemplate

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

the class ServiceActivatorParserTests method sendAndReceive.

private Object sendAndReceive(MessageChannel channel, Object payload) {
    MessagingTemplate template = new MessagingTemplate();
    template.setDefaultDestination(channel);
    return template.convertSendAndReceive(payload, Object.class);
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate)

Example 48 with MessagingTemplate

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

the class StandardIntegrationFlowRegistration method getMessagingTemplate.

/**
 * Obtain a {@link MessagingTemplate} with its default destination set to the input channel
 * of the {@link IntegrationFlow}.
 * <p> Any {@link IntegrationFlow} bean (not only manually registered) can be used for this method.
 * <p> If {@link IntegrationFlow} doesn't start with the {@link MessageChannel}, the
 * {@link IllegalStateException} is thrown.
 * @return the {@link MessagingTemplate} instance
 */
@Override
public MessagingTemplate getMessagingTemplate() {
    if (this.messagingTemplate == null) {
        this.messagingTemplate = new MessagingTemplate(getInputChannel()) {

            @Override
            public Message<?> receive() {
                return receiveAndConvert(Message.class);
            }

            @Override
            public <T> T receiveAndConvert(Class<T> targetClass) {
                throw new UnsupportedOperationException("The 'receive()/receiveAndConvert()' " + "isn't supported on the 'IntegrationFlow' input channel.");
            }
        };
        this.messagingTemplate.setBeanFactory(this.beanFactory);
    }
    return this.messagingTemplate;
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Message(org.springframework.messaging.Message)

Example 49 with MessagingTemplate

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

the class FileOutboundGatewayParserTests method gatewayWithFailModeLowercase.

/**
 * 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<String>("Initial File Content:"));
    final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
    assertEquals(expectedFileContent, actualFileContent);
    try {
        messagingTemplate.sendAndReceive(new GenericMessage<String>("String content:"));
    } catch (MessageHandlingException e) {
        assertTrue(e.getMessage().startsWith("The destination file already exists at '"));
        return;
    }
    fail("Was expecting a MessageHandlingException to be thrown.");
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) File(java.io.File) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 50 with MessagingTemplate

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

the class FileOutboundGatewayParserTests method gatewayWithFailMode.

/**
 * Test uses the Fail mode of the File Outbound Gateway. When persisting
 * a payload using the File Outbound Gateway and the mode is set to Fail,
 * then the destination {@link File} will be created and written if it does
 * not yet exist. BUT if the destination {@link File} already exists, a
 * {@link MessageHandlingException} will be thrown.
 */
@Test
public void gatewayWithFailMode() throws Exception {
    final MessagingTemplate messagingTemplate = new MessagingTemplate();
    messagingTemplate.setDefaultDestination(this.gatewayWithFailModeChannel);
    String expectedFileContent = "Initial File Content:";
    File testFile = new File("test/fileToAppend.txt");
    if (testFile.exists()) {
        testFile.delete();
    }
    messagingTemplate.sendAndReceive(new GenericMessage<String>("Initial File Content:"));
    final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
    assertEquals(expectedFileContent, actualFileContent);
    try {
        messagingTemplate.sendAndReceive(new GenericMessage<String>("String content:"));
    } catch (MessageHandlingException e) {
        assertTrue(e.getMessage().startsWith("The destination file already exists at '"));
        return;
    }
    fail("Was expecting a MessageHandlingException to be thrown.");
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) File(java.io.File) MessageHandlingException(org.springframework.messaging.MessageHandlingException) 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