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);
}
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);
}
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;
}
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.");
}
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.");
}
Aggregations