现在的位置: 首页 > 综合 > 正文

cxf工作原理

2014年08月29日 ⁄ 综合 ⁄ 共 3065字 ⁄ 字号 评论关闭

(转载自 http://yonge812.iteye.com/blog/1893117

最近使用了一下cxf,简单的查看了部分源代码,给我的感觉它就是一个可以大大简化我们客户端编写远程方法调用的一个工具框架,只需要简单的几行代码就可以解决这种复杂的问题,下面就举个例子:

Java代码  收藏代码
  1. package com.yonge.cxf;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.apache.cxf.frontend.ClientProxyFactoryBean;  
  6. import org.apache.cxf.transport.jms.spec.JMSSpecConstants;  
  7.   
  8. import com.erayt.solar2.adapter.config.Weather;  
  9. import com.erayt.solar2.adapter.fixed.HelloWorld;  
  10.   
  11. public class CXFClientTest {  
  12.   
  13.     public static void main(String[] args) {  
  14.         ClientProxyFactoryBean factory = new ClientProxyFactoryBean();  
  15.         factory.setTransportId(JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);  
  16.         factory.setAddress("http://localhost:9000/Hello");  
  17.         HelloWorld hello = factory.create(HelloWorld.class);  
  18.         Weather weather1 = hello.getWeather(new Date());  
  19.         System.out.println("forecast:" + weather1.getForecast());  
  20.     }  
  21. }  

上面一段是客户端调用远程服务器中HelloWorld接口的getWeather方法 的一个具体实现。服务端的代码如下:

Java代码  收藏代码
  1. package com.erayt.solar2.adapter.driver;  
  2.   
  3. import org.apache.cxf.frontend.ServerFactoryBean;  
  4.   
  5. import com.erayt.solar2.adapter.config.HelloWorldImpl;  
  6. import com.erayt.solar2.adapter.fixed.HelloWorld;  
  7.   
  8. public class CXFServer {  
  9.   
  10.     protected CXFServer() throws Exception {  
  11.         HelloWorld helloworldImpl = new HelloWorldImpl();  
  12.         ServerFactoryBean svrFactory = new ServerFactoryBean();  
  13.         svrFactory.setServiceClass(HelloWorld.class);  
  14.         svrFactory.setAddress("http://localhost:9000/Hello");  
  15.         svrFactory.setServiceBean(helloworldImpl);  
  16.         svrFactory.create();  
  17.     }  
  18.   
  19.     public static void main(String args[]) throws Exception {  
  20.         new CXFServer();  
  21.         System.out.println("Server ready...");  
  22.   
  23.         Thread.sleep(50 * 60 * 1000);  
  24.         System.out.println("Server exiting");  
  25.         System.exit(0);  
  26.     }  
  27. }  

 服务端将接口以服务的形式发布后,必须提供客户端访问服务的地址(http://localhost:9000/Hello),客户端可以根据提供的地址访问服务端相关服务的wsdl文件,客户端可以根据wsdl文件生成对应的客户端代码,也就是HelloWorld接口文件,然后客户端可以像调用本地接口方法一样,去调用远程方法,客户端与服务端之间的交互是通过代理完成的,所以开发在编程时不需要关系他们是如何交互的,在代理中,上面的客户端请求hello.getWeather方法时会向服务端发送如下的消息:

Xml代码  收藏代码
  1. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  2.     <soap:Body>  
  3.         <ns1:getWeather xmlns:ns1="http://fixed.adapter.solar2.erayt.com/">  
  4.             <arg0>2013-06-22T18:56:43.808+08:00</arg0>  
  5.         </ns1:getWeather>  
  6.     </soap:Body>  
  7. </soap:Envelope>  

 服务器端接收报文后,会解析报文,按照报文的信息执行相应的本地方法,然后将返回值又构造一个报文响应给客户端,如下:

Xml代码  收藏代码
  1. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  2.     <soap:Body>  
  3.         <ns1:getWeatherResponse xmlns:ns1="http://fixed.adapter.solar2.erayt.com/">  
  4.             <return>  
  5.                 <forecast>Cloudy with  
  6.                     showers  
  7.                 </forecast>  
  8.                 <howMuchRain>4.5</howMuchRain>  
  9.                 <rain>true</rain>  
  10.                 <temperature>39.3</temperature>  
  11.             </return>  
  12.         </ns1:getWeatherResponse>  
  13.     </soap:Body>  
  14. </soap:Envelope>  

 代理接收消息后,会将他解析、包装成对象返回给客户端,最后开发就看到了服务器返回的数据了。在这里只介绍一下我理解的工作原理,详情可以去看官网的介绍http://cxf.apache.org/

抱歉!评论已关闭.