use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.
the class MessagingGatewaySupport method registerReplyMessageCorrelatorIfNecessary.
protected void registerReplyMessageCorrelatorIfNecessary() {
MessageChannel replyChannel = getReplyChannel();
if (replyChannel != null && this.replyMessageCorrelator == null) {
boolean shouldStartCorrelator;
synchronized (this.replyMessageCorrelatorMonitor) {
if (this.replyMessageCorrelator != null) {
return;
}
AbstractEndpoint correlator;
BridgeHandler handler = new BridgeHandler();
if (getBeanFactory() != null) {
handler.setBeanFactory(getBeanFactory());
}
handler.afterPropertiesSet();
if (replyChannel instanceof SubscribableChannel) {
correlator = new EventDrivenConsumer((SubscribableChannel) replyChannel, handler);
} else if (replyChannel instanceof PollableChannel) {
PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChannel, handler);
endpoint.setBeanFactory(getBeanFactory());
endpoint.setReceiveTimeout(this.replyTimeout);
endpoint.afterPropertiesSet();
correlator = endpoint;
} else if (replyChannel instanceof ReactiveStreamsSubscribableChannel) {
ReactiveStreamsConsumer endpoint = new ReactiveStreamsConsumer(replyChannel, (Subscriber<Message<?>>) handler);
endpoint.afterPropertiesSet();
correlator = endpoint;
} else {
throw new MessagingException("Unsupported 'replyChannel' type [" + replyChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
}
this.replyMessageCorrelator = correlator;
shouldStartCorrelator = true;
}
if (shouldStartCorrelator && isRunning()) {
if (isRunning()) {
this.replyMessageCorrelator.start();
}
}
}
}
use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.
the class AbstractMethodAnnotationPostProcessor method doCreateEndpoint.
protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel, List<Annotation> annotations) {
AbstractEndpoint endpoint;
if (inputChannel instanceof PollableChannel) {
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) inputChannel, handler);
configurePollingEndpoint(pollingConsumer, annotations);
endpoint = pollingConsumer;
} else {
Poller[] pollers = MessagingAnnotationUtils.resolveAttribute(annotations, "poller", Poller[].class);
Assert.state(ObjectUtils.isEmpty(pollers), "A '@Poller' should not be specified for Annotation-based " + "endpoint, since '" + inputChannel + "' is a SubscribableChannel (not pollable).");
if (inputChannel instanceof Publisher) {
endpoint = new ReactiveStreamsConsumer(inputChannel, handler);
} else {
endpoint = new EventDrivenConsumer((SubscribableChannel) inputChannel, handler);
}
}
return endpoint;
}
use of org.springframework.integration.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.
the class ConsumerEndpointFactoryBean method initializeEndpoint.
@SuppressWarnings("unchecked")
private void initializeEndpoint() throws Exception {
synchronized (this.initializationMonitor) {
if (this.initialized) {
return;
}
MessageChannel channel = null;
if (StringUtils.hasText(this.inputChannelName)) {
channel = this.channelResolver.resolveDestination(this.inputChannelName);
}
if (this.inputChannel != null) {
channel = this.inputChannel;
}
Assert.state(channel != null, "one of inputChannelName or inputChannel is required");
if (channel instanceof SubscribableChannel) {
Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName + "', since '" + channel + "' is a SubscribableChannel (not pollable).");
this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
if (logger.isWarnEnabled() && Boolean.FALSE.equals(this.autoStartup) && channel instanceof FixedSubscriberChannel) {
logger.warn("'autoStartup=\"false\"' has no effect when using a FixedSubscriberChannel");
}
} else if (channel instanceof PollableChannel) {
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
if (this.pollerMetadata == null) {
this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName + "', and no default poller is available within the context.");
}
pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());
pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain());
pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll());
pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler());
pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout());
pollingConsumer.setTransactionSynchronizationFactory(this.pollerMetadata.getTransactionSynchronizationFactory());
pollingConsumer.setBeanClassLoader(this.beanClassLoader);
pollingConsumer.setBeanFactory(this.beanFactory);
this.endpoint = pollingConsumer;
} else {
this.endpoint = new ReactiveStreamsConsumer(channel, this.handler);
}
this.endpoint.setBeanName(this.beanName);
this.endpoint.setBeanFactory(this.beanFactory);
if (this.autoStartup != null) {
this.endpoint.setAutoStartup(this.autoStartup);
}
int phase = this.phase;
if (!this.isPhaseSet && this.endpoint instanceof PollingConsumer) {
phase = Integer.MAX_VALUE / 2;
}
this.endpoint.setPhase(phase);
this.endpoint.setRole(this.role);
if (this.taskScheduler != null) {
this.endpoint.setTaskScheduler(this.taskScheduler);
}
this.endpoint.afterPropertiesSet();
this.initialized = true;
}
}
use of org.springframework.integration.endpoint.ReactiveStreamsConsumer 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.endpoint.ReactiveStreamsConsumer in project spring-integration by spring-projects.
the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerPollableChannel.
@Test
@SuppressWarnings("unchecked")
public void testReactiveStreamsConsumerPollableChannel() throws InterruptedException {
QueueChannel testChannel = new QueueChannel();
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();
testChannel.send(testMessage);
reactiveConsumer.start();
verify(testSubscriber, times(2)).onSubscribe(subscriptionArgumentCaptor.capture());
subscription = subscriptionArgumentCaptor.getValue();
subscription.request(2);
Message<?> testMessage2 = new GenericMessage<>("test2");
testChannel.send(testMessage2);
message = messages.poll(10, TimeUnit.SECONDS);
assertSame(testMessage, message);
message = messages.poll(10, TimeUnit.SECONDS);
assertSame(testMessage2, message);
verify(testSubscriber, never()).onError(any(Throwable.class));
verify(testSubscriber, never()).onComplete();
assertTrue(messages.isEmpty());
}
Aggregations