use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class ManualFlowTests method testRoleControl.
@Test
public void testRoleControl() {
String testRole = "bridge";
PollableChannel resultChannel = new QueueChannel();
IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow -> flow.bridge(e -> e.role(testRole)).channel(resultChannel)).register();
MessagingTemplate messagingTemplate = this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId());
messagingTemplate.send(new GenericMessage<>("test"));
Message<?> receive = resultChannel.receive(1000);
assertNotNull(receive);
assertEquals("test", receive.getPayload());
this.roleController.stopLifecyclesInRole(testRole);
try {
messagingTemplate.send(new GenericMessage<>("test2"));
} catch (Exception e) {
assertThat(e, instanceOf(MessageDeliveryException.class));
assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for channel"));
}
this.roleController.startLifecyclesInRole(testRole);
messagingTemplate.send(new GenericMessage<>("test2"));
receive = resultChannel.receive(1000);
assertNotNull(receive);
assertEquals("test2", receive.getPayload());
flowRegistration.destroy();
assertTrue(this.roleController.getEndpointsRunningStatus(testRole).isEmpty());
}
use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class ReactiveStreamsTests method testFromPublisher.
@Test
public void testFromPublisher() {
Flux<Message<?>> messageFlux = Flux.just("1,2,3,4").map(v -> v.split(",")).flatMapIterable(Arrays::asList).map(Integer::parseInt).log("org.springframework.integration.flux").map(GenericMessage<Integer>::new);
QueueChannel resultChannel = new QueueChannel();
IntegrationFlow integrationFlow = IntegrationFlows.from(messageFlux).log("org.springframework.integration.flux2").<Integer, Integer>transform(p -> p * 2).channel(resultChannel).get();
this.integrationFlowContext.registration(integrationFlow).register();
for (int i = 0; i < 4; i++) {
Message<?> receive = resultChannel.receive(10000);
assertNotNull(receive);
assertEquals((i + 1) * 2, receive.getPayload());
}
}
use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class TransformerTests method transformWithHeader.
@Test
public void transformWithHeader() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload("Foo").setReplyChannel(replyChannel).build();
this.pojoTransformFlowInput.send(message);
Message<?> receive = replyChannel.receive(10000);
assertNotNull(receive);
assertEquals("FooBar", receive.getPayload());
try {
this.pojoTransformFlowInput.send(message);
fail("MessageRejectedException expected");
} catch (Exception e) {
assertThat(e, instanceOf(MessageRejectedException.class));
assertThat(e.getMessage(), containsString("IdempotentReceiver"));
assertThat(e.getMessage(), containsString("rejected duplicate Message"));
}
assertNotNull(this.idempotentDiscardChannel.receive(10000));
assertNotNull(this.adviceChannel.receive(10000));
}
use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class ExpressionEvaluatingMessageSourceIntegrationTests method test.
@Test
public void test() throws Exception {
QueueChannel channel = new QueueChannel();
String payloadExpression = "'test-' + T(org.springframework.integration.endpoint.ExpressionEvaluatingMessageSourceIntegrationTests).next()";
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
Map<String, Expression> headerExpressions = new HashMap<String, Expression>();
headerExpressions.put("foo", new LiteralExpression("x"));
headerExpressions.put("bar", new SpelExpressionParser().parseExpression("7 * 6"));
ExpressionFactoryBean factoryBean = new ExpressionFactoryBean(payloadExpression);
factoryBean.afterPropertiesSet();
Expression expression = factoryBean.getObject();
ExpressionEvaluatingMessageSource<Object> source = new ExpressionEvaluatingMessageSource<Object>(expression, Object.class);
source.setBeanFactory(mock(BeanFactory.class));
source.setHeaderExpressions(headerExpressions);
SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
adapter.setSource(source);
adapter.setTaskScheduler(scheduler);
adapter.setMaxMessagesPerPoll(3);
adapter.setTrigger(new PeriodicTrigger(60000));
adapter.setOutputChannel(channel);
adapter.setErrorHandler(t -> {
throw new IllegalStateException("unexpected exception in test", t);
});
adapter.start();
List<Message<?>> messages = new ArrayList<Message<?>>();
for (int i = 0; i < 3; i++) {
messages.add(channel.receive(1000));
}
scheduler.destroy();
Message<?> message1 = messages.get(0);
assertEquals("test-1", message1.getPayload());
assertEquals("x", message1.getHeaders().get("foo"));
assertEquals(42, message1.getHeaders().get("bar"));
Message<?> message2 = messages.get(1);
assertEquals("test-2", message2.getPayload());
assertEquals("x", message2.getHeaders().get("foo"));
assertEquals(42, message2.getHeaders().get("bar"));
Message<?> message3 = messages.get(2);
assertEquals("test-3", message3.getPayload());
assertEquals("x", message3.getHeaders().get("foo"));
assertEquals(42, message3.getHeaders().get("bar"));
}
use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.
the class PollingLifecycleTests method ensurePollerTaskStops.
@Test
public void ensurePollerTaskStops() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
QueueChannel channel = new QueueChannel();
channel.send(new GenericMessage<String>("foo"));
MessageHandler handler = Mockito.spy(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
latch.countDown();
}
});
PollingConsumer consumer = new PollingConsumer(channel, handler);
consumer.setTrigger(new PeriodicTrigger(0));
consumer.setErrorHandler(errorHandler);
consumer.setTaskScheduler(taskScheduler);
consumer.setBeanFactory(mock(BeanFactory.class));
consumer.afterPropertiesSet();
consumer.start();
assertTrue(latch.await(2, TimeUnit.SECONDS));
Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class));
consumer.stop();
for (int i = 0; i < 10; i++) {
channel.send(new GenericMessage<String>("foo"));
}
// give enough time for poller to kick in if it didn't stop properly
Thread.sleep(2000);
// we'll still have a natural race condition between call to stop() and poller polling
// so what we really have to assert is that it doesn't poll for more then once after stop() was called
Mockito.reset(handler);
Mockito.verify(handler, atMost(1)).handleMessage(Mockito.any(Message.class));
}
Aggregations