gpt4 book ai didi

java - RabbitTemplate convertAndSend 上的 Spring Amqp 内部 NullPointerException

转载 作者:搜寻专家 更新时间:2023-10-30 23:01:35 25 4
gpt4 key购买 nike

我在尝试使用 RabbitTemplate.convertAndSend 发布消息时随机遇到 NullPointerException

我尝试了 spring-amqp:1.7.6 和 spring-amqp:1.7.8,这应该是我的依赖树的相关部分:

[INFO] | +- org.springframework.amqp:spring-rabbit:jar:1.7.4.RELEASE:compile
[INFO] | | +- com.rabbitmq:http-client:jar:1.1.1.RELEASE:compile
[INFO] | | \- com.rabbitmq:amqp-client:jar:4.0.3:compile

这是异常的堆栈跟踪

org.springframework.amqp.UncategorizedAmqpException: java.lang.NullPointerException
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:83)
at org.springframework.amqp.rabbit.connection.RabbitAccessor.convertRabbitAccessException(RabbitAccessor.java:113)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1461)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1411)
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:712)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:813)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:791)

at io.reactivex.internal.operators.completable.CompletableFromAction.subscribeActual(CompletableFromAction.java:34)
at io.reactivex.Completable.subscribe(Completable.java:1635)
at io.reactivex.internal.operators.completable.CompletableCache.subscribeActual(CompletableCache.java:59)
at io.reactivex.Completable.subscribe(Completable.java:1635)
at io.reactivex.internal.operators.completable.CompletableSubscribeOn$SubscribeOnObserver.run(CompletableSubscribeOn.java:64)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: null
at sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:980)
at com.sun.proxy.$Proxy174.basicPublish(Unknown Source)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doSend(RabbitTemplate.java:1532)
at org.springframework.amqp.rabbit.core.RabbitTemplate$3.doInRabbit(RabbitTemplate.java:716)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1455)
... 19 common frames omitted

关于可能导致此问题的任何线索?

编辑:附加信息

一开始我没有注意到 spring-rabbit 和 spring-amqp 之间的区别,但是在 jar 里面看我有这些依赖关系:

128356 Tue May 29 17:50:46 CEST 2018 lib/spring-amqp-1.7.8.RELEASE.jar
469867 Mon Sep 11 15:28:04 CEST 2017 lib/spring-rabbit-1.7.4.RELEASE.jar
481919 Wed Jun 07 15:25:06 CEST 2017 lib/amqp-client-4.0.3.jar

最佳答案

我会说问题出在这样的 block 中:

synchronized (this.targetMonitor) {
if (this.target == null) {
this.target = createBareChannel(this.theConnection, this.transactional);
}
Object result = method.invoke(this.target, args);
if (this.transactional) {
if (txStarts.contains(methodName)) {
this.txStarted = true;
}
else if (txEnds.contains(methodName)) {
this.txStarted = false;
}
}
return result;
}

注意 if (this.target == null) {method.invoke(this.target, args);。在这些操作之间,this.target 可能变为 null。这就是下面的 catch (InvocationTargetException ex) { block 的事实:我们在 synchronized (this.targetMonitor) {。因此,某些其他线程可能会导致此 target 属性出现竞争条件。

这就是我对 NPE 问题的意思,这是一个关于如何解决的建议。

现在我们需要了解不同的线程访问同一个 ChannelProxy 是如何导致这种竞争条件的……也许 channel 缓存仍然存在一些缺陷。

当然,如果您使用最新的 spring-rabbit-1.7.8 尝试您的解决方案,那就太好了,因为您的依赖项仍然向我们显示为 1.7.4.

关于java - RabbitTemplate convertAndSend 上的 Spring Amqp 内部 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51289212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com