use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class EndpointSpec method obtainInputChannelFromFlow.
/**
* Try to get a {@link MessageChannel} as an input for the provided {@link IntegrationFlow}
* or create one and wrap the provided flow to a new one.
* @param subFlow the {@link IntegrationFlow} to extract input channel.
* @param evaluateInternalBuilder true if an internal {@link IntegrationFlowDefinition} should be
* evaluated to an {@link IntegrationFlow} component or left as a builder in the {@link #componentsToRegister}
* for future use-case. For example the builder is used for router configurations to retain beans
* registration order for parent-child dependencies.
* @return the input channel of the flow of create one
* @since 5.0.4
*/
protected MessageChannel obtainInputChannelFromFlow(IntegrationFlow subFlow, boolean evaluateInternalBuilder) {
Assert.notNull(subFlow, "'subFlow' must not be null");
MessageChannel messageChannel = subFlow.getInputChannel();
if (messageChannel == null) {
messageChannel = new DirectChannel();
IntegrationFlowDefinition<?> flowBuilder = IntegrationFlows.from(messageChannel);
subFlow.configure(flowBuilder);
this.componentsToRegister.put(evaluateInternalBuilder ? flowBuilder.get() : flowBuilder, null);
} else {
this.componentsToRegister.put(subFlow, null);
}
return messageChannel;
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class IntegrationFlows method from.
/**
* Populate the {@link MessageChannel} to the new {@link IntegrationFlowBuilder}
* chain, which becomes as a {@code requestChannel} for the Messaging Gateway(s) built
* on the provided service interface.
* <p>A gateway proxy bean for provided service interface is registered under a name of
* the provided {@code beanName} if not null, or from the
* {@link org.springframework.integration.annotation.MessagingGateway#name()} if present
* or as a fallback to the {@link IntegrationFlow} bean name plus {@code .gateway} suffix.
* @param serviceInterface the service interface class with an optional
* {@link org.springframework.integration.annotation.MessagingGateway} annotation.
* @param beanName the bean name to be used for registering bean for the gateway proxy
* @return new {@link IntegrationFlowBuilder}.
*/
public static IntegrationFlowBuilder from(Class<?> serviceInterface, String beanName) {
final DirectChannel gatewayRequestChannel = new DirectChannel();
GatewayProxyFactoryBean gatewayProxyFactoryBean = new AnnotationGatewayProxyFactoryBean(serviceInterface);
gatewayProxyFactoryBean.setDefaultRequestChannel(gatewayRequestChannel);
gatewayProxyFactoryBean.setBeanName(beanName);
return from(gatewayRequestChannel).addComponent(gatewayProxyFactoryBean);
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class IntegrationFlows method from.
private static IntegrationFlowBuilder from(MessagingGatewaySupport inboundGateway, IntegrationFlowBuilder integrationFlowBuilder) {
MessageChannel outputChannel = inboundGateway.getRequestChannel();
if (outputChannel == null) {
outputChannel = new DirectChannel();
inboundGateway.setRequestChannel(outputChannel);
}
if (integrationFlowBuilder == null) {
integrationFlowBuilder = from(outputChannel);
} else {
integrationFlowBuilder.channel(outputChannel);
}
return integrationFlowBuilder.addComponent(inboundGateway);
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerDirectChannel.
@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerDirectChannel() throws InterruptedException {
DirectChannel testChannel = new DirectChannel();
Subscriber<Message<?>> testSubscriber = (Subscriber<Message<?>>) Mockito.mock(Subscriber.class);
BlockingQueue<Message<?>> messages = new LinkedBlockingQueue<>();
willAnswer(i -> {
messages.put(i.getArgument(0));
return null;
}).given(testSubscriber).onNext(any(Message.class));
ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
reactiveConsumer.afterPropertiesSet();
reactiveConsumer.start();
Message<?> testMessage = new GenericMessage<>("test");
testChannel.send(testMessage);
ArgumentCaptor<Subscription> subscriptionArgumentCaptor = ArgumentCaptor.forClass(Subscription.class);
verify(testSubscriber).onSubscribe(subscriptionArgumentCaptor.capture());
Subscription subscription = subscriptionArgumentCaptor.getValue();
subscription.request(1);
Message<?> message = messages.poll(10, TimeUnit.SECONDS);
assertSame(testMessage, message);
reactiveConsumer.stop();
try {
testChannel.send(testMessage);
fail("MessageDeliveryException");
} catch (Exception e) {
assertThat(e, instanceOf(MessageDeliveryException.class));
}
reactiveConsumer.start();
subscription.request(1);
testMessage = new GenericMessage<>("test2");
testChannel.send(testMessage);
message = messages.poll(10, TimeUnit.SECONDS);
assertSame(testMessage, message);
verify(testSubscriber, never()).onError(any(Throwable.class));
verify(testSubscriber, never()).onComplete();
assertTrue(messages.isEmpty());
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class AggregatorTests method testAggPerfDefaultPartial.
@Test
public void testAggPerfDefaultPartial() throws InterruptedException, ExecutionException, TimeoutException {
AggregatingMessageHandler handler = new AggregatingMessageHandler(new DefaultAggregatingMessageGroupProcessor());
handler.setCorrelationStrategy(message -> "foo");
handler.setReleasePartialSequences(true);
DirectChannel outputChannel = new DirectChannel();
handler.setOutputChannel(outputChannel);
final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>();
outputChannel.subscribe(message -> {
Collection<?> payload = (Collection<?>) message.getPayload();
logger.warn("Received " + payload.size());
resultFuture.complete(payload);
});
SimpleMessageStore store = new SimpleMessageStore();
SimpleMessageGroupFactory messageGroupFactory = new SimpleMessageGroupFactory(SimpleMessageGroupFactory.GroupType.BLOCKING_QUEUE);
store.setMessageGroupFactory(messageGroupFactory);
handler.setMessageStore(store);
StopWatch stopwatch = new StopWatch();
stopwatch.start();
for (int i = 0; i < 120000; i++) {
if (i % 10000 == 0) {
stopwatch.stop();
logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)");
stopwatch.start();
}
handler.handleMessage(MessageBuilder.withPayload("foo").setSequenceSize(120000).setSequenceNumber(i + 1).build());
}
stopwatch.stop();
logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)");
Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS);
assertNotNull(result);
assertEquals(120000, result.size());
// actually < 2.0, was many minutes
assertThat(stopwatch.getTotalTimeSeconds(), lessThan(60.0));
}
Aggregations