Search in sources :

Example 6 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware 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);
    MessageSource<?> messageSource = resources.getSource();
    if (messageSource instanceof BeanFactoryAware) {
        ((BeanFactoryAware) messageSource).setBeanFactory(getApplicationContext().getBeanFactory());
    }
    bindingTarget.setSource(messageSource);
    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 (properties.isAutoStartup() && 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 boolean isInput() {
            return true;
        }

        @Override
        public void afterUnbind() {
            afterUnbindConsumer(destination, this.group, properties);
            destroyErrorInfrastructure(destination, this.group, properties);
        }
    };
    doPublishEvent(new BindingCreatedEvent(binding));
    return binding;
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) ErrorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy) Lifecycle(org.springframework.context.Lifecycle) ConsumerDestination(org.springframework.cloud.stream.provisioning.ConsumerDestination) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware)

Example 7 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware in project spring-integration by spring-projects.

the class AbstractCorrelatingMessageHandler method onInit.

@Override
protected void onInit() throws Exception {
    super.onInit();
    Assert.state(!(this.discardChannelName != null && this.discardChannel != null), "'discardChannelName' and 'discardChannel' are mutually exclusive.");
    BeanFactory beanFactory = this.getBeanFactory();
    if (beanFactory != null) {
        if (this.outputProcessor instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.outputProcessor).setBeanFactory(beanFactory);
        }
        if (this.correlationStrategy instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.correlationStrategy).setBeanFactory(beanFactory);
        }
        if (this.releaseStrategy instanceof BeanFactoryAware) {
            ((BeanFactoryAware) this.releaseStrategy).setBeanFactory(beanFactory);
        }
    }
    if (this.discardChannel == null) {
        this.discardChannel = new NullChannel();
    }
    if (this.releasePartialSequences) {
        Assert.isInstanceOf(SequenceSizeReleaseStrategy.class, this.releaseStrategy, "Release strategy of type [" + this.releaseStrategy.getClass().getSimpleName() + "] cannot release partial sequences. Use a SequenceSizeReleaseStrategy instead.");
        ((SequenceSizeReleaseStrategy) this.releaseStrategy).setReleasePartialSequences(this.releasePartialSequences);
    }
    if (this.evaluationContext == null) {
        this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
    }
    if (this.sequenceAware) {
        this.logger.warn("Using a SequenceSizeReleaseStrategy with large groups may not perform well, consider " + "using a SimpleSequenceSizeReleaseStrategy");
    }
    /*
		 * Disallow any further changes to the lock registry
		 * (checked in the setter).
		 */
    this.lockRegistrySet = true;
    this.forceReleaseProcessor = createGroupTimeoutProcessor();
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) BeanFactory(org.springframework.beans.factory.BeanFactory) NullChannel(org.springframework.integration.channel.NullChannel)

Example 8 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware in project spring-integration by spring-projects.

the class FileWritingMessageHandler method doInit.

@Override
protected void doInit() {
    this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
    if (this.destinationDirectoryExpression instanceof LiteralExpression) {
        final File directory = new File(this.destinationDirectoryExpression.getValue(this.evaluationContext, null, String.class));
        validateDestinationDirectory(directory, this.autoCreateDirectory);
    }
    Assert.state(!(this.temporaryFileSuffixSet && (FileExistsMode.APPEND.equals(this.fileExistsMode) || FileExistsMode.APPEND_NO_FLUSH.equals(this.fileExistsMode))), "'temporaryFileSuffix' can not be set when appending to an existing file");
    if (!this.fileNameGeneratorSet && this.fileNameGenerator instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.fileNameGenerator).setBeanFactory(getBeanFactory());
    }
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) LiteralExpression(org.springframework.expression.common.LiteralExpression) File(java.io.File)

Example 9 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware in project spring-integration by spring-projects.

the class RedisInboundChannelAdapter method onInit.

@Override
protected void onInit() {
    super.onInit();
    boolean hasTopics = false;
    if (this.topics != null) {
        Assert.noNullElements(this.topics, "'topics' may not contain null elements.");
        hasTopics = true;
    }
    boolean hasPatterns = false;
    if (this.topicPatterns != null) {
        Assert.noNullElements(this.topicPatterns, "'topicPatterns' may not contain null elements.");
        hasPatterns = true;
    }
    Assert.state(hasTopics || hasPatterns, "at least one topic or topic pattern is required for subscription.");
    if (this.messageConverter instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.messageConverter).setBeanFactory(this.getBeanFactory());
    }
    MessageListenerDelegate delegate = new MessageListenerDelegate();
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
    adapter.setSerializer(this.serializer);
    List<Topic> topicList = new ArrayList<Topic>();
    if (hasTopics) {
        for (String topic : this.topics) {
            topicList.add(new ChannelTopic(topic));
        }
    }
    if (hasPatterns) {
        for (String pattern : this.topicPatterns) {
            topicList.add(new PatternTopic(pattern));
        }
    }
    adapter.afterPropertiesSet();
    this.container.addMessageListener(adapter, topicList);
    this.container.afterPropertiesSet();
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) PatternTopic(org.springframework.data.redis.listener.PatternTopic) ArrayList(java.util.ArrayList) Topic(org.springframework.data.redis.listener.Topic) PatternTopic(org.springframework.data.redis.listener.PatternTopic) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic)

Example 10 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware in project spring-integration by spring-projects.

the class SubscribableRedisChannel method onInit.

@Override
public void onInit() throws Exception {
    if (this.initialized) {
        return;
    }
    super.onInit();
    if (this.maxSubscribers == null) {
        Integer maxSubscribers = getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS, Integer.class);
        this.setMaxSubscribers(maxSubscribers);
    }
    if (this.messageConverter == null) {
        this.messageConverter = new SimpleMessageConverter();
    }
    if (this.messageConverter instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.messageConverter).setBeanFactory(this.getBeanFactory());
    }
    this.container.setConnectionFactory(this.connectionFactory);
    if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
        ErrorHandler errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(this.getBeanFactory()));
        this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, errorHandler);
    }
    this.container.setTaskExecutor(this.taskExecutor);
    MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageListenerDelegate());
    adapter.setSerializer(this.serializer);
    adapter.afterPropertiesSet();
    this.container.addMessageListener(adapter, new ChannelTopic(this.topicName));
    this.container.afterPropertiesSet();
    this.dispatcher.setBeanFactory(this.getBeanFactory());
    this.initialized = true;
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) ErrorHandler(org.springframework.util.ErrorHandler) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) SimpleMessageConverter(org.springframework.integration.support.converter.SimpleMessageConverter) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor)

Aggregations

BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)11 BeanFactory (org.springframework.beans.factory.BeanFactory)2 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 BeanNameAware (org.springframework.beans.factory.BeanNameAware)2 InitializingBean (org.springframework.beans.factory.InitializingBean)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)2 MessageListenerAdapter (org.springframework.data.redis.listener.adapter.MessageListenerAdapter)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 BeansException (org.springframework.beans.BeansException)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 ConsumerDestination (org.springframework.cloud.stream.provisioning.ConsumerDestination)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 ApplicationEventPublisherAware (org.springframework.context.ApplicationEventPublisherAware)1