问题
I have some JAX-RS 2.0 clients running ok in Liberty Profile 8.5.5.6. They are all working ok sequentially, but I want to execute some of them in a Thread so they run concurrently. When I try to get a "target" inside a FutureTask with:
WebTarget myResource = client.target(apiURLBase + ...);
I get NullPointerExcepton in this com.ibm.ws... class:
Caused by: java.lang.NullPointerException
at com.ibm.ws.jaxrs20.client.bus.LibertyJAXRSClientBusFactory.getClientScopeBus(LibertyJAXRSClientBusFactory.java:89)
at com.ibm.ws.jaxrs20.client.JAXRSClientImpl.target(JAXRSClientImpl.java:109)
at org.apache.cxf.jaxrs.client.spec.ClientImpl.target(ClientImpl.java:100)
at com.servengine.watson.naturallanguageclassifier.NaturalLanguageClassifierRESTClient.classify(NaturalLanguageClassifierRESTClient.java:161)
at com.servengine.watson.naturallanguageclassifier.NaturalLanguageClassifierRESTClient$Proxy$_$$_WeldClientProxy.classify(Unknown Source)
at com.skios.eliza.nlq.NaturalLanguageQueryFlowView$1.call(NaturalLanguageQueryFlowView.java:66)
at com.skios.eliza.nlq.NaturalLanguageQueryFlowView$1.call(NaturalLanguageQueryFlowView.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
CXF cannot be used out of "main" Thread? What am I missing?
Thanks for any help.
回答1:
If you want to use the client API in a forked new thread, you need to make sure the new thread is a container managed one. In your case, you can enable concurrent-1.0 feature. Then use:
@Resource(name="java:comp/DefaultManagedExecutorServcie") ManagedExecutorService executor;
Task task = new Task();
Future<String> result = executor.submit(task);
In Your Task class, you can do the same thing with what you done before: WebTarget myResource = client.target(apiURLBase + ...);
来源:https://stackoverflow.com/questions/31922438/nullpointerexception-when-running-cxf-jax-rs-2-0-client-target-method-in-liber