Search in sources :

Example 1 with ProvisioningException

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;
}
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)

Example 2 with ProvisioningException

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

Aggregations

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