JAX-WS:有状态WS在独立进程中失败 [英] JAX-WS: stateful WS fails in a standalone process
问题描述
我在Tomcat上部署了有状态的Web服务.它由工厂服务和主要API服务组成,并且工作正常.工厂服务将W3CEndpointReference返回到主API实例,客户端使用该会话.
I have a stateful web service deployed onto Tomcat. It consists of factory service and main API service, and works just fine. Factory service returns a W3CEndpointReference to main API instance, and client uses the session.
现在,我正在尝试运行与独立应用程序相同的服务.在这种情况下,工厂返回的W3CEndpointReference突然开始指向工厂URI,而不是主服务URL.
Now, I'm trying to run the very same service as a standalone application. In this case, the W3CEndpointReference returned by the factory suddenly starts to point to factory URI, not to main service one.
当比较针对Tomcat和独立运行的引用时,很明显独立引用的URI错误.具体来说,它指向工厂URI,而不是主API.
When references from runs against Tomcat and standalone are compared, it becomes clear that standalone reference has a wrong URI. Specifically, it points to factory URI, not main API one.
这里是正确的参考:
...
<ns3:Address>http://localhost:8080/td2ws/api</ns3:Address>
...
在独立进程上调用factory时的引用如下:
Here's references when factory is called on standalone process:
<Address>http://localhost:9009/td2ws/factory</Address>
我的理解是,Servlet上下文下的某些代码了解服务类(Td2Ws)与它的URI之间的对应关系,并相应地调整了引用.该代码段在独立过程中无效.我什至可以怀疑代码使用了sun-jaxws.xml,但是我不知道如何打开"它.
My understanding that some code under servlet context has a knowledge of correspondence between service class (Td2Ws) and it's URI, and adjusts the references accordingly. That piece of code is not in effect under standalone process though. I can even suspect that code uses sun-jaxws.xml, but I don't know how to "turn it on".
如何在独立的应用程序中使有状态的Web服务正常工作?
以下是代码的相关部分:
Here are relevant parts of the code:
工厂服务(无状态):
@WebService(targetNamespace="http://server.td2ws.sf.net/")
public class Td2WsFactory {
@WebMethod(operationName="StartSession", action="urn:StartSession")
@WebResult(name="Reference")
public W3CEndpointReference startSession() {
StatefulWebServiceManager<Td2Ws> manager = Td2Ws.getManager();
// for standalone execution only
if( manager == null ) {
manager = new StatefulInstanceResolver<Td2Ws>(Td2Ws.class);
Td2Ws.setManager(manager);
}
Td2Ws session = new Td2Ws();
return manager.export(W3CEndpointReference.class,session);
}
}
状态API:
@Stateful
@WebService(targetNamespace="http://server.td2ws.sf.net/")
@Addressing
public class Td2Ws {
private static StatefulWebServiceManager<Td2Ws> manager;
public static void setManager(StatefulWebServiceManager<Td2Ws> manager ) {
Td2Ws.manager = manager;
}
public static StatefulWebServiceManager<Td2Ws> getManager() {
return Td2Ws.manager;
}
@WebMethod(operationName="Login", action="urn:Login")
public void login(
@WebParam(name="Username") String username,
@WebParam(name="Password") String password,
@WebParam(name="Domain") String domain,
@WebParam(name="Project") String project
) throws Td2WsException {
客户端代码(定位为独立):
Client code (targeted to standalone):
public static void main(String[] args) throws Exception {
Endpoint epf = Endpoint.publish("http://localhost:9009/td2ws/factory", new net.sf.td2ws.server.Td2WsFactory());
Endpoint eps = Endpoint.publish("http://localhost:9009/td2ws/api", new net.sf.td2ws.server.Td2Ws());
URL factoryUrl = new URL("http://localhost:9009/td2ws/factory?wsdl");
QName factoryQname = new QName("http://server.td2ws.sf.net/", "Td2WsFactoryService");
URL sessionUrl = new URL("http://localhost:9009/td2ws/api?wsdl");
QName sessionQname = new QName("http://server.td2ws.sf.net/", "Td2WsService");
Td2WsFactory factory = new Td2WsFactoryService(factoryUrl,factoryQname).getTd2WsFactoryPort();
System.out.println("factory: "+factory);
W3CEndpointReference session = factory.startSession();
System.out.println("session: "+session);
Td2WsService service = new Td2WsService(sessionUrl,sessionQname);
System.out.println("service: "+service);
Td2Ws port = service.getPort(session,Td2Ws.class);
System.out.println("port: "+port);
port.login(
Config.get("td.user"),
Config.get("td.pass"),
Config.get("td.domain"),
Config.get("td.project"));
System.out.println("logged in...");
该代码在login()上失败,因为目标对象没有Login操作(毕竟,它错误地针对了工厂URI):
That code fails on login() as the target object has no Login operation (it's wrongly targeted to factory URI, after all):
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException:
Cannot find dispatch method for {http://server.td2ws.sf.net/}Login
推荐答案
我已经有一段时间没有这样做了,但是我看了一段时间前构建的功能性有状态Web服务.我的注释与您的注释非常相似,只不过我在班级顶部有此注释:
I haven't done this in awhile, but I took a look at a functional stateful web service that I built some time ago. My Annotations are very similar to yours, except I have this at the top of my class:
public class MyService {
@Resource
WebServiceContext wsCtx;
...
}
请注意,没有修饰符,如果我认为其他任何修饰符都需要公开,则我并不乐观.容器如何知道如何设置此设置使我很吃惊,但它确实有效.它必须只查找WebServiceContext类型的任何@Resource并进行设置.您可以像这样访问会话信息:
Notice there is no modifier, if anything else I think it needs to be public, but I'm not positive. How the container knows to set this beats me, but it works. It must just look for any @Resource of type WebServiceContext and set it. You can access session information like so:
HttpServletRequest hRequest = (HttpServletRequest)ctx.getMessageContext().get(MessageContext.SERVLET_REQUEST);
Session s = hRequest.getSession();
这篇关于JAX-WS:有状态WS在独立进程中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!