【问题标题】:Web service handler doesn't work when a service calls another service当一个服务调用另一个服务时,Web 服务处理程序不起作用
【发布时间】:2015-05-15 17:07:06
【问题描述】:

我正在测试 Web 服务处理程序,并创建了两个模拟服务和一个模拟客户端。客户端 (A) 调用服务 (B),该服务调用另一个服务 (C)。我在 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

【问题讨论】:

    标签: web-services soap handler


    【解决方案1】:

    问题在于我编写代码的方式。当我的 Web 服务充当客户端时,它没有处理处理程序。我必须为我的 Web 服务生成一个 Web 服务客户端并在他身上注册处理程序,然后使用他来调用我的其他 Web 服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2023-04-03
      • 1970-01-01
      • 2013-03-27
      • 2011-02-16
      • 1970-01-01
      相关资源
      最近更新 更多