我尝试使用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
答案 0 :(得分:0)
我会说问题出在这样的块中:
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) {
块的事实:我们在this.target = null;
之外进行synchronized (this.targetMonitor) {
。因此,某些其他线程可能导致此target
属性的竞争状态。
这就是我对NPE
问题的意思,这有点像如何解决的建议。
现在,我们需要了解不同的线程怎么可能访问相同的ChannelProxy
从而导致这种竞争状态...也许在为通道缓存方面仍然存在一些缺陷。
当然,如果您尝试使用最新的spring-rabbit-1.7.8
解决方案,那将会很棒,因为您的依存关系仍将其显示为1.7.4
。
答案 1 :(得分:0)
在实时环境中随机发布时遇到了相同的错误,这是由于创建了频道而不是关闭了频道。 Rabbitmq的频道数上限为5000。 在测试环境中,这不可能被很好地重现,因为它需要打开5k通道。