Example usage for org.springframework.integration.handler BridgeHandler BridgeHandler

List of usage examples for org.springframework.integration.handler BridgeHandler BridgeHandler

Introduction

In this page you can find the example usage for org.springframework.integration.handler BridgeHandler BridgeHandler.

Prototype

BridgeHandler

Source Link

Usage

From source file:com.mine.cassandra.sink.CassandraSinkConfiguration.java

@Bean
@Primary//  w  w  w.jav a2  s  .co  m
@ServiceActivator(inputChannel = Sink.INPUT)
public MessageHandler bridgeMessageHandler() {
    AbstractMessageProducingHandler messageHandler;
    if (StringUtils.hasText(this.cassandraSinkProperties.getIngestQuery())) {
        messageHandler = new MessageTransformingHandler(
                new PayloadToMatrixTransformer(this.cassandraSinkProperties.getIngestQuery()));
    } else {
        messageHandler = new BridgeHandler();
    }
    messageHandler.setOutputChannel(toSink());
    return messageHandler;
}

From source file:org.grails.plugin.platform.events.registry.SpringIntegrationEventsRegistry.java

private void initServiceActivatingHandler(ServiceActivatingHandler serviceActivatingHandler,
        ListenerId listener, String topic) {
    if (topic == null || topic.isEmpty()) {
        throw new RuntimeException("topic name must not be null or empty");
    }/*from w ww .  jav a2s  .c  o m*/

    String callBackId = listener.toString();
    serviceActivatingHandler.setBeanName(callBackId);
    serviceActivatingHandler.setChannelResolver(resolver);
    serviceActivatingHandler.setRequiresReply(true);
    serviceActivatingHandler.setOutputChannel(outputChannel);
    String beanIdBase = listener.getClassName();
    int counter = 0;
    String beanId;

    do {
        counter++;
        beanId = beanIdBase + BeanDefinitionReaderUtils.GENERATED_BEAN_NAME_SEPARATOR + counter;
    } while (beanFactory.containsBean(beanId));

    beanFactory.registerSingleton(beanId, serviceActivatingHandler);
    serviceActivatingHandler.afterPropertiesSet();

    SubscribableChannel bridgeChannel = null;
    SubscribableChannel channel = null;
    String channelName = listener.getTopic();

    try {
        bridgeChannel = ctx.getBean(channelName, SubscribableChannel.class);
    } catch (BeansException be) {
        log.debug("no overriding/existing channel found " + be.getMessage());
    }

    if (bridgeChannel == null
            || !bridgeChannel.getClass().isAssignableFrom(GrailsPublishSubscribeChannel.class)) {
        if (bridgeChannel != null) {
            channelName += "-plugin";
        }
        channel = createChannel(channelName);
    } else {
        channel = bridgeChannel;
    }

    channel.subscribe(serviceActivatingHandler);

    if (bridgeChannel != null
            && !bridgeChannel.getClass().isAssignableFrom(GrailsPublishSubscribeChannel.class)) {
        BridgeHandler bridgeHandler = new BridgeHandler();
        bridgeHandler.setOutputChannel(channel);
        bridgeChannel.subscribe(bridgeHandler);
    }

}

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

/**
 * Register an error channel for the destination when an async send error is received.
 * Bridge the channel to the global error channel (if present).
 * @param destination the destination.//from  ww w.j a  v a 2 s. c  o m
 * @return the channel.
 */
private SubscribableChannel registerErrorInfrastructure(ProducerDestination destination) {
    ConfigurableListableBeanFactory beanFactory = getApplicationContext().getBeanFactory();
    String errorChannelName = errorsBaseName(destination);
    SubscribableChannel errorChannel = null;
    if (getApplicationContext().containsBean(errorChannelName)) {
        Object errorChannelObject = getApplicationContext().getBean(errorChannelName);
        if (!(errorChannelObject instanceof SubscribableChannel)) {
            throw new IllegalStateException(
                    "Error channel '" + errorChannelName + "' must be a SubscribableChannel");
        }
        errorChannel = (SubscribableChannel) errorChannelObject;
    } else {
        errorChannel = new PublishSubscribeChannel();
        beanFactory.registerSingleton(errorChannelName, errorChannel);
        errorChannel = (PublishSubscribeChannel) beanFactory.initializeBean(errorChannel, errorChannelName);
    }
    MessageChannel defaultErrorChannel = null;
    if (getApplicationContext().containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) {
        defaultErrorChannel = getApplicationContext().getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME,
                MessageChannel.class);
    }
    if (defaultErrorChannel != null) {
        BridgeHandler errorBridge = new BridgeHandler();
        errorBridge.setOutputChannel(defaultErrorChannel);
        errorChannel.subscribe(errorBridge);
        String errorBridgeHandlerName = getErrorBridgeName(destination);
        beanFactory.registerSingleton(errorBridgeHandlerName, errorBridge);
        beanFactory.initializeBean(errorBridge, errorBridgeHandlerName);
    }
    return errorChannel;
}

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

/**
 * Build an errorChannelRecoverer that writes to a pub/sub channel for the destination
 * when an exception is thrown to a consumer.
 * @param destination the destination.//  ww  w.  j a va  2 s.co m
 * @param group the group.
 * @param consumerProperties the properties.
 * @param true if this is for a polled consumer.
 * @return the ErrorInfrastructure which is a holder for the error channel, the recoverer and the
 * message handler that is subscribed to the channel.
 */
protected final ErrorInfrastructure registerErrorInfrastructure(ConsumerDestination destination, String group,
        C consumerProperties, boolean polled) {

    ErrorMessageStrategy errorMessageStrategy = getErrorMessageStrategy();
    ConfigurableListableBeanFactory beanFactory = getApplicationContext().getBeanFactory();
    String errorChannelName = errorsBaseName(destination, group, consumerProperties);
    SubscribableChannel errorChannel = null;
    if (getApplicationContext().containsBean(errorChannelName)) {
        Object errorChannelObject = getApplicationContext().getBean(errorChannelName);
        if (!(errorChannelObject instanceof SubscribableChannel)) {
            throw new IllegalStateException(
                    "Error channel '" + errorChannelName + "' must be a SubscribableChannel");
        }
        errorChannel = (SubscribableChannel) errorChannelObject;
    } else {
        errorChannel = new BinderErrorChannel();
        beanFactory.registerSingleton(errorChannelName, errorChannel);
        errorChannel = (LastSubscriberAwareChannel) beanFactory.initializeBean(errorChannel, errorChannelName);
    }
    ErrorMessageSendingRecoverer recoverer;
    if (errorMessageStrategy == null) {
        recoverer = new ErrorMessageSendingRecoverer(errorChannel);
    } else {
        recoverer = new ErrorMessageSendingRecoverer(errorChannel, errorMessageStrategy);
    }
    String recovererBeanName = getErrorRecovererName(destination, group, consumerProperties);
    beanFactory.registerSingleton(recovererBeanName, recoverer);
    beanFactory.initializeBean(recoverer, recovererBeanName);
    MessageHandler handler;
    if (polled) {
        handler = getPolledConsumerErrorMessageHandler(destination, group, consumerProperties);
    } else {
        handler = getErrorMessageHandler(destination, group, consumerProperties);
    }
    MessageChannel defaultErrorChannel = null;
    if (getApplicationContext().containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) {
        defaultErrorChannel = getApplicationContext().getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME,
                MessageChannel.class);
    }
    if (handler == null && errorChannel instanceof LastSubscriberAwareChannel) {
        handler = getDefaultErrorMessageHandler((LastSubscriberAwareChannel) errorChannel,
                defaultErrorChannel != null);
    }
    String errorMessageHandlerName = getErrorMessageHandlerName(destination, group, consumerProperties);
    if (handler != null) {
        beanFactory.registerSingleton(errorMessageHandlerName, handler);
        beanFactory.initializeBean(handler, errorMessageHandlerName);
        errorChannel.subscribe(handler);
    }
    if (defaultErrorChannel != null) {
        BridgeHandler errorBridge = new BridgeHandler();
        errorBridge.setOutputChannel(defaultErrorChannel);
        errorChannel.subscribe(errorBridge);
        String errorBridgeHandlerName = getErrorBridgeName(destination, group, consumerProperties);
        beanFactory.registerSingleton(errorBridgeHandlerName, errorBridge);
        beanFactory.initializeBean(errorBridge, errorBridgeHandlerName);
    }
    return new ErrorInfrastructure(errorChannel, recoverer, handler);
}

From source file:org.springframework.integration.flow.config.FlowUtils.java

/**
 * Create a bridge/*from  w  ww .  j av a 2 s .co m*/
 * 
 * @param inputChannel
 * @param outputChannel
 */

public static void bridgeChannels(SubscribableChannel inputChannel, MessageChannel outputChannel) {
    BridgeHandler bridgeHandler = new BridgeHandler();
    bridgeHandler.setOutputChannel(outputChannel);
    inputChannel.subscribe(bridgeHandler);
}

From source file:org.springframework.xd.module.CompositeModule.java

@Override
public void initialize() {
    List<AbstractEndpoint> endpoints = new ArrayList<AbstractEndpoint>();
    MessageChannel previousOutputChannel = null;
    for (int i = 0; i < this.modules.size(); i++) {
        SimpleModule module = this.modules.get(i);
        module.initialize();/*w w w  .j a  v  a  2  s.com*/
        MessageChannel inputChannel = module.getComponent("input", MessageChannel.class);
        MessageChannel outputChannel = module.getComponent("output", MessageChannel.class);
        if (i == 0 && inputChannel != null) {
            // this will act as THE input for the composite module
            // if the first module has no input, the composite is a source
            this.context.getBeanFactory().registerSingleton("input", inputChannel);
        }
        if (i > 0) {
            // first module MAY have 'input', all others MUST
            Assert.notNull(inputChannel, "each module after the first must provide 'input'");
        }
        if (previousOutputChannel != null) {
            BridgeHandler handler = new BridgeHandler();
            handler.setOutputChannel(inputChannel);
            handler.afterPropertiesSet();
            ConsumerEndpointFactoryBean bridgeFactoryBean = new ConsumerEndpointFactoryBean();
            bridgeFactoryBean.setInputChannel(previousOutputChannel);
            bridgeFactoryBean.setHandler(handler);
            try {
                // TODO: might not be necessary to pass this context, but the FB requires non-null
                bridgeFactoryBean.setBeanFactory(this.context.getBeanFactory());
                bridgeFactoryBean.afterPropertiesSet();
                AbstractEndpoint endpoint = bridgeFactoryBean.getObject();
                endpoints.add(endpoint);
                this.context.getBeanFactory().registerSingleton("bridge-" + i, endpoint);
            } catch (Exception e) {
                throw new IllegalStateException("failed to start bridge for CompositeModule", e);
            }
        }
        if (i < this.modules.size() - 1) {
            // last module MAY have 'output', all others MUST
            Assert.notNull(outputChannel, "each module before the last must provide 'output'");
        }
        previousOutputChannel = outputChannel;
        if (i == this.modules.size() - 1 && outputChannel != null) {
            // this will act as THE output for the composite module
            // if the final module has no outputChannel, the composite is a sink
            this.context.getBeanFactory().registerSingleton("output", outputChannel);
        }
    }
    for (int i = endpoints.size() - 1; i >= 0; i--) {
        endpoints.get(i).start();
    }
    initContext();
    if (logger.isInfoEnabled()) {
        logger.info("initialized module: " + this.toString());
    }
}

From source file:org.springframework.xd.module.core.CompositeModule.java

@Override
public void initialize() {
    List<AbstractEndpoint> endpoints = new ArrayList<AbstractEndpoint>();
    MessageChannel previousOutputChannel = null;
    for (int i = 0; i < this.modules.size(); i++) {
        Module module = this.modules.get(i);
        module.initialize();/*from   www .  j a v  a 2 s.c  om*/
        MessageChannel inputChannel = module.getComponent("input", MessageChannel.class);
        MessageChannel outputChannel = module.getComponent("output", MessageChannel.class);
        if (i == 0 && inputChannel != null) {
            // this will act as THE input for the composite module
            // if the first module has no input, the composite is a source
            this.context.getBeanFactory().registerSingleton("input", inputChannel);
        }
        if (i > 0) {
            // first module MAY have 'input', all others MUST
            Assert.notNull(inputChannel, "each module after the first must provide 'input'");
        }
        if (previousOutputChannel != null) {
            BridgeHandler handler = new BridgeHandler();
            handler.setBeanFactory(this.context.getBeanFactory());
            handler.setOutputChannel(inputChannel);
            handler.afterPropertiesSet();
            ConsumerEndpointFactoryBean bridgeFactoryBean = new ConsumerEndpointFactoryBean();
            bridgeFactoryBean.setInputChannel(previousOutputChannel);
            bridgeFactoryBean.setHandler(handler);
            try {
                // TODO: might not be necessary to pass this context, but the FB requires non-null
                bridgeFactoryBean.setBeanFactory(this.context.getBeanFactory());
                bridgeFactoryBean.afterPropertiesSet();
                AbstractEndpoint endpoint = bridgeFactoryBean.getObject();
                endpoints.add(endpoint);
                this.context.getBeanFactory().registerSingleton("bridge-" + i, endpoint);
                endpoint.setComponentName("bridge-" + i);
                endpoint.afterPropertiesSet();
            } catch (Exception e) {
                throw new IllegalStateException("failed to start bridge for CompositeModule", e);
            }
        }
        if (i < this.modules.size() - 1) {
            // last module MAY have 'output', all others MUST
            Assert.notNull(outputChannel, "each module before the last must provide 'output'");
        }
        previousOutputChannel = outputChannel;
        if (i == this.modules.size() - 1 && outputChannel != null) {
            // this will act as THE output for the composite module
            // if the final module has no outputChannel, the composite is a sink
            this.context.getBeanFactory().registerSingleton("output", outputChannel);
        }
    }
    for (int i = endpoints.size() - 1; i >= 0; i--) {
        endpoints.get(i).start();
    }
    initContext();
    if (logger.isInfoEnabled()) {
        logger.info("initialized module: " + this.toString());
    }
}