Search in sources :

Example 1 with PublishSubscribeChannel

use of org.springframework.integration.channel.PublishSubscribeChannel in project spring-cloud-stream by spring-cloud.

the class TestChannelBinderProvisioner method provisionDestination.

private SubscribableChannel provisionDestination(String name, boolean pubSub) {
    String destinationName = name + ".destination";
    SubscribableChannel destination = this.provisionedDestinations.get(destinationName);
    if (destination == null) {
        destination = pubSub ? new PublishSubscribeChannel() : new DirectChannel();
        ((AbstractMessageChannel) destination).setBeanName(destinationName);
        ((AbstractMessageChannel) destination).setComponentName(destinationName);
        this.provisionedDestinations.put(destinationName, destination);
    }
    return destination;
}
Also used : PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel)

Example 2 with PublishSubscribeChannel

use of org.springframework.integration.channel.PublishSubscribeChannel in project spring-cloud-stream by spring-cloud.

the class AbstractMessageChannelBinder method registerErrorInfrastructure.

/**
 * 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.
 * @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;
}
Also used : PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) BridgeHandler(org.springframework.integration.handler.BridgeHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 3 with PublishSubscribeChannel

use of org.springframework.integration.channel.PublishSubscribeChannel in project spring-integration by spring-projects.

the class MessagingGatewayTests method validateErrorChannelWithSuccessfulReply.

// should not fail but it does now
@Test
public void validateErrorChannelWithSuccessfulReply() {
    TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    testApplicationContext.refresh();
    DirectChannel reqChannel = new DirectChannel();
    reqChannel.subscribe(message -> {
        throw new RuntimeException("ooops");
    });
    PublishSubscribeChannel errorChannel = new PublishSubscribeChannel();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new MyOneWayErrorService());
    handler.setBeanFactory(testApplicationContext);
    handler.afterPropertiesSet();
    errorChannel.subscribe(handler);
    this.messagingGateway = new MessagingGatewaySupport() {
    };
    this.messagingGateway.setRequestChannel(reqChannel);
    this.messagingGateway.setErrorChannel(errorChannel);
    this.messagingGateway.setReplyChannel(null);
    this.messagingGateway.setBeanFactory(mock(BeanFactory.class));
    this.messagingGateway.afterPropertiesSet();
    this.messagingGateway.start();
    this.messagingGateway.send("hello");
    testApplicationContext.close();
}
Also used : TestUtils(org.springframework.integration.test.util.TestUtils) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 4 with PublishSubscribeChannel

use of org.springframework.integration.channel.PublishSubscribeChannel in project spring-integration by spring-projects.

the class DefaultConfiguringBeanFactoryPostProcessor method registerErrorChannel.

/**
 * Register an error channel in the given BeanDefinitionRegistry.
 */
private void registerErrorChannel(BeanDefinitionRegistry registry) {
    if (this.logger.isInfoEnabled()) {
        this.logger.info("No bean named '" + IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME + "' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.");
    }
    registry.registerBeanDefinition(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, new RootBeanDefinition(PublishSubscribeChannel.class));
    BeanDefinition loggingHandler = BeanDefinitionBuilder.genericBeanDefinition(LoggingHandler.class).addConstructorArgValue("ERROR").getBeanDefinition();
    String errorLoggerBeanName = ERROR_LOGGER_BEAN_NAME + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX;
    registry.registerBeanDefinition(errorLoggerBeanName, loggingHandler);
    BeanDefinitionBuilder loggingEndpointBuilder = BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class).addPropertyReference("handler", errorLoggerBeanName).addPropertyValue("inputChannelName", IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
    BeanComponentDefinition componentDefinition = new BeanComponentDefinition(loggingEndpointBuilder.getBeanDefinition(), ERROR_LOGGER_BEAN_NAME);
    BeanDefinitionReaderUtils.registerBeanDefinition(componentDefinition, registry);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with PublishSubscribeChannel

use of org.springframework.integration.channel.PublishSubscribeChannel in project spring-integration by spring-projects.

the class PublishSubscribeChannelParserTests method applySequenceEnabledWithTaskExecutor.

@Test
public void applySequenceEnabledWithTaskExecutor() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("publishSubscribeChannelParserTests.xml", this.getClass());
    PublishSubscribeChannel channel = (PublishSubscribeChannel) context.getBean("channelWithApplySequenceEnabledAndTaskExecutor");
    DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
    BroadcastingDispatcher dispatcher = (BroadcastingDispatcher) accessor.getPropertyValue("dispatcher");
    DirectFieldAccessor dispatcherAccessor = new DirectFieldAccessor(dispatcher);
    assertTrue((Boolean) dispatcherAccessor.getPropertyValue("applySequence"));
    Executor executor = (Executor) dispatcherAccessor.getPropertyValue("executor");
    assertNotNull(executor);
    assertEquals(ErrorHandlingTaskExecutor.class, executor.getClass());
    DirectFieldAccessor executorAccessor = new DirectFieldAccessor(executor);
    Executor innerExecutor = (Executor) executorAccessor.getPropertyValue("executor");
    assertEquals(context.getBean("pool"), innerExecutor);
    context.close();
}
Also used : ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) Executor(java.util.concurrent.Executor) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BroadcastingDispatcher(org.springframework.integration.dispatcher.BroadcastingDispatcher) Test(org.junit.Test)

Aggregations

PublishSubscribeChannel (org.springframework.integration.channel.PublishSubscribeChannel)16 Test (org.junit.Test)13 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)7 BroadcastingDispatcher (org.springframework.integration.dispatcher.BroadcastingDispatcher)6 DirectChannel (org.springframework.integration.channel.DirectChannel)5 Executor (java.util.concurrent.Executor)3 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)3 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)3 ErrorHandlingTaskExecutor (org.springframework.integration.util.ErrorHandlingTaskExecutor)3 BeanFactory (org.springframework.beans.factory.BeanFactory)2 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)2 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1