use of org.springframework.cloud.stream.provisioning.ProvisioningException in project spring-cloud-stream by spring-cloud.
the class AbstractMessageChannelBinder method doBindProducer.
/**
* Binds an outbound channel to a given destination. The implementation delegates to
* {@link ProvisioningProvider#provisionProducerDestination(String, ProducerProperties)}
* and {@link #createProducerMessageHandler(ProducerDestination, ProducerProperties, MessageChannel)}
* for handling the middleware specific logic. If the returned producer message
* handler is an {@link InitializingBean} then
* {@link InitializingBean#afterPropertiesSet()} will be called on it. Similarly, if
* the returned producer message handler endpoint is a {@link Lifecycle}, then
* {@link Lifecycle#start()} will be called on it.
*
* @param destination the name of the destination
* @param outputChannel the channel to be bound
* @param producerProperties the {@link ProducerProperties} of the binding
* @return the Binding for the channel
* @throws BinderException on internal errors during binding
*/
@Override
public final Binding<MessageChannel> doBindProducer(final String destination, MessageChannel outputChannel, final P producerProperties) throws BinderException {
Assert.isInstanceOf(SubscribableChannel.class, outputChannel, "Binding is supported only for SubscribableChannel instances");
final MessageHandler producerMessageHandler;
final ProducerDestination producerDestination;
try {
producerDestination = this.provisioningProvider.provisionProducerDestination(destination, producerProperties);
SubscribableChannel errorChannel = producerProperties.isErrorChannelEnabled() ? registerErrorInfrastructure(producerDestination) : null;
producerMessageHandler = createProducerMessageHandler(producerDestination, producerProperties, errorChannel);
if (producerMessageHandler instanceof InitializingBean) {
((InitializingBean) producerMessageHandler).afterPropertiesSet();
}
} catch (Exception e) {
if (e instanceof BinderException) {
throw (BinderException) e;
} else if (e instanceof ProvisioningException) {
throw (ProvisioningException) e;
} else {
throw new BinderException("Exception thrown while building outbound endpoint", e);
}
}
if (producerMessageHandler instanceof Lifecycle) {
((Lifecycle) producerMessageHandler).start();
}
postProcessOutputChannel(outputChannel, producerProperties);
((SubscribableChannel) outputChannel).subscribe(new SendingHandler(producerMessageHandler, HeaderMode.embeddedHeaders.equals(producerProperties.getHeaderMode()), this.headersToEmbed, producerProperties.isUseNativeEncoding()));
Binding<MessageChannel> binding = new DefaultBinding<MessageChannel>(destination, null, outputChannel, producerMessageHandler instanceof Lifecycle ? (Lifecycle) producerMessageHandler : null) {
@Override
public Map<String, Object> getExtendedInfo() {
return doGetExtendedInfo(destination, producerProperties);
}
@Override
public void afterUnbind() {
try {
destroyErrorInfrastructure(producerDestination);
if (producerMessageHandler instanceof DisposableBean) {
((DisposableBean) producerMessageHandler).destroy();
}
} catch (Exception e) {
AbstractMessageChannelBinder.this.logger.error("Exception thrown while unbinding " + toString(), e);
}
afterUnbindProducer(producerDestination, producerProperties);
}
};
doPublishEvent(new BindingCreatedEvent(binding));
return binding;
}
use of org.springframework.cloud.stream.provisioning.ProvisioningException 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