Example usage for org.springframework.integration.core MessageProducer setOutputChannel

List of usage examples for org.springframework.integration.core MessageProducer setOutputChannel

Introduction

In this page you can find the example usage for org.springframework.integration.core MessageProducer setOutputChannel.

Prototype

void setOutputChannel(MessageChannel outputChannel);

Source Link

Document

Specify the MessageChannel to which produced Messages should be sent.

Usage

From source file:org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.java

/**
 * Binds an inbound channel to a given destination. The implementation delegates to
 * {@link ProvisioningProvider#provisionConsumerDestination(String, String, ConsumerProperties)}
 * and//from ww w  . j a v  a2 s  .  co  m
 * {@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();
        }

        return new DefaultBinding<MessageChannel>(name, group, inputChannel,
                consumerEndpoint instanceof Lifecycle ? (Lifecycle) consumerEndpoint : null) {

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

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