Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I randomically experience a NullPointerException while trying to publish a message using RabbitTemplate.convertAndSend
I tried both spring-amqp:1.7.6 and spring-amqp:1.7.8 and this should be the relevant part of my dependency tree:
[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
this is the stacktrace of the exception
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
Any clue on what might be causing this?
edit: additional infos
i didn't notice the difference between spring-rabbit and spring-amqp at first, but looking inside the jar i have these dependencies:
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
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;
Pay attention to the if (this.target == null) {
and into the method.invoke(this.target, args);
. In between of these to operations the this.target
may become as null
. That's the fact for the catch (InvocationTargetException ex) {
block below: we do this.target = null;
outside of the synchronized (this.targetMonitor) {
. So, some other thread may cause a race condition for this target
property.
That's what I mean about the NPE
problem and this is kinda a suggestion how to fix.
Now we need to understand how is it even possible that different threads get access to the same ChannelProxy
to cause such a race condition... Maybe there is still some flaw around caching for the channels.
Would be great, of course, if you try your solution with the latest spring-rabbit-1.7.8
, because you dependencies still show us it as a 1.7.4
.
–
–
–
–
–
I got the same error while publishing randomly in live environment, this is due to creating the channels and not closing them. Rabbitmq has a limit of 5000 channels.
This could not be easily reproducible in test environment as it requires opening 5k channels.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.