Search in sources :

Example 1 with ConsumerDestination

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;
}
Also used : AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ErrorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy) Lifecycle(org.springframework.context.Lifecycle) ConsumerDestination(org.springframework.cloud.stream.provisioning.ConsumerDestination)

Example 2 with ConsumerDestination

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

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