我测试了Web服务处理程序,并且我创建了两个模拟服务和一个模拟客户端。客户端(A)调用调用其他服务(C)的服务(B)。我在B上有一个处理程序,在C上有另一个处理程序,只是打印它们已被激活。我期望的是B处理程序激活4次(一次当A联系他时,一次当他联系C时,一次当C回复时,一次当B回复A时),但他只在A联系他时以及何时激活他必须回复A. C上的处理程序按预期工作(激活两次)。
我有以下代码启动服务并激活客户端:
Endpoint.publish("http://localhost:8080/WSSecurity/helloworld", new WSDocEndpointImpl());
Endpoint.publish("http://localhost:8081/WSSecurity2/helloworld2", new WSDocEndpointImpl2());
URL wsdlUrl = new URL("http://localhost:8081/WSSecurity2/helloworld2?wsdl");
QName qname = new QName("http://webservice2.document.ftn.uns.ac.rs/", "WSDocEndpointImpl2Service");
Service service = Service.create(wsdlUrl, qname);
WSDocEndpoint2 helloWS = service.getPort(WSDocEndpoint2.class);
String response = helloWS.getHelloWorldAsString("Petar");
System.out.println(response);
我的网络服务:
@WebService(endpointInterface = "rs.ac.uns.ftn.document.webservice.WSDocEndpoint")
@HandlerChain(file = "/rs/ac/uns/ftn/document/handler-chain-document.xml")
public class WSDocEndpointImpl implements WSDocEndpoint {
@Override
public String getHelloWorldAsString(String name) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ARRIVED AT WS 1 --- " + new Date());
return "Hello, " + name;
}
}
@WebService(endpointInterface = "rs.ac.uns.ftn.document.webservice2.WSDocEndpoint2")
@HandlerChain(file = "/rs/ac/uns/ftn/document/handler-chain-document2.xml")
public class WSDocEndpointImpl2 implements WSDocEndpoint2 {
@Override
public String getHelloWorldAsString(String name) {
try {
URL wsdlUrl = new URL(
"http://localhost:8080/WSSecurity/helloworld?wsdl");
QName qname = new QName(
"http://webservice.document.ftn.uns.ac.rs/",
"WSDocEndpointImplService");
Service service = Service.create(wsdlUrl, qname);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ARRIVED AT WS 2 --- " + new Date());
WSDocEndpoint helloWS = service.getPort(WSDocEndpoint.class);
String responseFromWS1 = helloWS.getHelloWorldAsString("Petar");
return responseFromWS1;
} catch (Exception e) {
return "EXCEPTION!";
}
}
}
句柄消息方法:
@Override
public boolean handleMessage(SOAPMessageContext context) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("TEST HANDLER! --- " + new Date());
return true;
}
最后是xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hc:handler-chains xmlns:hc="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<hc:handler-chain>
<hc:handler>
<hc:handler-class>rs.ac.uns.ftn.document.handler.TestHandler</hc:handler-class>
</hc:handler>
</hc:handler-chain>
</hc:handler-chains>
我在控制台上获得的输出:
TEST HANDLER 2! --- Fri May 15 18:58:35 CEST 2015
ARRIVED AT WS 2 --- Fri May 15 18:58:36 CEST 2015
TEST HANDLER! --- Fri May 15 18:58:38 CEST 2015
ARRIVED AT WS 1 --- Fri May 15 18:58:39 CEST 2015
TEST HANDLER! --- Fri May 15 18:58:41 CEST 2015
TEST HANDLER 2! --- Fri May 15 18:58:43 CEST 2015
Hello, Petar
答案 0 :(得分:0)
问题在于我编写代码的方式。当我的Web服务充当客户端时,它没有处理处理程序。我必须为我的Web服务生成一个Web服务客户端并在他身上注册处理程序,然后使用他来调用我的其他Web服务。