5、运行测试应用
启动Tomcat
再打开浏览器,输入:http://localhost:8080/ws/services,
服务器返回的结果如下:
WebService已经布署成功了,打开WSDL信息:
http://localhost:8080/ws/services/SayHelloService?wsdl。
其中 sayHelloService是配置文件中 service\name 元素所定义的内容,”wsdl”参数表示查看该 Web 服务的 WSDL(Web服务描述语言)文件。
注意:XML的空格或者有些字符在不同的编码下,会出现乱码,到时XML解析会出错。我这边运行这个servers.xml实例,由于编码不符合UTF-8,配置内容是其他编码形式,导致 XML解析报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.codehaus.xfire.spring.ServiceBean' defined in class path resource [META-INF/xfire/services.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: I nvalid property '????' of bean class [org.codehaus.xfire.spring.ServiceBean]: Bean property '????' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter? org.springframework.beans.NotWritablePropertyException: Invalid property '????' of bean class [org.codehaus.xfire.spring.ServiceBean]: Bean property '????' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter? |
6、模拟远程调用
客户端Java代码
package com.test.ws; import java.net.MalformedURLException; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.transport.http.CommonsHttpMessageSender; public class SayHelloClient { public static void main(String args[]) { String serviceURL = "http://localhost:8080/ws/services/SayHelloService"; //创建service对象 Service serviceModel = new ObjectServiceFactory().create(SayHelloService.class); XFireProxyFactory serviceFactory = new XFireProxyFactory(); try{ //获取服务对象 SayHelloService service = (SayHelloService) serviceFactory.create(serviceModel, serviceURL); Client client = Client.getInstance(service); //忽略http连接的超时时间,0为不设置超时时间,》=1为超时毫秒数 client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0"); String msg = service.sayHello("大兵"); System.out.println("服务器对[大兵]的回复:" + msg); msg = service.sayHello(null); System.out.println("服务器对你的回复:" + msg); }catch (MalformedURLException e) { e.printStackTrace(); } } |
客户端运行结果:
服务器对[大兵]的回复:大兵,早上好,还没有去工作吗 ?
服务器对你的回复:你叫什么名字呢?
注意:当客户端设置http连接超时为1毫秒时
Client client = Client.getInstance(service);
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "1");
会出现如下错误提示:
2013-9-12 11:21:02 org.codehaus.xfire.transport.http.HttpChannel sendViaClient 严重: java.net.SocketTimeoutException: Read timed out Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Couldn't send message. org.codehaus.xfire.fault.XFireFault: Couldn't send message. at org.codehaus.xfire.fault.XFireFault.createFault (XFireFault.java:89) at org.codehaus.xfire.handler.OutMessageSender.invoke (OutMessageSender.java:30) at org.codehaus.xfire.handler.HandlerPipeline.invoke (HandlerPipeline.java:13
|
从例子实现的过程中,我们可以发现 XFire 框架最大化的发挥了 POJO 的作用,减少了 SOA 实施时对框架本身的依赖,降低了 SOA 实施的难度,企业实施 SOA 时并不需要增加太多的投入就可以实现目标。
最后附上工程源码