use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class PseudoTransactionalMessageSourceTests method testCommitWithManager.
@Test
public void testCommitWithManager() {
final PollableChannel queueChannel = new QueueChannel();
TransactionTemplate transactionTemplate = new TransactionTemplate(new PseudoTransactionManager());
transactionTemplate.execute(status -> {
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
syncProcessor.setBeanFactory(mock(BeanFactory.class));
syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix"));
syncProcessor.setBeforeCommitChannel(queueChannel);
syncProcessor.setAfterCommitChannel(queueChannel);
syncProcessor.setAfterCommitExpression(new SpelExpressionParser().parseExpression("#baz"));
DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
adapter.setTransactionSynchronizationFactory(syncFactory);
QueueChannel outputChannel = new QueueChannel();
adapter.setOutputChannel(outputChannel);
adapter.setSource(new MessageSource<String>() {
@Override
public Message<String> receive() {
GenericMessage<String> message = new GenericMessage<String>("foo");
IntegrationResourceHolder holder = (IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this);
holder.addAttribute("baz", "qux");
holder.addAttribute("bix", "qox");
return message;
}
});
doPoll(adapter);
return null;
});
Message<?> beforeCommitMessage = queueChannel.receive(1000);
assertNotNull(beforeCommitMessage);
assertEquals("qox", beforeCommitMessage.getPayload());
Message<?> afterCommitMessage = queueChannel.receive(1000);
assertNotNull(afterCommitMessage);
assertEquals("qux", afterCommitMessage.getPayload());
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class PseudoTransactionalMessageSourceTests method testRollback.
@Test
public void testRollback() {
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
syncProcessor.setBeanFactory(mock(BeanFactory.class));
PollableChannel queueChannel = new QueueChannel();
syncProcessor.setAfterRollbackChannel(queueChannel);
syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz"));
DefaultTransactionSynchronizationFactory syncFactory = new DefaultTransactionSynchronizationFactory(syncProcessor);
adapter.setTransactionSynchronizationFactory(syncFactory);
QueueChannel outputChannel = new QueueChannel();
adapter.setOutputChannel(outputChannel);
final Message<?> testMessage = new GenericMessage<>("foo");
adapter.setSource(new MessageSource<String>() {
@Override
public Message<String> receive() {
GenericMessage<String> message = new GenericMessage<String>("foo");
((IntegrationResourceHolder) TransactionSynchronizationManager.getResource(this)).addAttribute("baz", testMessage);
return message;
}
});
TransactionSynchronizationManager.initSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(true);
doPoll(adapter);
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
Message<?> rollbackMessage = queueChannel.receive(1000);
assertNotNull(rollbackMessage);
assertSame(testMessage, rollbackMessage);
TransactionSynchronizationManager.clearSynchronization();
TransactionSynchronizationManager.setActualTransactionActive(false);
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class ServiceActivatorEndpointTests method dynamicReplyChannel.
@Test
public void dynamicReplyChannel() throws Exception {
TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
testApplicationContext.refresh();
final QueueChannel replyChannel1 = new QueueChannel();
final QueueChannel replyChannel2 = new QueueChannel();
replyChannel2.setBeanName("replyChannel2");
Object handler = new Object() {
@SuppressWarnings("unused")
public Message<?> handle(Message<?> message) {
return new GenericMessage<String>("foo" + message.getPayload());
}
};
ServiceActivatingHandler endpoint = new ServiceActivatingHandler(handler, "handle");
TestChannelResolver channelResolver = new TestChannelResolver();
channelResolver.addChannel("replyChannel2", replyChannel2);
endpoint.setChannelResolver(channelResolver);
endpoint.setBeanFactory(testApplicationContext);
endpoint.afterPropertiesSet();
Message<String> testMessage1 = MessageBuilder.withPayload("bar").setReplyChannel(replyChannel1).build();
endpoint.handleMessage(testMessage1);
Message<?> reply1 = replyChannel1.receive(50);
assertNotNull(reply1);
assertEquals("foobar", reply1.getPayload());
Message<?> reply2 = replyChannel2.receive(0);
assertNull(reply2);
Message<String> testMessage2 = MessageBuilder.fromMessage(testMessage1).setReplyChannelName("replyChannel2").build();
endpoint.handleMessage(testMessage2);
reply1 = replyChannel1.receive(0);
assertNull(reply1);
reply2 = replyChannel2.receive(0);
assertNotNull(reply2);
assertEquals("foobar", reply2.getPayload());
testApplicationContext.close();
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class MapToObjectTransformerParserTests method testMapToObjectTransformationWithRef.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testMapToObjectTransformationWithRef() {
Map map = new HashMap();
map.put("fname", "Justin");
map.put("lname", "Case");
Address address = new Address();
address.setStreet("1123 Main st");
map.put("address", address);
Message message = MessageBuilder.withPayload(map).build();
inputA.send(message);
Message<?> newMessage = outputA.receive();
Person person = (Person) newMessage.getPayload();
assertNotNull(person);
assertEquals("Justin", person.getFname());
assertEquals("Case", person.getLname());
assertNull(person.getSsn());
assertNotNull(person.getAddress());
assertEquals("1123 Main st", person.getAddress().getStreet());
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class AsyncMessagingTemplateTests method asyncReceiveWithTimeoutException.
@Test(expected = TimeoutException.class)
public void asyncReceiveWithTimeoutException() throws Exception {
AsyncMessagingTemplate template = new AsyncMessagingTemplate();
Future<Message<?>> result = template.asyncReceive(new QueueChannel());
result.get(100, TimeUnit.MILLISECONDS);
}
Aggregations