使用 nv-websocket-client 在 WebSocket 侦听器下的 Liberty Profile 中调用 JAX-RS 2.0 CXF 中的目标时出现 NPException

NPException when calling target in JAX-RS 2.0 CXF in Liberty Profile under WebSocket listener with nv-websocket-client

我在调用简单的 JAX-RS 2.0 REST 客户端例程时收到此 NullPointerException。我的方法 (CogniAPIRESTClient.performQuery() ) 在我的应用程序的许多地方工作正常,但是当它从 NV-WebSocketClient WebSocket 侦听器创建的线程调用时失败,如异常跟踪中所示:

[3/13/16 20:54:47:789 CET] 000000ba id=         com.cogniapi.ElizaDemoView                                   E Exception handling audio content
 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.cogniapi.ws.rs.client.CogniAPIRESTClient.performQuery(CogniAPIRESTClient.java:44)
     at com.cogniapi.ElizaDemoView.onMessage(ElizaDemoView.java:145)
     at com.cogniapi.speechtotext.WebSocketSpeechToTextClient$WebSocketListener.onTextMessage(WebSocketSpeechToTextClient.java:116)
     at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
     at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:233)
     at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:211)
     at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:910)
     at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:693)
     at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:102)
     at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:61)

我已经检查了 REST 参数,它们是正确的,事实上,如果我从 class postConstruct 调用具有相同参数的方法,例如

这与该线程不是 Liberty 管理的线程有关,但即使我使用 ManagedExecutor 调用 connect() 方法,nv-websocket-client 库似乎没有使用该管理执行器来创建线程负责 callback/listener 调用,所以当它被调用时我得到异常。关于如何使该线程成为容器管理的任何想法?

注:图书馆:https://github.com/TakahikoKawasaki/nv-websocket-client

谢谢,伊格纳西奥

nv-websocket-client internally creates two threads after a WebSocket connection is established. One to receive WebSocket frames from the WebSocket server and the other to send WebSocket frames to the WebSocket server. Most callback methods listed in <a href="http://takahikokawasaki.github.io/nv-websocket-client/com/neovisionaries/ws/client/WebSocketListener.html" rel="nofollow">WebSocketListener</a> 被任一线程调用。

如果您的代码必须 运行 在特定线程(Liberty 管理的线程)中,您应该将通过 onTextMessage 接收到的数据传递给该线程。例如,如果线程通过 getQueue() 方法公开了 <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html" rel="nofollow">BlockingQueue</a> 的实例,则可以像下面这样通过队列向线程传递消息。

public void onTextMessage(WebSocket websocket, String message) {
    // Pass the message to the Liberty managed thread.
    theThread.getQueue().add(message);
}

您可以在 java.util.concurrent 包下找到实用程序 类 以在线程之间共享数据。