我使用的是 SAP NetWeaver 7.3 EHP 1(经过 JEE5 认证),并且我已经为现有 WSDL 生成了 Web 服务框架。此 Web 服务中的所有方法都是非阻塞的,因此用 @Oneway
进行注释。该服务本身运行得很好。
我还需要访问注入(inject)的WebServiceContext
,这就是麻烦开始的地方。我可以很好地注入(inject)上下文,但对它的每次调用最终都会出现在特定于服务器的 NPE 或 ISE 中。我在某种程度上可以理解,因为非阻塞调用是由 SAP 的 JAX-WS 实现异步执行的。不过,我尝试通过查看 JAX-WS 规范来验证此行为。
现在我要么是盲目的,要么规范没有明确规定 WebServiceContext
不得在非阻塞 Web 方法中使用。事实上,在最新版本规范的整个第5.3章中,@Oneway
一次都没有被提及。主要描述是
The javax.xml.ws.WebServiceContext interface makes it possible for an endpoint implementation object and potentially any other objects that share its execution context to access information pertaining to the request being served.
The result of invoking any methods on the WebServiceContext of a component outside the invocation of one of its web service methods is undefined. An implementation SHOULD throw a java.lang- .IllegalStateException if it detects such a usage.
我想重点是“正在服务的请求”的定义。在异步处理 SOAP 操作时,HTTP 请求已经完成。但话又说回来,我没有访问“在其 Web 服务方法的调用之外”的上下文。我很困惑...
你们中有人知道我是否应该被允许在非阻塞 Web 方法中访问 WebServiceContext
吗?谢谢!
最佳答案
你是对的:规范没有提及此限制 - 它只是含糊地暗示。
来自 JAX-WS 规范(2.2 Rev A):
The WebServiceContext is treated as an injectable resource that can be set at the time an endpoint is initialized. The WebServiceContext object will then use thread-local information to return the correct information regardless of how many threads are concurrently being used to serve requests addressed to the same endpoint object.
应该可以在端点存在的情况下进行操作,但前提是在设置请求上下文信息并将其存储到线程本地内存存储的同一线程中使用。鉴于 WebServiceContext 主要是 MessageContext 信息,即 HTTP 请求/Servlet 请求/WSDL 操作设置,线程本地上下文信息可能需要由最初编码 SOAP/HTTP 请求的线程填充,这意味着稍后的异步处理线程没有可用的此信息。仅基于阅读 - 尚未测试此组合。
关于java - JAX-WS - 将 WebServiceContext 注入(inject)仅具有 @Oneway 方法的 @WebService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15290118/