WS-DISCOVERY使用

最近项目中要用到探测webservice服务端的信息,在网上查询后了解到apache cxf提供了这部分功能,支持包在附件中。

具体使用如下:

将支持包放到webservice服务项目lib目录下,提供udp探测功能。

客户端探测实现代码:

 

public class WsClientDiscovery {
    public static void main(String[] args) {
        Bus bus = null;
        try {
            bus = BusFactory.newInstance().createBus();

            WSDiscoveryClient client = new WSDiscoveryClient(bus);
            client.setAddress("soap.udp://192.168.0.143:3702");  //服务端地址和udp端口地址
            ProbeType pt = new ProbeType();
            ScopesType scopes = new ScopesType();
            pt.setScopes(scopes);
            ProbeMatchesType pmts = client.probe(pt);
            List<ResolveMatchType> rmts = new LinkedList<ResolveMatchType>();
            for (ProbeMatchType pmt : pmts.getProbeMatch()) {
                rmts.add(client.resolve(pmt.getEndpointReference()));
            }

            int i = 0;
            for (ResolveMatchType rmt : rmts) {
                i++;
                String address = rmt.getXAddrs().iterator().next();    //服务端提供服务
                QName qname = rmt.getTypes().iterator().next();
                if (qname.toString().contains("RemoteWSServer")) {
                    RemoteWSServer port = rmt.getEndpointReference().getPort(
                        RemoteWSServer.class);
                    ((BindingProvider) port).getRequestContext().put(
                        BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                        "http://192.168.0.143:8081/remote/services/remoteWSServerRemote");
                    List<String> l = port.list();
                    System.out.println(l);
                }
            }
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bus.shutdown(true);
        }
    }

}

 

 另外,google中也有相同功能的开源项目:https://code.google.com/p/java-ws-discovery/

你可能感兴趣的:(over)