use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class TestAnnotatedEndpointWithReleaseStrategy method aggregatingMethod.
@Aggregator(inputChannel = "inputChannel")
public Message<?> aggregatingMethod(List<Message<?>> messages) {
List<Message<?>> sortableList = new ArrayList<>(messages);
Collections.sort(sortableList, new MessageSequenceComparator());
StringBuffer buffer = new StringBuffer();
Object correlationId = null;
for (Message<?> message : sortableList) {
buffer.append(message.getPayload().toString());
if (null == correlationId) {
correlationId = new IntegrationMessageHeaderAccessor(message).getCorrelationId();
}
}
Message<?> returnedMessage = new GenericMessage<>(buffer.toString());
this.aggregatedMessages.put(correlationId, returnedMessage);
return returnedMessage;
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class EnricherParserTests method integrationTest.
@Test
public void integrationTest() {
QueueChannel output = context.getBean("output", QueueChannel.class);
output.purge(null);
SubscribableChannel requests = context.getBean("requests", SubscribableChannel.class);
class Foo extends AbstractReplyProducingMessageHandler {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return new Source("foo");
}
}
Foo foo = new Foo();
foo.setOutputChannel(context.getBean("replies", MessageChannel.class));
requests.subscribe(foo);
Target original = new Target();
Message<?> request = MessageBuilder.withPayload(original).setHeader("sourceName", "test").setHeader("notOverwrite", "test").build();
context.getBean("input", MessageChannel.class).send(request);
Message<?> reply = output.receive(0);
Target enriched = (Target) reply.getPayload();
assertEquals("foo", enriched.getName());
assertEquals(42, enriched.getAge());
assertEquals(Gender.MALE, enriched.getGender());
assertTrue(enriched.isMarried());
assertNotSame(original, enriched);
assertEquals(1, adviceCalled);
MessageHeaders headers = reply.getHeaders();
assertEquals("bar", headers.get("foo"));
assertEquals(Gender.MALE, headers.get("testBean"));
assertEquals("foo", headers.get("sourceName"));
assertEquals("test", headers.get("notOverwrite"));
requests.unsubscribe(foo);
adviceCalled--;
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class EnricherParserTests3 method testSourceBeanResolver.
@Test
public void testSourceBeanResolver() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-context.xml", this.getClass());
MessageChannel beanResolveIn = context.getBean("beanResolveIn", MessageChannel.class);
PollableChannel beanResolveOut = context.getBean("beanResolveOut", PollableChannel.class);
SomeBean payload = new SomeBean("foo");
assertEquals("foo", payload.getNested().getValue());
beanResolveIn.send(new GenericMessage<SomeBean>(payload));
@SuppressWarnings("unchecked") Message<SomeBean> out = (Message<SomeBean>) beanResolveOut.receive();
assertSame(payload, out.getPayload());
assertEquals("bar", out.getPayload().getNested().getValue());
context.close();
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testTransactionalAdviceChain.
@Test
public void testTransactionalAdviceChain() throws Throwable {
SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
QueueChannel outputChannel = new QueueChannel();
TestApplicationContext context = TestUtils.createTestApplicationContext();
factoryBean.setBeanFactory(context.getBeanFactory());
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
factoryBean.setOutputChannel(outputChannel);
factoryBean.setSource(() -> new GenericMessage<>("test"));
PollerMetadata pollerMetadata = new PollerMetadata();
List<Advice> adviceChain = new ArrayList<Advice>();
final AtomicBoolean adviceApplied = new AtomicBoolean(false);
adviceChain.add((MethodInterceptor) invocation -> {
adviceApplied.set(true);
return invocation.proceed();
});
pollerMetadata.setTrigger(new PeriodicTrigger(5000));
pollerMetadata.setMaxMessagesPerPoll(1);
final AtomicInteger count = new AtomicInteger();
final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
adviceChain.add((MethodInterceptor) invocation -> {
count.incrementAndGet();
return invocation.proceed();
});
when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
count.incrementAndGet();
return ((MethodInvocation) invocation.getArgument(0)).proceed();
});
pollerMetadata.setAdviceChain(adviceChain);
factoryBean.setPollerMetadata(pollerMetadata);
factoryBean.setAutoStartup(true);
factoryBean.afterPropertiesSet();
context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
context.refresh();
Message<?> message = outputChannel.receive(5000);
assertEquals("test", message.getPayload());
assertEquals(1, count.get());
assertTrue("adviceChain was not applied", adviceApplied.get());
}
use of org.springframework.messaging.Message in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testAdviceChain.
@Test
public void testAdviceChain() throws Exception {
SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
QueueChannel outputChannel = new QueueChannel();
TestApplicationContext context = TestUtils.createTestApplicationContext();
factoryBean.setBeanFactory(context.getBeanFactory());
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
factoryBean.setOutputChannel(outputChannel);
factoryBean.setSource(() -> new GenericMessage<>("test"));
PollerMetadata pollerMetadata = new PollerMetadata();
List<Advice> adviceChain = new ArrayList<Advice>();
final AtomicBoolean adviceApplied = new AtomicBoolean(false);
adviceChain.add((MethodInterceptor) invocation -> {
adviceApplied.set(true);
return invocation.proceed();
});
pollerMetadata.setTrigger(new PeriodicTrigger(5000));
pollerMetadata.setMaxMessagesPerPoll(1);
pollerMetadata.setAdviceChain(adviceChain);
factoryBean.setPollerMetadata(pollerMetadata);
factoryBean.setAutoStartup(true);
factoryBean.afterPropertiesSet();
context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
context.refresh();
Message<?> message = outputChannel.receive(5000);
assertEquals("test", message.getPayload());
assertTrue("adviceChain was not applied", adviceApplied.get());
}
Aggregations