本文也是参照网上的教程,自己写的cxf的两个小例子,一个是普通的java application,一个是和spring整合的web application,想通过两者的对比进行更加深入的理解
项目地址: http://download.csdn.net/detail/u012049463/6722275
服务端:1. 先定义一个服务接口,然后实现这个借口,最后进行发布。
代码如下:
User.java,定义一个服务需要的对象:
package com.unj.service2; public class User { int id; String name; String address; //get、set方法 @Override public String toString() { return "User [id=" + id + ", name=" + name + ", address=" + address + "]"; } }
CxfService.java,服务接口,这里值得注意的就是几口上面的@webservice注解:
package com.unj.service2; import javax.jws.WebService; @WebService public interface CxfService { public User getUser(int id); public boolean addUser(User user); }
CxfServiceImpl.java, 服务实现:
package com.unj.service2; import javax.jws.WebService; @WebService public class CxfServiceImpl implements CxfService { @Override public User getUser(int id) { User user = new User(id, "unj", "wuhan"); return user; } @Override public boolean addUser(User user) { try { if (Integer.valueOf(user.getId()) != null) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } }
CxfPublish.java, 对服务进行发布:
package com.unj.service2; import java.io.IOException; import javax.xml.ws.Endpoint; public class CxfPublish { public static void main(String[] args) throws IOException { CxfServiceImpl cws = new CxfServiceImpl(); Endpoint.publish("http://localhost:1234/cxfWebservice", cws); } }
然后通过浏览器,输入“http://localhost:1234/cxfWebservice?wsdl” 就可以看到我们发布的websense的xml内容。
下面编写客户端调用我们之前写的webservice。
首先我们得下载cxf然后安装,下载地址:http://www.apache.org/dyn/closer.cgi?path=%2Fcxf%2F2.3.1%2Fapache-cxf-2.3.1.zip 下载之后其实并没有安装文件,我们需要解压缩自后进行环境变量的配置,假如我们将cxf zip文件解压到了D:/javaApp目录下,我们需要配置的环境变量如下:
在CLASSPATH中添加 D:\JavaApp\apache-cxf-2.7.5\lib;
新建CXF_HOME D:\JavaApp\apache-cxf-2.7.5;
在PATH中添加 D:\JavaApp\apache-cxf-2.7.5\bin
配置好之后,我们cmd进入到bin目录中,运行wsdl2java http://localhost:1234/cxfWebservice?wsdl,运行之后会生成一个文件夹,(上面是根据web url进行转化,还有种方法是根据wsdl文件进行转化,如test.wsdl,转化命令为:
wsdl2java -p com.unj -d d:/abc -all test.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
package com.unj.client2; import com.unj.service2.CxfServiceImpl; import com.unj.service2.CxfServiceImplService; import com.unj.service2.User; public class Client { public static void main(String[] args) { CxfServiceImplService impl = new CxfServiceImplService(); CxfServiceImpl server = impl.getCxfServiceImplPort(); User user = new User(); user.setId(001); boolean seaved = server.addUser(user); System.out.println(seaved); } }
以上是通过javaapplication来开发和调用的webservice,下面我们再来看看spring和cxf整合起来的webService,这个项目是用maven开发,所以在导入到自己的eclipse时注意区别。
步骤: 1. 定义接口 2.实现接口 3.配置cxf-beans 4. 配置web.xml
首先要通过maven导入jar包: cxf-rt-frontend-jaxws, cxf-rt-transports-http,spring-context,spring-web;
cxfService.java:
package com.unj.service2; import javax.jws.WebService; @WebService public interface CxfService { public User getUser(int id); public boolean addUser(User user); }
package com.unj.service2; import javax.jws.WebService; @WebService public class CxfServiceImpl implements CxfService { @Override public User getUser(int id) { User user = new User(id, "unj", "wuhan"); return user; } @Override public boolean addUser(User user) { try { if (Integer.valueOf(user.getId()) != null) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-autowire="byType" default-lazy-init="true"> <description>使用Apache CXF的Web Service配置文件,以下三个为固定配置文件(不需要创建) </description> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <!--在这里配置相应内容 --> <!--id:随意配,implementor:指定接口具体实现类,address:随意配,访问时会用到,下面会做说明 --> <jaxws:endpoint id="userService" implementor="com.unj.service2.CxfServiceImpl" address="/userService"> </jaxws:endpoint> </beans>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--cxf start --> <!--用于加载cxf-beans.xml配置信息 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/cxf-beans.xml</param-value> </context-param> <!--使用spring ContextLoaderListener 加载cxf-beans.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置CXFServlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <!-- url可自定义配置,用于CXFServlet请求地址拦截,访问会用到 --> <url-pattern>/services/*</url-pattern> </servlet-mapping> <!--cxf end --> </web-app>
然后启动项目,在浏览器输入“http://localhost:8080/cxfWSTest/services/userService?wsdl”就可以看见我们发布的webservice的wsdl。
服务端发布后,我们再来看看客户端的实现。
步骤:1. 通过cxf将wsdl转化为java代码并拷贝到项目中, 2. 配置beans.xml
进入到cxf的bin目录,运行wsdl2java http://localhost:8080/cxfWSTest/services/userService?wsdl ,然后将生成的代码拷贝进项目。
cxfClient.java:
package com.unj.invoke; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.unj.service2.CxfService; import com.unj.service2.User; public class CxfClient { public static void main(String[] args) { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");//此处beans.xml放在src下,也需要放在其他目录下,但需要注明清楚 //获取webservice服务的操作接口 CxfService userService = (CxfService)app.getBean("userServeiceClient"); User user = userService.getUser(1); System.out.println(user); } }
这样就实现了cxf与spring的整合。
另外附上cxf客户端调用webservice的几种方式:http://blog.csdn.net/liaomin416100569/article/details/5503410
public static void invokeMethod1() {
long s=new Date().getTime();
UserServiceImplService serivce = new UserServiceImplService();
UserServiceImpl impl = serivce.getUserServiceImplPort();
User u = new User();
impl.addUser(u);
long s1=new Date().getTime();
System.out.println(s1-s);
//第一次去调用1297
//1437 1062 1063 1078 1047
}
public static void invokeMethod2() {
long s=new Date().getTime();
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8088/abc");
QName SERVICE = new QName("http://liaomin", "UserServiceImplService");
factory.setServiceName(SERVICE);
factory.setServiceClass(UserService.class);
UserService us = (UserService) factory.create();
User u = new User();
// us.addUser(u);
us.addUser(u);
long s1=new Date().getTime();
System.out.println(s1-s);
//第一次去调用1265
//1047 1047(比较稳定)
}
public static void invokeMethod3() throws MalformedURLException {
long s=new Date().getTime();
QName SERVICE = new QName("http://liaomin", "UserServiceImplService");
QName UserServiceImplPort = new QName("http://liaomin", "UserServiceImplPort");
URL url = new URL("http://localhost:8088/abc?wsdl");
ServiceDelegate dele=Provider.provider().createServiceDelegate(url,SERVICE,Service.class);
UserService us = (UserService) dele.getPort(UserServiceImplPort,UserService.class);
User u = new User();
us.addUser(u);
long s1=new Date().getTime();
System.out.println(s1-s);
//第一次去调用 1281
//1047 1031 1047 1016 1032
}
public static void invokeMethod4()
{
long s=new Date().getTime();
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://localhost:8088/abc");
// factory.getServiceFactory().setDataBinding(new AegisDatabinding());
UserService client = (UserService) factory.create();
User u = new User();
client.addUser(u);
long s1=new Date().getTime();
System.out.println(s1-s);
//第一次去调用 1188
//调用一次后 1016 1000 1016 1015
}
通过实践去测试 只有第四种调用耗费的时间最少的
如果在频繁取数据的系统中 能优化100ms的速度 对性能就会有大大的提高
比如我调用 100次webservice发送消息 每发送一次 是 1200ms(1.2s) 总共耗时 120s
如果采用第四种 只需要 1000ms(1s) 总共耗时 100s 那么优化了20s时间 20s是个什么概念
假如是调用 1000 10000次发送了 优化的效率就更大