Java – webtarget and thread safety
•
Java
My understanding is that Jersey's webtargets are thread safe and can be called at the same time, but I have a concurrent bug The following code works normally in a thread, but when I call it at the same time, I get an 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
Solution
The problem is that multivaluedmaps is not thread safe, and Jersey's recorder seems to use titles in a thread unsafe manner, so it shares a common static final multivalued map < string, Object > accept_ Gzip is not a good idea The header 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(),new GenericType<List<MyPojo>>(){});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码