My understanding is that Jersey's WebTargets are thread safe and can be called concurrently but I have a concurrent bug. The code below works fine in one thread but as soon as I call it concurrently I get a NPE.
public static final MultivaluedMap<String, Object> ACCEPT_GZIP =
new MultivaluedHashMap<>(hashMap("Accept-Encoding", "gzip"));
webTarget.path("my_web_service/path")
.request(MediaType.APPLICATION_JSON_TYPE)
.headers(ACCEPT_GZIP)
.post(entity(symbols.keySet(), APPLICATION_JSON_TYPE),
new GenericType<List<MyPojo>>(){});
Stack trace:
javax.ws.rs.ProcessingException
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:705)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:701)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:446)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at com.assylias.xxx.ws.xx.getLastDates(xx.java:107)
at com.assylias.xxx.ws.xx.lambda$main$5(xx.java:87)
at com.assylias.xxx.ws.xx$$Lambda$34/231311211.accept(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1359)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290)
at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:902)
at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1689)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1644)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.NullPointerException
at java.util.LinkedList$ListItr.next(LinkedList.java:893)
at jersey.repackaged.com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at org.glassfish.jersey.filter.LoggingFilter.printPrefixedHeaders(LoggingFilter.java:190)
at org.glassfish.jersey.filter.LoggingFilter.filter(LoggingFilter.java:230)
at org.glassfish.jersey.client.ClientFilteringStages$RequestFilteringStage.apply(ClientFilteringStages.java:110)
at org.glassfish.jersey.client.ClientFilteringStages$RequestFilteringStage.apply(ClientFilteringStages.java:98)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:171)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
... 21 more
The problem is that MultivaluedMaps are not thread safe and Jersey's logger seems to play with the headers in a thread unsafe way so sharing a public static final MultivaluedMap<String, Object> ACCEPT_GZIP
is not a good idea. The headers should be recreated for each request:
webTarget.path("my_web_service/path")
.request(MediaType.APPLICATION_JSON_TYPE)
.header("Accept-Encoding", "gzip")
.post(entity(symbols.keySet(), APPLICATION_JSON_TYPE),
new GenericType<List<MyPojo>>(){});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With