use of org.springframework.cloud.stream.provisioning.ConsumerDestination in project spring-cloud-stream by spring-cloud.
the class AbstractMessageChannelBinder method bindPollableConsumer.
@Override
public Binding<PollableSource<MessageHandler>> bindPollableConsumer(String name, String group, final PollableSource<MessageHandler> inboundBindTarget, C properties) {
Assert.isInstanceOf(DefaultPollableMessageSource.class, inboundBindTarget);
DefaultPollableMessageSource bindingTarget = (DefaultPollableMessageSource) inboundBindTarget;
ConsumerDestination destination = this.provisioningProvider.provisionConsumerDestination(name, group, properties);
if (HeaderMode.embeddedHeaders.equals(properties.getHeaderMode())) {
bindingTarget.addInterceptor(0, this.embeddedHeadersChannelInterceptor);
}
final PolledConsumerResources resources = createPolledConsumerResources(name, group, destination, properties);
bindingTarget.setSource(resources.getSource());
if (resources.getErrorInfrastructure() != null) {
if (resources.getErrorInfrastructure().getErrorChannel() != null) {
bindingTarget.setErrorChannel(resources.getErrorInfrastructure().getErrorChannel());
}
ErrorMessageStrategy ems = getErrorMessageStrategy();
if (ems != null) {
bindingTarget.setErrorMessageStrategy(ems);
}
}
if (properties.getMaxAttempts() > 1) {
bindingTarget.setRetryTemplate(buildRetryTemplate(properties));
bindingTarget.setRecoveryCallback(getPolledConsumerRecoveryCallback(resources.getErrorInfrastructure(), properties));
}
postProcessPollableSource(bindingTarget);
if (resources.getSource() instanceof Lifecycle) {
((Lifecycle) resources.getSource()).start();
}
Binding<PollableSource<MessageHandler>> binding = new DefaultBinding<PollableSource<MessageHandler>>(name, group, inboundBindTarget, resources.getSource() instanceof Lifecycle ? (Lifecycle) resources.getSource() : null) {
@Override
public Map<String, Object> getExtendedInfo() {
return doGetExtendedInfo(destination, properties);
}
@Override
public void afterUnbind() {
afterUnbindConsumer(destination, this.group, properties);
destroyErrorInfrastructure(destination, group, properties);
}
};
doPublishEvent(new BindingCreatedEvent(binding));
return binding;
}
use of org.springframework.cloud.stream.provisioning.ConsumerDestination in project spring-cloud-stream by spring-cloud.
the class AbstractMessageChannelBinder method doBindConsumer.
/**
* Binds an inbound channel to a given destination. The implementation delegates to
* {@link ProvisioningProvider#provisionConsumerDestination(String, String, ConsumerProperties)}
* and
* {@link #createConsumerEndpoint(ConsumerDestination, String, ConsumerProperties)}
* for handling middleware-specific logic. If the returned consumer endpoint is an
* {@link InitializingBean} then {@link InitializingBean#afterPropertiesSet()} will be
* called on it. Similarly, if the returned consumer endpoint is a {@link Lifecycle},
* then {@link Lifecycle#start()} will be called on it.
*
* @param name the name of the destination
* @param group the consumer group
* @param inputChannel the channel to be bound
* @param properties the {@link ConsumerProperties} of the binding
* @return the Binding for the channel
* @throws BinderException on internal errors during binding
*/
@Override
public final Binding<MessageChannel> doBindConsumer(String name, String group, MessageChannel inputChannel, final C properties) throws BinderException {
MessageProducer consumerEndpoint = null;
try {
ConsumerDestination destination = this.provisioningProvider.provisionConsumerDestination(name, group, properties);
if (HeaderMode.embeddedHeaders.equals(properties.getHeaderMode())) {
enhanceMessageChannel(inputChannel);
}
consumerEndpoint = createConsumerEndpoint(destination, group, properties);
consumerEndpoint.setOutputChannel(inputChannel);
if (consumerEndpoint instanceof InitializingBean) {
((InitializingBean) consumerEndpoint).afterPropertiesSet();
}
if (consumerEndpoint instanceof Lifecycle) {
((Lifecycle) consumerEndpoint).start();
}
Binding<MessageChannel> binding = new DefaultBinding<MessageChannel>(name, group, inputChannel, consumerEndpoint instanceof Lifecycle ? (Lifecycle) consumerEndpoint : null) {
@Override
public Map<String, Object> getExtendedInfo() {
return doGetExtendedInfo(destination, properties);
}
@Override
protected void afterUnbind() {
try {
if (getEndpoint() instanceof DisposableBean) {
((DisposableBean) getEndpoint()).destroy();
}
} catch (Exception e) {
AbstractMessageChannelBinder.this.logger.error("Exception thrown while unbinding " + toString(), e);
}
afterUnbindConsumer(destination, this.group, properties);
destroyErrorInfrastructure(destination, group, properties);
}
};
doPublishEvent(new BindingCreatedEvent(binding));
return binding;
} catch (Exception e) {
if (consumerEndpoint instanceof Lifecycle) {
((Lifecycle) consumerEndpoint).stop();
}
if (e instanceof BinderException) {
throw (BinderException) e;
} else if (e instanceof ProvisioningException) {
throw (ProvisioningException) e;
} else {
throw new BinderException("Exception thrown while starting consumer: ", e);
}
}
}
Aggregations