use of org.springframework.cloud.stream.provisioning.ProducerDestination 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;
}
Aggregations