本站消息

站长简介/公众号


站长简介:逗比程序员,理工宅男,前每日优鲜python全栈开发工程师,利用周末时间开发出本站,欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,java大神匠心打造,零基础java开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2021-05(19)

2021-06(53)

2021-07(6)

2021-08(53)

2021-09(46)

Dispatcher has no subscribers for channel排坑指南

发布于2021-05-29 22:11     阅读(726)     评论(0)     点赞(23)     收藏(2)


Dispatcher has no subscribers for channel排坑指南

通过Spring Cloud Stream解耦具体消息中间件,屏蔽掉不同中间件之间的差异,简化了消息中间件使用难度,便于切换不同的消息队列。带来这些便利的同时,也引入了不少问题,本文从源码角度结合实际使用场景分析异常Dispatcher has no subscribers for channel产生的原因以及解决方案

原因分析

由报错信息可知,产生报错的位置在UnicastingDispatcher类的doDispatch方法,此方法是由MessageChannel的send方法调用的。从代码分析可知doDispatch方法会获取消息对应的处理器,如果没有处理器处理消息,则会报Dispatcher has no subscribers。接下来我们需要去找初始化消息处理器的地方

private boolean doDispatch(Message<?> message) {
    if (tryOptimizedDispatch(message)) {
        return true;
    }
    boolean success = false;
    // 获取消息对应的处理器
    Iterator<MessageHandler> handlerIterator = this.getHandlerIterator(message);
    // 如果没有对应的处理器报错
    if (!handlerIterator.hasNext()) {
        throw new MessageDispatchingException(message, "Dispatcher has no subscribers");
    }
    List<RuntimeException> exceptions = new ArrayList<RuntimeException>();
    while (!success && handlerIterator.hasNext()) {
        MessageHandler handler = handlerIterator.next();
        try {
            handler.handleMessage(message);
            success = true; // we have a winner.
        }
        catch (Exception e) {
            @SuppressWarnings("deprecation")
            RuntimeException runtimeException = wrapExceptionIfNecessary(message, e);
            exceptions.add(runtimeException);
            this.handleExceptions(exceptions, message, !handlerIterator.hasNext());
        }
    }
    return success;
}

我们知道报错的原因是没有拿到消息的处理器,我们看一下getHandlerIterator方法获取处理器的逻辑。该方法会从this.getHandlers()获取处理器

private Iterator<MessageHandler> getHandlerIterator(Message<?> message) {
    if (this.loadBalancingStrategy != null) {
        return this.loadBalancingStrategy.getHandlerIterator(message, this.getHandlers());
    }
    return this.getHandlers().iterator();
}

查看getHandlers方法可知其获取的处理器其实是从OrderedAwareCopyOnWriteArraySet类型的属性handlers获取的,handlers的值是通过addHandler方法设置

protected Set<MessageHandler> getHandlers() {
    return this.handlers.asUnmodifiableSet();
}

private final OrderedAwareCopyOnWriteArraySet<MessageHandler> handlers =
			new OrderedAwareCopyOnWriteArraySet<MessageHandler>();
			
public synchronized boolean addHandler(MessageHandler handler) {
    Assert.notNull(handler, "handler must not be null");
    Assert.isTrue(this.handlers.size() < this.maxSubscribers, "Maximum subscribers exceeded");
    boolean added = this.handlers.add(handler);
    if (this.handlers.size() == 1) {
        this.theOneHandler = handler;
    }
    else {
        this.theOneHandler = null;
    }
    return added;
}

AbstractSubscribableChannel类的subscribe方法有调用addHandler方法,而subscribe方法调用的地方有很多,我们可以看到AbstractMessageChannelBinder类的doBindProducer方法中有去调用subscribe设置处理处理器。而doBindProducer实际工作就是在初始化发送消息的通道及绑定通道到对应消息中间件等事情

@Override
public boolean subscribe(MessageHandler handler) {
    MessageDispatcher dispatcher = getRequiredDispatcher();
    // 调用addHandler添加处理消息方法
    boolean added = dispatcher.addHandler(handler);
    adjustCounterIfNecessary(dispatcher, added ? 1 : 0);
    return added;
}

public final Binding<MessageChannel> doBindProducer(final String destination, MessageChannel outputChannel,
        final P producerProperties) throws BinderException {
    Assert.isInstanceOf(SubscribableChannel.class, outputChannel,
            "Binding is supported only for SubscribableChannel instances");
    final MessageHandler producerMessageHandler;
    final ProducerDestination producerDestination;
    try {
        producerDestination = this.provisioningProvider.provisionProducerDestination(destination,
                producerProperties);
        SubscribableChannel errorChannel = producerProperties.isErrorChannelEnabled()
                ? registerErrorInfrastructure(producerDestination) : null;
        producerMessageHandler = createProducerMessageHandler(producerDestination, producerProperties,
                errorChannel);
        if (producerMessageHandler instanceof InitializingBean) {
            ((InitializingBean) producerMessageHandler).afterPropertiesSet();
        }
    }
    catch (Exception e) {
        if (e instanceof BinderException) {
            throw (BinderException) e;
        }
        else if (e instanceof ProvisioningException) {
            throw (ProvisioningException) e;
        }
        else {
            throw new BinderException("Exception thrown while building outbound endpoint", e);
        }
    }
    if (producerMessageHandler instanceof Lifecycle) {
        ((Lifecycle) producerMessageHandler).start();
    }
    postProcessOutputChannel(outputChannel, producerProperties);
    // 调用subscribe方法设置处理器
    ((SubscribableChannel) outputChannel).subscribe(
            new SendingHandler(producerMessageHandler, HeaderMode.embeddedHeaders
                    .equals(producerProperties.getHeaderMode()), this.headersToEmbed,
                    producerProperties.isUseNativeEncoding()));

    Binding<MessageChannel> binding = new DefaultBinding<MessageChannel>(destination, null, outputChannel,
            producerMessageHandler instanceof Lifecycle ? (Lifecycle) producerMessageHandler : null) {

        @Override
        public Map<String, Object> getExtendedInfo() {
            return doGetExtendedInfo(destination, producerProperties);
        }

        @Override
        public void afterUnbind() {
            try {
                destroyErrorInfrastructure(producerDestination);
                if (producerMessageHandler instanceof DisposableBean) {
                    ((DisposableBean) producerMessageHandler).destroy();
                }
            }
            catch (Exception e) {
                AbstractMessageChannelBinder.this.logger
                        .error("Exception thrown while unbinding " + toString(), e);
            }
            afterUnbindProducer(producerDestination, producerProperties);
        }
    };

    doPublishEvent(new BindingCreatedEvent(binding));
    return binding;
}

查看doBindProducer方法的调用方法,可以知道BindingService的bindProducer方法调用了doBindProducer

public <T> Binding<T> bindProducer(T output, String outputName) {
    String bindingTarget = this.bindingServiceProperties
            .getBindingDestination(outputName);
    Binder<T, ?, ProducerProperties> binder = (Binder<T, ?, ProducerProperties>) getBinder(
            outputName, output.getClass());
    ProducerProperties producerProperties = this.bindingServiceProperties
            .getProducerProperties(outputName);
    if (binder instanceof ExtendedPropertiesBinder) {
        Object extension = ((ExtendedPropertiesBinder) binder)
                .getExtendedProducerProperties(outputName);
        ExtendedProducerProperties extendedProducerProperties = new ExtendedProducerProperties<>(
                extension);
        BeanUtils.copyProperties(producerProperties, extendedProducerProperties);
        producerProperties = extendedProducerProperties;
    }
    validate(producerProperties);
    // 调用doBindProducer方法
    Binding<T> binding = doBindProducer(output, bindingTarget, binder, producerProperties);
    this.producerBindings.put(outputName, binding);
    return binding;
}

继续追踪可以发现BinderAwareChannelResolver类的resolveDestination方法调用了bindProducer,该方法通过channelName从容器中获取发送消息的通道,如果不存在则根据通道名绑定的信息构建消息发送通道并关联上具体的消息中间件。resolveDestination方法返回的消息输出通道就可以直接用于发送消息。调用返回的MessageChannel的send方法,就可以完成消息的发送,也正是在这个send方法中抛出的Dispatcher has no subscribers

// 调用BinderAwareChannelResolver类的resolveDestination方法,获取MessageChannel对象并调用send方法发送消息
resolver.resolveDestination(channelName).send(MessageBuilder.createMessage(dataWrapper, messageHeaders), timeout);

public MessageChannel resolveDestination(String channelName) {
    try {
        // 更具通道名获取发送消息通道
        return super.resolveDestination(channelName);
    }
    catch (DestinationResolutionException e) {
        // intentionally empty; will check again while holding the monitor
    }
    // 如果发送消息通道不存在则创建。多线程可能会导致异常
    synchronized (this) {
        BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties();
        String[] dynamicDestinations = bindingServiceProperties.getDynamicDestinations();
        boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations) || ObjectUtils.containsElement(dynamicDestinations, channelName);
        try {
            return super.resolveDestination(channelName);
        }
        catch (DestinationResolutionException e) {
            if (!dynamicAllowed) {
                throw e;
            }
        }

        MessageChannel channel = this.bindingTargetFactory.createOutput(channelName);
        this.beanFactory.registerSingleton(channelName, channel);

        this.instrumentChannelWithGlobalInterceptors(channel, channelName);

        channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName);
        if (this.newBindingCallback != null) {
            ProducerProperties producerProperties = bindingServiceProperties.getProducerProperties(channelName);
            Object extendedProducerProperties = this.bindingService.getExtendedProducerProperties(channel, channelName);
            this.newBindingCallback.configure(channelName, channel, producerProperties, extendedProducerProperties);
            bindingServiceProperties.updateProducerProperties(channelName, producerProperties);
        }
        Binding<MessageChannel> binding = this.bindingService.bindProducer(channel, channelName);
        this.dynamicDestinationsBindable.addOutputBinding(channelName, binding);
        return channel;
    }
}

由上述代码分析可知,在resolveDestination方法进行MessageChannel初始化时,如果在doBindProducer方法调用((SubscribableChannel) outputChannel).subscribe(…)方法之前遇到异常将无法完成消息处理方法的注册,将会导致在之后send方法中获取不到消息的处理器导致抛出Dispatcher has no subscribers。那在resolveDestination方法中抛出的异常都将会是导致之后send方法抛出Dispatcher has no subscribers异常的原因

原因一:Failed to register bean with name ‘" + name + "’, since bean with the same name already exists…

Failed to register bean with name ‘" + name + "’, since bean with the same name already exists…错误就是resolveDestination方法进行MessageChannel初始化时可能出现的异常。该异常的抛出点在AbstractMessageChannelBinder的registerComponentWithBeanFactory方法。该方法判断上下文中是否已经包含指定名称的bean,如果包含就会抛出上述异常

private void registerComponentWithBeanFactory(String name, Object component) {
    if (getApplicationContext().getBeanFactory().containsBean(name)) {
        throw new IllegalStateException("Failed to register bean with name '" + name + "', since bean with the same name already exists. Possible reason: "
                + "You may have multiple bindings with the same 'destination' and 'group' name (consumer side) "
                + "and multiple bindings with the same 'destination' name (producer side). Solution: ensure each binding uses different group name (consumer side) "
                + "or 'destination' name (producer side)." );
    }
    else {
        getApplicationContext().getBeanFactory().registerSingleton(name, component);
    }
}

查看registerComponentWithBeanFactory调用链可知AbstractMessageChannelBinder的registerErrorInfrastructure方法在进行调用,该方法在注册错误通道,错误通道的命名为destination.getName() + “.errors”,errorBridgeHandlerName的命名为destination.getName() + “.errors” + “.bridge”。在注错误通道或者errorBridge时都会调用registerComponentWithBeanFactory方法,传入错误通道名或errorBridgeHandlerName。由错误通道名、errorBridgeHandlerName的命名规则可知,如果两个不同通道对应同一个destination(对于Kafka就是topic),第一个通道完成初始化后第二个通道再初始化时,由于两个通道destination相同,第二个通道初始化时将会抛出上述异常。更近一步由于resolveDestination在初始化输出通道时并非线程安全,如果多个线程同时调用resolveDestination方法,第一个线程进入synchronized代码块,但还没有完成输出通道注册,之后的线程在上下文无法通过输出通道名获取bean,也会等待进入synchronized代码块的初始化输出通道逻辑。当第一个线程初始化输出通道逻辑结束,第二个线程进入synchronized代码块执行初始化输出通道逻辑,由于已经初始化完成,则在创建错误通道时会报上述异常,之后进入synchronized代码块的相同输出通道的线程都会报这个异常

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();
        this.registerComponentWithBeanFactory(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);
        // 获取errorBridgeHandlerName
        String errorBridgeHandlerName = getErrorBridgeName(destination);
        this.registerComponentWithBeanFactory(errorBridgeHandlerName, errorBridge);
        beanFactory.initializeBean(errorBridge, errorBridgeHandlerName);
    }
    return errorChannel;
}

// 错误通道名生成规则
protected String errorsBaseName(ProducerDestination destination) {
    return destination.getName() + ".errors";
}

// errorBridgeHandlerName生成规则
protected String getErrorBridgeName(ProducerDestination destination) {
    return errorsBaseName(destination) + ".bridge";
}

原因二:"The number of expected partitions was: " + partitionCount + ", but " + partitionSize + (partitionSize > 1 ? " have " : " has ") + “been found instead.”…

"The number of expected partitions was: " + partitionCount + ", but " + partitionSize + (partitionSize > 1 ? " have " : " has ") + “been found instead.”…异常在KafkaTopicProvisioner类的createTopicAndPartitions方法中抛出。该方法判断如果传入的分区数大于实际分区数,并且没有设置自动增加分区数及isAutoRebalanceEnabled设置为false将抛出上述异常

private void createTopicAndPartitions(AdminClient adminClient, final String topicName, final int partitionCount,
        boolean tolerateLowerPartitionsOnBroker, KafkaAdminProperties adminProperties) throws Throwable {

    ListTopicsResult listTopicsResult = adminClient.listTopics();
    KafkaFuture<Set<String>> namesFutures = listTopicsResult.names();

    Set<String> names = namesFutures.get(operationTimeout, TimeUnit.SECONDS);
    if (names.contains(topicName)) {
        // only consider minPartitionCount for resizing if autoAddPartitions is true
        // 有效的分区数
        int effectivePartitionCount = this.configurationProperties.isAutoAddPartitions()
                ? Math.max(this.configurationProperties.getMinPartitionCount(), partitionCount)
                : partitionCount;
        DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(Collections.singletonList(topicName));
        KafkaFuture<Map<String, TopicDescription>> topicDescriptionsFuture = describeTopicsResult.all();
        Map<String, TopicDescription> topicDescriptions = topicDescriptionsFuture.get(operationTimeout, TimeUnit.SECONDS);
        TopicDescription topicDescription = topicDescriptions.get(topicName);
        // 实际分区数
        int partitionSize = topicDescription.partitions().size();
        // 实际分区数是否小于有效分区数
        if (partitionSize < effectivePartitionCount) {
            // 判断是否运行自动添加分区数
            if (this.configurationProperties.isAutoAddPartitions()) {
                CreatePartitionsResult partitions = adminClient.createPartitions(
                        Collections.singletonMap(topicName, NewPartitions.increaseTo(effectivePartitionCount)));
                partitions.all().get(operationTimeout, TimeUnit.SECONDS);
            }
            // 判断是否允许更低的分区数
            else if (tolerateLowerPartitionsOnBroker) {
                logger.warn("The number of expected partitions was: " + partitionCount + ", but "
                        + partitionSize + (partitionSize > 1 ? " have " : " has ") + "been found instead."
                        + "There will be " + (effectivePartitionCount - partitionSize) + " idle consumers");
            }
            else {
                throw new ProvisioningException("The number of expected partitions was: " + partitionCount + ", but "
                        + partitionSize + (partitionSize > 1 ? " have " : " has ") + "been found instead."
                        + "Consider either increasing the partition count of the topic or enabling " +
                        "`autoAddPartitions`");
            }
        }
    }
    else {
        // always consider minPartitionCount for topic creation
        final int effectivePartitionCount = Math.max(this.configurationProperties.getMinPartitionCount(),
                partitionCount);
        this.metadataRetryOperations.execute(context -> {

            NewTopic newTopic;
            Map<Integer, List<Integer>> replicasAssignments = adminProperties.getReplicasAssignments();
            if (replicasAssignments != null &&  replicasAssignments.size() > 0) {
                newTopic = new NewTopic(topicName, adminProperties.getReplicasAssignments());
            }
            else {
                newTopic = new NewTopic(topicName, effectivePartitionCount,
                        adminProperties.getReplicationFactor() != null
                                ? adminProperties.getReplicationFactor()
                                : configurationProperties.getReplicationFactor());
            }
            if (adminProperties.getConfiguration().size() > 0) {
                newTopic.configs(adminProperties.getConfiguration());
            }
            CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singletonList(newTopic));
            try {
                createTopicsResult.all().get(operationTimeout, TimeUnit.SECONDS);
            }
            catch (Exception e) {
                if (e instanceof ExecutionException) {
                    String exceptionMessage = e.getMessage();
                    if (exceptionMessage.contains("org.apache.kafka.common.errors.TopicExistsException")) {
                        if (logger.isWarnEnabled()) {
                            logger.warn("Attempt to create topic: " + topicName + ". Topic already exists.");
                        }
                    }
                    else {
                        logger.error("Failed to create topics", e.getCause());
                        throw e.getCause();
                    }
                }
                else {
                    logger.error("Failed to create topics", e.getCause());
                    throw e.getCause();
                }
            }
            return null;
        });
    }
}

在实际使用中,如果我们想要指定发送分区的策略,需要设置分区的大小,并且需要大于1。但如果我们设置分区的大小大于实际的大小,并且没有其它配置,有可能会报出上述错误.由于createTopicAndPartitions方法也是在resolveDestination初始化输出通道时执行,因此此异常抛出后会紧接着抛出Dispatcher has no subscribers for channel…异常

ProducerProperties producerProperties = new ProducerProperties();
producerProperties.setPartitionKeyExpression(new SpelExpressionParser().parseExpression("payload.tenantId"));
producerProperties.setPartitionCount(2);
eventSender.fireEvent(EventSenderConfig.newBuilder()
                                    .tenantId(0L)
                                    .eventCode("HALT_TARGET_1")
                                    .category("HALT_TARGET_1")
                                    .producerProperties(producerProperties)
                                    .data(alertRoute)
                                    .builder()

实际使用案例及解决方案

通过上面原因的分析以及结合具体报错分析可知,Dispatcher has no subscribers for channel…异常只是结果,真正触发抛出此异常的原因需要看resolveDestination初始化时抛出的异常,此异常通常在Dispatcher has no subscribers for channel…异常的前面抛出,并且基本都挨在一起。因此遇到Dispatcher has no subscribers for channel…异常可以往前翻翻,找到具体报错原因,具体问题具体分析

场景一:通过事件服务发送消息,不同事件关联同一个topic

在调用EventSender方法的fireEvent方法发送消息时,如果两个不同eventCode的两个事件,它们都指定了topic,并且topic还相同,在同一个服务下,当第一个事件先发送消息,第二个事件后发送消息,第二个发送消息的事件将报错

AlertRoute alertRoute = new AlertRoute();
alertRoute.setAlertRouteId(1L);
eventSender.fireEvent(EventSenderConfig.newBuilder()
                .tenantId(0L)
                .eventCode("HALT_TARGET_1")
                .category("HALT_TARGET_1")
                .data(alertRoute)
                .builder()
        , message -> {
            System.out.println("======message=====Headers " + message.getHeaders() + "================");

            System.out.println("======message=====Payload " + message.getPayload() + "================");

        });


eventSender.fireEvent(CustomTopicSenderConfig.newBuilder()
                .topic("halt-target-test-13")
                .channelName("halt-target-test-11010")
                .data(alertRoute)
                .eventSourceCode("hzero-kafaka")
                .tenantId(0L)
                .builder()
        , message -> {
            System.out.println("======message=====Headers " + message.getHeaders() + "================");

            System.out.println("======message=====Payload " + message.getPayload() + "================");
        });

解决方案

每个topic最好只对应一个事件,如果非要对应多个事件,那同一个服务中,不能使用两个不同事件但topic相同

场景二:服务启动时,开启异步线程,发送消息

当服务启动时开启异步线程发送消息,此时事件客户端启动完成初始化的阶段不能确定,有可能事件客户端还没开始初始化,或者初始化正在进行,此时异步线程发送消息可能导致不能预料的错误,具体错误与事件客户端进行进度有关
在这里插入图片描述

解决方案

监听事件客户端启动完成后发布的EventSender事件,在监听EventSender的回调方法中执行异步发送消息的逻辑。事件监听的相关知识请查看https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

场景三:多线程并发调用发送消息方法

多线程并发调用发送消息方法,并且都是往同一个事件发送消息,可能导致第一个线程进入resolveDestination方法的synchronized代码块,但还没有完成输出通道注册,之后的线程在上下文无法通过输出通道名获取bean,也会等待进入synchronized代码块的初始化输出通道逻辑。当第一个线程初始化输出通道逻辑结束,第二个线程进入synchronized代码块执行初始化输出通道逻辑,由于已经初始化完成,则在创建错误通道时会报上述异常,之后进入synchronized代码块的相同输出通道的线程都会报这个异常

ProducerProperties producerProperties = new ProducerProperties();
producerProperties.setPartitionKeyExpression(new SpelExpressionParser().parseExpression("payload.tenantId"));
producerProperties.setPartitionCount(2);
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        for(int i=0; i< 10 ; i++) {
            eventSender.fireEvent(EventSenderConfig.newBuilder()
                            .tenantId(0L)
                            .eventCode("HALT_TARGET_1")
                            .category("HALT_TARGET_1")
                            .producerProperties(producerProperties)
                            .data(alertRoute)
                            .builder()
                    , message -> {
                        System.out.println("======message=====Headers " + message.getHeaders() + "================");

                        System.out.println("======message=====Payload " + message.getPayload() + "================");
                    });
        }
    }
});
thread.start();


Thread thread2 = new Thread(new Runnable() {
    @Override
    public void run() {
        for(int i=0; i< 10 ; i++) {
            eventSender.fireEvent(EventSenderConfig.newBuilder()
                            .tenantId(0L)
                            .eventCode("HALT_TARGET_1")
                            .category("HALT_TARGET_1")
                            .producerProperties(producerProperties)
                            .data(alertRoute)
                            .builder()
                    , message -> {
                        System.out.println("======message=====Headers " + message.getHeaders() + "================");

                        System.out.println("======message=====Payload " + message.getPayload() + "================");
                    });
        }
    }
});
thread2.start();

解决方案

覆盖BinderAwareChannelResolver,重写resolveDestination方法,在进入synchronized方法块后再尝试从上下文获取通道名对应的bean,如果获取到,则表明上个线程已经创建好输出通道,直接返回使用;如果还不能获取到输出通道的bean,则继续执行初始化任务

public MessageChannel resolveDestination(String channelName) {
    try {
        return super.resolveDestination(channelName);
    } catch (DestinationResolutionException var12) {
        synchronized(this) {
           // 如果再次获取输出通道成功则直接返回,失败则继续执行下面逻辑
            try{
                return super.resolveDestination(channelName)
            } catch (DestinationResolutionException var12) {
                
            }
            
            BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties();
            String[] dynamicDestinations = bindingServiceProperties.getDynamicDestinations();
            boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations) || ObjectUtils.containsElement(dynamicDestinations, channelName);

            MessageChannel var10000;
            try {
                var10000 = super.resolveDestination(channelName);
            } catch (DestinationResolutionException var10) {
                if (!dynamicAllowed) {
                    throw var10;
                }

                MessageChannel channel = (MessageChannel)this.bindingTargetFactory.createOutput(channelName);
                this.beanFactory.registerSingleton(channelName, channel);
                this.instrumentChannelWithGlobalInterceptors(channel, channelName);
                channel = (MessageChannel)this.beanFactory.initializeBean(channel, channelName);
                if (this.newBindingCallback != null) {
                    ProducerProperties producerProperties = bindingServiceProperties.getProducerProperties(channelName);
                    Object extendedProducerProperties = this.bindingService.getExtendedProducerProperties(channel, channelName);
                    this.newBindingCallback.configure(channelName, channel, producerProperties, extendedProducerProperties);
                    bindingServiceProperties.updateProducerProperties(channelName, producerProperties);
                }

                Binding<MessageChannel> binding = this.bindingService.bindProducer(channel, channelName);
                this.dynamicDestinationsBindable.addOutputBinding(channelName, binding);
                return channel;
            }

            return var10000;
        }
    }
}


所属网站分类: 技术文章 > 博客

作者:我是一个射手

链接:http://www.javaheidong.com/blog/article/207753/6bdaa77a7724884a3cdb/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)