use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class ContentEnricherTests method replyChannelReplyTimingOut.
/**
* In this test a {@link Target} message is passed into a {@link ContentEnricher}.
* The Enricher passes the message to a "request-channel" that is backed by a
* {@link QueueChannel}. The consumer of the "request-channel" takes a long
* time to execute, longer actually than the specified "replyTimeout" set on
* the {@link ContentEnricher}.
*
* Due to the occurring replyTimeout, a Null replyMessage is returned and because
* "requiresReply" is set to "true" on the {@link ContentEnricher}, a
* {@link ReplyRequiredException} is raised.
*/
@Test
public void replyChannelReplyTimingOut() throws Exception {
final long requestTimeout = 500L;
final long replyTimeout = 100L;
final DirectChannel replyChannel = new DirectChannel();
final QueueChannel requestChannel = new QueueChannel(1);
final ContentEnricher enricher = new ContentEnricher();
enricher.setRequestChannel(requestChannel);
enricher.setReplyChannel(replyChannel);
enricher.setOutputChannel(new NullChannel());
enricher.setRequestTimeout(requestTimeout);
enricher.setReplyTimeout(replyTimeout);
final ExpressionFactoryBean expressionFactoryBean = new ExpressionFactoryBean("payload");
expressionFactoryBean.setSingleton(false);
expressionFactoryBean.afterPropertiesSet();
final Map<String, Expression> expressions = new HashMap<String, Expression>();
expressions.put("name", new LiteralExpression("cartman"));
expressions.put("child.name", expressionFactoryBean.getObject());
enricher.setPropertyExpressions(expressions);
enricher.setRequiresReply(true);
enricher.setBeanName("Enricher");
enricher.setBeanFactory(mock(BeanFactory.class));
enricher.afterPropertiesSet();
final AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
fail(e.getMessage());
}
return new Target("child");
}
};
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
final PollingConsumer consumer = new PollingConsumer(requestChannel, handler);
final TestErrorHandler errorHandler = new TestErrorHandler();
consumer.setTrigger(new PeriodicTrigger(0));
consumer.setErrorHandler(errorHandler);
consumer.setTaskScheduler(taskScheduler);
consumer.setBeanFactory(mock(BeanFactory.class));
consumer.afterPropertiesSet();
consumer.start();
final Target target = new Target("replace me");
Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
try {
enricher.handleMessage(requestMessage);
} catch (ReplyRequiredException e) {
assertEquals("No reply produced by handler 'Enricher', and its 'requiresReply' property is set to true.", e.getMessage());
return;
}
fail("ReplyRequiredException expected.");
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class ApplicationContextMessageBusTests method exactlyOneConsumerReceivesPointToPointMessage.
@Test
public void exactlyOneConsumerReceivesPointToPointMessage() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
QueueChannel inputChannel = new QueueChannel();
QueueChannel outputChannel1 = new QueueChannel();
QueueChannel outputChannel2 = new QueueChannel();
AbstractReplyProducingMessageHandler handler1 = new AbstractReplyProducingMessageHandler() {
@Override
public Object handleRequestMessage(Message<?> message) {
return message;
}
};
AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {
@Override
public Object handleRequestMessage(Message<?> message) {
return message;
}
};
context.registerChannel("input", inputChannel);
context.registerChannel("output1", outputChannel1);
context.registerChannel("output2", outputChannel2);
handler1.setOutputChannel(outputChannel1);
handler2.setOutputChannel(outputChannel2);
PollingConsumer endpoint1 = new PollingConsumer(inputChannel, handler1);
endpoint1.setBeanFactory(mock(BeanFactory.class));
PollingConsumer endpoint2 = new PollingConsumer(inputChannel, handler2);
endpoint2.setBeanFactory(mock(BeanFactory.class));
context.registerEndpoint("testEndpoint1", endpoint1);
context.registerEndpoint("testEndpoint2", endpoint2);
context.refresh();
inputChannel.send(new GenericMessage<String>("testing"));
Message<?> message1 = outputChannel1.receive(10000);
Message<?> message2 = outputChannel2.receive(0);
context.stop();
assertTrue("exactly one message should be null", message1 == null ^ message2 == null);
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class ApplicationContextMessageBusTests method endpointRegistrationWithInputChannelReference.
@Test
public void endpointRegistrationWithInputChannelReference() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
QueueChannel sourceChannel = new QueueChannel();
QueueChannel targetChannel = new QueueChannel();
context.registerChannel("sourceChannel", sourceChannel);
context.registerChannel("targetChannel", targetChannel);
Message<String> message = MessageBuilder.withPayload("test").setReplyChannelName("targetChannel").build();
sourceChannel.send(message);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
public Object handleRequestMessage(Message<?> message) {
return message;
}
};
handler.setBeanFactory(context);
handler.afterPropertiesSet();
PollingConsumer endpoint = new PollingConsumer(sourceChannel, handler);
endpoint.setBeanFactory(mock(BeanFactory.class));
context.registerEndpoint("testEndpoint", endpoint);
context.refresh();
Message<?> result = targetChannel.receive(10000);
assertEquals("test", result.getPayload());
context.stop();
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class ApplicationContextMessageBusTests method bothConsumersReceivePublishSubscribeMessage.
@Test
public void bothConsumersReceivePublishSubscribeMessage() throws InterruptedException {
TestApplicationContext context = TestUtils.createTestApplicationContext();
PublishSubscribeChannel inputChannel = new PublishSubscribeChannel();
QueueChannel outputChannel1 = new QueueChannel();
QueueChannel outputChannel2 = new QueueChannel();
final CountDownLatch latch = new CountDownLatch(2);
AbstractReplyProducingMessageHandler handler1 = new AbstractReplyProducingMessageHandler() {
@Override
public Object handleRequestMessage(Message<?> message) {
latch.countDown();
return message;
}
};
AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {
@Override
public Object handleRequestMessage(Message<?> message) {
latch.countDown();
return message;
}
};
context.registerChannel("input", inputChannel);
context.registerChannel("output1", outputChannel1);
context.registerChannel("output2", outputChannel2);
handler1.setOutputChannel(outputChannel1);
handler2.setOutputChannel(outputChannel2);
EventDrivenConsumer endpoint1 = new EventDrivenConsumer(inputChannel, handler1);
EventDrivenConsumer endpoint2 = new EventDrivenConsumer(inputChannel, handler2);
context.registerEndpoint("testEndpoint1", endpoint1);
context.registerEndpoint("testEndpoint2", endpoint2);
context.refresh();
inputChannel.send(new GenericMessage<String>("testing"));
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals("both handlers should have been invoked", 0, latch.getCount());
Message<?> message1 = outputChannel1.receive(500);
Message<?> message2 = outputChannel2.receive(500);
context.stop();
assertNotNull("both handlers should have replied to the message", message1);
assertNotNull("both handlers should have replied to the message", message2);
}
use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.
the class NestedGatewayTests method replyChannelRetained.
@Test
public void replyChannelRetained() {
DirectChannel requestChannel = new DirectChannel();
DirectChannel replyChannel = new DirectChannel();
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage.getPayload() + "-reply";
}
});
MessagingGatewaySupport gateway = new MessagingGatewaySupport() {
};
gateway.setRequestChannel(requestChannel);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.afterPropertiesSet();
Message<?> message = MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build();
Message<?> reply = gateway.sendAndReceiveMessage(message);
assertEquals("test-reply", reply.getPayload());
assertEquals(replyChannel, reply.getHeaders().getReplyChannel());
}
Aggregations