我的理解是Jersey的WebTargets是线程安全的,可以同时调用,但我有一个并发错误。下面的代码在一个线程中工作正常,但是当我同时调用它时,我得到一个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>>(){});
堆栈追踪:
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
答案 0 :(得分:7)
问题是MultivaluedMaps不是线程安全的,并且Jersey的记录器似乎以线程不安全的方式使用标头,因此共享public static final MultivaluedMap<String, Object> ACCEPT_GZIP
不是一个好主意。应为每个请求重新创建标头:
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>>(){});