`
lijiang
  • 浏览: 77278 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论
阅读更多
Web Services使我们能够在网络上建立分布式系统,应用程序组件可以通过任何平台、任何语言和任何方式访问。无论应用程序如何开发,使用了什么语言,以及运行在什么操作系统平台上,只要它作为Web Service,并且为协同解决问题而设计,那么你的应用程序,以任何语言开发或在任何平台上,都可以利用它的服务。这是Web Service的主要概念。

  为了实现Web Services的平台无关性和实现访问独立性,软件行业需要遵循一些作为标准的技术。其中一些包括:

  ---XML:在Web Services环境中各层之间进行传递的默认数据格式。

  ---SOAP:封装和交换信息的默认协议。第一次被提出时,它是只取Simple Object Access Protocol(简单对象访问协议)的首字母。但是现在SOAP更多被认为是一个特定的名词,以它自己而命名,同样很多人认为这是用词不当:SOAP实际上不是用来访问对象的。另外,它也不再简单。

  ---WSDL(Web Services Description Language,Web Services描述语言):描述Web Services的语言。尽管基于XML并且可以被人理解,WSDL主要是由机器处理,由客户端程序读取和理解。

  下面的高级层次图表,基于WWW协会发布的“Web Services Architecture”(Web Services架构)文档,显示了这些技术在实际的工作环境中是如何发挥作用:

  这个流程图显示了Web Services中的核心技术是如何工作的。

  这里,Provider是提供服务的应用程序组件,Requester是使用服务的客户端程序。很多其他技术也会参与到交互中,但是这个图只显示了在Web Services环境中必需的核心技术组件。

  XFire是一个免费的开源SOAP框架,它不仅可以极大方便地实现这样一个环境,并且可以提供许多Web Services规范中高级特征,这些特征在多数的商业或者开源工具都没有提供。你要恰当的理解这些单词:great ease and simplicity(非常轻松和简单)。你将会看到使用XFire创建Web Services是多么的简单。
  假如你的Web应用有一个Java类,并且你想把它的一个方法发布为Web Services,当使用XFire时,你不需要编写一行额外的Java代码。只需要编辑发布描述符,然后你就会得到一个Web Services。是的,它相当地简单。我们来看一个例子。

  一个简单的Java类

  我们的例子是一个银行业应用程序,服务器是运行在 J2SE1.4.2_07下的Apache Tomcat5.5.7。假定你已经了解如何使用Java编写Web应用程序,并知道应该如何把它部署到Apache Tomcat服务器上。我们的Web应用程序非常简单;它只做一件事——将资金从一个账户转到另外一个账户上。一个普通的Java类 BankingService包含了一个叫做transferFunds()的方法来为我们完成这项工作。它需要四个输入参数:

    *   1、 String fromAccount
    *   2、 String toAccount
    *   3、 double amount
    *   4、 String currency

  代码如下:

package com.mybank.xfire.example;

import java.text.NumberFormat;
import java.text.DecimalFormat;

/** XFire WebServices sample implementation class.
*/
public class BankingService implements IBankingService {
   
     //Default constructor.
     public BankingService(){   
     }
   
     /** Transfers fund from one account to another.
     */
     public String transferFunds(
         String fromAccount, String toAccount, double amount, String currency){
       
         String statusMessage = "";
                     
         //Call business objects and other components to get the job done.
         //Then create a status message and return.
         try {
             NumberFormat formatter = new DecimalFormat("###,###,###,###.00");      
             statusMessage = "COMPLETED: " + currency + " " + formatter.format(amount)+
             " was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount;
         } catch (Exception e){
             statusMessage = "BankingService.transferFunds(): EXCEPTION: " + e.toString();
         }
         return statusMessage;
     }
   
}
  在这里你看到了什么异常的东西了吗?或许没有,除了默认的构造函数,类型是public。这是必须的。否则,XFire不能够初始化这个类。
 

  因为使用接口的设计是一个好的实践,所以我们的Java类也实现了一个称为IBankingService的接口。代码十分简单:

package com.mybank.xfire.example;

public interface IBankingService { 

     public String transferFunds(
         String fromAccount, String toAccount, double amount, String currency);
       
}

  在实际实现中,这样一个方法可能包括各种类型的复杂调用、查询和处理操作。但是我们的示例代码已经最小化了,以至于我们可以集中精力在主要目标上:把这个方法发布为Web Services。

  你可以看到BankingService是一个普通的Java类,没有任何代码告诉我们它将会在Web Services中使用。好的,这里我们不需要增加任何东西。我们所有的工作都在部署描述符里完成。

  Web应用的部署描述符

  在Java中,Web应用程序通常需要至少一个部署描述符(叫做web.xml)对其进行配置。XFire本身是一个基于servlet的应用程序。因此,我们需要增加必要的引用到描述符文件中。然后我们还必须配置将要创建的Web Services。我们使用一个称为services.xml的新文件来完成这件事。

  web.xml

  首先,修改web.xml。我们需要增加下面的XFire servlet相关的条目:

<servlet>
         <servlet-name>XFireServlet</servlet-name>
         <display-name>XFire Servlet</display-name>
         <servlet-class>org.codehaus.xfire.transport.http.XfireConfigurableServlet
          </servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>XFireServlet</servlet-name>
         <url-pattern>/servlet/XFireServlet/*</url-pattern>
     </servlet-mapping>
   
     <servlet-mapping>
         <servlet-name>XFireServlet</servlet-name>
         <url-pattern>/services/*</url-pattern>
     </servlet-mapping>

 

  services.xml

  现在我们不得不说一下我们的Web Services的由什么组成的了。这由一个叫做services.xml的文件完成,它存放在META-INF/xfire目录下,而这整个目录放在 WEB-INF/classes文件夹中,它在Web应用程序的标准类路径中。这里是services.xml中的基本配置条目:

<beans xmlns="http://xfire.codehaus.org/config/1.0">
 
   <service>
     <name>Banking</name>
     <namespace>mybank</namespace>
     <serviceClass>com.mybank.xfire.example.IBankingService</serviceClass>
     <implementationClass>com.mybank.xfire.example.BankingService</implementationClass>
   </service> 
 
</beans>

  让我们看看这里都包含了什么内容。Web Services的定义包含在元素中,它还含有一些子元素。第一个子元素是,它可以是你提供任何的合法名字。这将会被客户端程序和其它需要定位你的服务的组件用到。例如,在服务准备好以后,你将在浏览器上使用这个名字来查看WSDL。

  下一个子元素是<namespace>。任何合法的XML名字都是可以的。<namespace>用来唯一标识你的服务的各个参数。
< serviceClass>元素包含了Java类的名字,它指定了方法签名。在我们的例子中,它是接口IBankingService。如果 Java类没有实现任何接口,你就需要把类的名字放在这里。在你的Java类或者接口中可能有几个方法。只需要一个入口把它们全部发布为Web Services。

  <implementationClass>保存了实现方法的Java类名。这是一个可选元素。如果上一个元素<serviceClass>包含了一个接口,那么相应的实现类必须在这里指定。

  就是这样。我们的Web Services配置完成了。

  XFire和其它类库

  现在是最后一步了,需要得到所有必需的类库。我们怎样得到它们呢?去XFire网站,下载xfire-distribution-1.0.zip,然后解压到一个本地文件夹。复制下面的jar文件和它的库文件夹到WEB-INF/lib中:

    *   • activation-1.0.2.jar
    *   • commons-codec-1.3.jar
    *   • commons-httpclient-3.0.jar
    *   • commons-logging-1.0.4.jar
    *   • jaxen-1.1-beta-8.jar
    *   • jdom-1.0.jar
    *   • log4j-1.2.x.jar
    *   • mail-1.3.3_01.jar
    *   • spring-1.2.x.jar
    *   • stax-api-1.0.jar
    *   • wsdl4j-1.5.2.jar
    *   • wstx-asl-2.9.jar
    *   • xbean-2.1.0.jar
    *   • xbean-spring-2.2.jar
    *   • xfire-all-1.0.jar
    *   • XmlSchema-1.0.jar

  一切妥当。我们来部署和启动应用程序。为了部署示例应用,只需要复制websvc.war到Apache Tomcat的webapps文件夹中,再等待几秒钟。它将会自动启动。这个应用的全部源代码也包含在这个war文件中。我们的程序已经准备作为一个 Web Service了。

  我们如何知道Web Service正在工作呢?

  为了了解Web Service是否正在工作,我们需要测试。首先,我们测试来看WSDL是否可用。我们在浏览器中输入URL。哪个URL?因为我们的应用程序的war文件是websvc.war,并且在services.xml中给出的服务名是Banking,WSDL的URL应该是:http: //localhost:8080/websvc/services/Banking?wsdl。

  请注意:URL的第一部分,例如,http://localhost:8080,可能会根据你的应用服务器不同而不同。无论怎样,当你输入URL后,将会看到一个XML文档,它的根元素是。这个文档叫做服务的WSDL。如果你看到了,这就是你的应用作为Web Service已经可用的第一个证明。

  但是这个测试是不够的。可能会发生这种情况,可以看到WSDL,但是从客户端程序可能会访问不到服务。因此为了核实服务是否可以访问了,我们必须使用一个客户端进行服务的实际调用来进行一个真正的测试。

  开发一个客户端

  你可以使用任何的SOAP工具创建客户端,例如,.Net或者Apache Axis,有很多种方法:使用从WSDL产生的stubs,使用动态代理,等等。在例子中,我们使用一个动态代理,以一个简单的Servlet形式,叫做 WsClient.java。为了保持代码两最小,所有在屏幕显示的元素都放在了doGet()方法中。对Web Service的实际调用由callWebService()方法完成,它相当地简单。和下面的类似:

/* Call the Web service
     *
     */
     public String callWebService(
         String fromAccount, String toAccount, double amount, String currency)
         throws MalformedURLException, Exception {
       
         //Create a metadata of the service     
         Service serviceModel = new ObjectServiceFactory().create(IBankingService.class);       
         log.debug("callSoapServiceLocal(): got service model." );
  
         //Create a proxy for the deployed service
         XFire xfire = XFireFactory.newInstance().getXFire();
         XFireProxyFactory factory = new XFireProxyFactory(xfire);     
   
         String serviceUrl = "http://localhost:8080/websvc/services/Banking";
       
         IBankingService client = null;
         try {
             client = (IBankingService) factory.create(serviceModel, serviceUrl);
         } catch (MalformedURLException e) {
             log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());
         }   
              
         //Invoke the service
         String serviceResponse = "";
         try {
             serviceResponse = client.transferFunds(fromAccount, toAccount, amount, currency);
        } catch (Exception e){
             log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());                
             serviceResponse = e.toString();
         }       
         log.debug("WsClient.callWebService(): status=" + serviceResponse);             

         //Return the response
         return serviceResponse;
     }

  这个代码是如何工作的呢?我来解释一下:首先,我们创建一个服务模型,它包含服务的说明——换句话说,就是服务的元数据。我们使用XFire的ObjectServiceFactory从IBankingService.class接口创建这个模型。

  接着,为XFire获得一个代理工厂对象,它包含了常规的代码,也相当地简单和易懂。这一步中没有任何特定应用的东西。从这个proxyFactory,使用服务模型和服务端点URL(用来获得WSDL),我们可以得到一个服务的本地代理。

  就是它了。这个代理就是实际的客户端。现在,我们可以调用它的transferFunds()方法来得到我们需要的Web Service。

  一旦示例应用发布并启动,就可以尝试servlet URL:

  http://localhost:8080/websvc/ws。

  这个Servlet使用默认参数来调用Web Service和显示接收到的响应。页面的最后两行应该读取:

  Response Received
  COMPLETED: CDN$ 500.00 was successfully transferred from A/C# 11111-01234 to A/C# 99999-05678

  现在你可以确定Web Service已经发布并且在运行中了。

  为了尝试不同的输入值,你可以使用完整的URL,例如:

  http://localhost:8080/websvc/ws?from=11-2345&to=77-9876&amt=250.00&cur=EUR。

  基本的Web Services开发步骤清单

  这个清单总结了将一个Java方法发布为Web Service所必须的步骤:

    *   1、 检查Java类的方法和默认构造函数确保为public
    *   2、 增加XFire servlet相关条目到web.xml中
    *   3、 创建services.xml,把它放到WEB-INF/classes/META-INF/xfire目录下
    *   4、 增加XFire和第三方包到你的Web应用的WEB-INF/lib文件夹中

  这就是所有需要的步骤,是的,相当简单。

  XFire的其他高级特性

  XFire的使用可能比较简单,但是在特性和功能性上,它却占据着领导者的位置。下面是它的高级特性:

    *   ---本地数据绑定支持POJOs(plain-old Java objects)、XMLBeans、JAXB(Java Architecture for XML Binding)、Castor等等。数据绑定说明了Web Services的XML请求和映射到Java对象的XML响应。
    *   ---使用StAX(Streaming API for XML)处理XML文档。同DOM的基于树和SAX的事件驱动的处理方式相比,StAX使用了拉(pull)机制,它使处理更快速,内存效率更高。
    *   ---支持多种传输协议,如HTTP、JMS(Java Message Service)和JVM内部传输。
    *   ---嵌入式,这是XFire的核心功能之一。你可以把这个SOAP引擎嵌入到你的应用中,完全隐藏所有XFire特定引用,同样所有配置都是程序驱动。
    *   ---丰富的API,它使XFire可高度自定义,允许开发者在不同的阶段截获请求,并且对他们进行处理。
    *   ---兼容最新的标准例如SOAP1.1(没有加密远程工程调用,或者RPC)和1.2、WSDL1.1、the Web Services Interoperability Organization’s Basic Profile 1.0、Web Services Addressing和WS-Security。

  最重要的是,XFire属于新一代 Web Services引擎。不仅仅是营销用语,“新一代”有一些重要的意义。Apache Axis是第一代Java语言的Web Services引擎,已经成为了所有后来工具的参考标准。在过去的几年里,Axis以及这些其它的工具已经在很多生产环境中进行了实地测试。从中得出的一个关键的问题就是Web Services并不最适合RPC类型的通信。对于性能和效率,面向文档的消息形式是最好的方式。但是Apache Axis和很多其他的Web Services引擎都被设计成了面向RPC的(尽管它们支持文档形式)。现在,整个行业正在开发新一代的SOAP引擎,设计为面向文档的。Apache 已经宣布结束旧版本的Axis引擎开发,现在专注于Axis2,现在它的预发布版本是0.95。XFire在今年的2月份发布了它的第一个产品版本 (1.0)。它的下一个版本(1.1)仅仅在几个星期之后就进行了发布。

  性能

  Web Services需要消耗很多资源,但是性能方面它们不是那么引人注目。XFire打破了这种趋势。它消耗更少的内存(部分因为 StAX的使用),但是表现却比多数可比较的SOAP引擎出色。你可以在资源中提供的链接中看到比较的结果。

  此外,XFire还提供了各种方法来进一步优化性能。一个方法是使用JVM内置传输(in-JVM transport)。如果你知道Web Services和客户端运行在同一个JVM上,你可以选择使用本地传输,它可以大幅提升性能。在示例中的客户端代码,看以下指定服务端点URL的这行:

  String serviceUrl = "http://localhost:8080/websvc/services/Banking";

  替换为

  String serviceUrl = "xfire.local://Banking";

  你会看到性能上的明显提高,因为它绕过了整个网络层。

  局限性

  XFire有些重要的局限性你应该清楚:

    *   ---开发Web Services的好的实践是从WSDL开始。大部分的SOAP引擎提供了从WSDL生成服务stub的工具。XFire也提供了这样一个工具。但是它是基于注释(annotations-based)的,因此需要J2SE5.0。对于仍坚持使用J2SE1.4.x的人来说,它不是一个令人拍手叫好的工具,因为我们有很多其他方式来编写客户端,一个就是文章中演示的方式。
    *   ---附件支持,它将会包含在未来发行的版本中。
    *   ---易于学习的用户向导。XFire团队在这个方面还有很多工作需要做。
分享到:
评论

相关推荐

    Learn Amazon Web Services in a Month of Lunches 无水印原版pdf

    Learn Amazon Web Services in a Month of Lunches 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn...

    Apache Axis2 Web Services, 2nd Edition.pdf

    Chapter 11, Developing JAX-WS Web Services - Learn the fundamentals of developing JAXWS based web services, the most popular web service development technology used by Java developers. Chapter 12, ...

    Website Hosting and Migration with Amazon Web Services

    Website Hosting and Migration with Amazon Web Services: A Practical Guide to Moving Your Website to AWS What You’ll Learn Evaluate Amazon Web Services (AWS) offered on the platform that may benefit ...

    Amazon Web Services in Action(Manning,2015)

    Amazon Web Services in Action introduces you to computing, storing, and networking in the AWS cloud. You'll start with a broad overview of AWS and learn how to spin-up servers manually and from the ...

    docker on amazon web services

    Docker on Amazon Web Services is for you if you want to build, deploy, and operate applications using the power of containers, Docker, and Amazon Web Services. Basic understanding of containers and ...

    building restful web services with spring 5 2e

    Building RESTful Web Services with Spring 5 – Second Edition: Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0 Find out how to implement the REST architecture to build resilient ...

    Amazon Web Services in Action

    Amazon Web Services in Action introduces you to computing, storing and networking in the AWS cloud. You'll start with a broad overview of AWS and learn how to spin up servers manually and from the ...

    Building RESTful Web Services with Java EE 8

    Learn the fundamentals of Java EE 8 APIs to build effective web services Java Enterprise Edition is one of the leading application programming platforms for enterprise Java development. With Java EE 8...

    Apache Axis2 Web Services, 2nd

    Extensive and detailed coverage of the enterprise ready Apache Axis2 Web Services / SOAP / WSDL engine. Attain a more flexible and extensible framework with the world class Axis2 architecture. Learn ...

    Mastering Go Web Services

    About This Book, Effectively ...What You Will Learn, Familiarize yourself with RESTful practices and apply them in Go Acquaint yourself with the best practices for API design such as URL routing and ...

    RESTful Web Services Cookbook

    You'll learn ways to design RESTful web services for client and server applications that meet performance, scalability, reliability, and security goals, no matter what programming language and ...

    Beginning.Amazon.Web.Services.with.Node.js.1484206541

    Beginning Amazon Web Services with Node.js teaches any novice Node.js developer to configure, deploy, and maintain scalable small to large scale Node.js applications in Amazon Web Services. Hosting a ...

    Building.RESTful.Python.Web.Services.epub

    Create web services that are lightweight, maintainable, scalable, and secure using the best tools and techniques designed for Python About This Book Develop RESTful Web Services using the most popular...

    PHP.Web.Services.APIs.for.the.Modern.Web.2nd.Edition

    Learn new material on working with and publishing web hooks Table of Contents Chapter 1. HTTP Chapter 2. HTTP Verbs Chapter 3. Headers Chapter 4. Cookies Chapter 5. JSON Chapter 6. XML Chapter 7. RPC ...

    Web Services Testing with soapUI

    Become more proficient in testing web services included in your service-... Learn with clear step-by-step instructions and hands-on examples on various topics related to web services testing using soapUI

Global site tag (gtag.js) - Google Analytics