Search in sources :

Example 1 with ProducerDestination

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;
}
Also used : AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) Lifecycle(org.springframework.context.Lifecycle) ProvisioningException(org.springframework.cloud.stream.provisioning.ProvisioningException) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) ProvisioningException(org.springframework.cloud.stream.provisioning.ProvisioningException) DisposableBean(org.springframework.beans.factory.DisposableBean) InitializingBean(org.springframework.beans.factory.InitializingBean) ProducerDestination(org.springframework.cloud.stream.provisioning.ProducerDestination) SubscribableChannel(org.springframework.messaging.SubscribableChannel)

Aggregations

DisposableBean (org.springframework.beans.factory.DisposableBean)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 ProducerDestination (org.springframework.cloud.stream.provisioning.ProducerDestination)1 ProvisioningException (org.springframework.cloud.stream.provisioning.ProvisioningException)1 Lifecycle (org.springframework.context.Lifecycle)1 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)1 AbstractMessageHandler (org.springframework.integration.handler.AbstractMessageHandler)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageHandler (org.springframework.messaging.MessageHandler)1 SubscribableChannel (org.springframework.messaging.SubscribableChannel)1