Getting started with xfire

Source: Internet
Author: User

Pojo and SOA Overview

Reviewed pojo

Pojo (plain old Java object, simple Java object) is a JavaCommunityThe oldest Member in (think back to the first exciting moment when you were learning Java, the simple "Hello world! "Example.

However, in reality, Java has developed far beyond the scope of pojo and has become the most successful application of object-oriented technology.Programming LanguageIn particular, the inheritance and polymorphism applications have created a large number of development frameworks (such as struts) and standards (such as EJB) for us, and the complexity of implementation is followed, we must face a lot of limitations on inheritance relationships. For example, to develop a Struts-based application, we must understand the specific inheritance relationships of struts, such as actionform and validateactionform. To develop an EJB application, we must inherit ejbobject and sessionejb.

In order to put aside these restrictions and reduce the difficulty of Java application implementation, the Java Community began to review the value of pojo and tried to maximize the role of pojo. The latest effort was ejb3.0. The Java Community designs ejb3.0 Based on pojo, rather than preparing more inheritance relationships and other restrictions for it.

A fascinating SOA

SOA has become a hot term in the Java Community. Almost all software vendors are discussing it and providing solutions and product support for it, most enterprises have implemented or are considering implementing SOA.

However, the implementation of SOA in an enterprise is not a simple task. Even if the new system is directly implemented based on the SOA architecture, it is not easy to incorporate existing enterprise systems into the SOA framework. Enterprises must carry out large-scale transformation of existing systems on the basis of in-depth understanding of the current architecture to meet new requirements. How to economically switch from the original technical architecture to the SOA architecture has become a challenge for many enterprises.



Back to Top

Xfire Overview

Xfire is an open-source framework provided by codehaus. It builds a bridge between pojo and SOA. Its main feature is to support publishing pojo into a web service in a very simple way, this processing method not only gives full play to the role of pojo, simplifies the steps and processes for converting Java applications into web services, but also directly reduces the difficulty of SOA implementation, it provides a simple and feasible way for enterprises to switch to the SOA architecture.

The latest version of xfire is 1.2.2. Currently, the following features are supported:

    • Web services can be bound to pojo, xmlbeans, jaxb1.1, jaxb2.0, And Castor;
    • Supports access to Web Services Based on HTTP, JMS, XMPP, and other protocols;
    • Supports a variety of important standards in the Web service industry, such as SOAP, WSDL, WS-Addressing, and WS-Security;
    • Jsr181 is supported. You can configure web services through jdk5;
    • High-performance SOAP implementation;
    • Server and clientCodeAuxiliary generation;
    • Support for spring, Pico, plexus, and other projects.



Back to Top

Xfire installation package

The latest version of the xfire framework is 1.2.6. You can visit xfire.codehaus.org to download the installation package of the xfire framework. during download, select "all binary release packages (Binary distribution in zip package )", instead of just "xfire JAR file (jar of all xfire modules )".

After the download is complete, we can download. decompress the ZIP file to any folder (% xfire_home % is used in the subsequent chapter to indicate the installation directory of the xfire framework). The structure of the extracted file directory is as follows:

    • API (directory)

      The API directory contains the API documentation corresponding to all classes in the xfire framework, which helps developers use xfire to complete application development.

    • Examples (directory)

      The examples directory contains all instances released with the xfire binary package, includingSource codeAnd Related Web application configuration content.

    • Lib (directory)

      The lib directory contains the external support class package (. jar file) required for running xfire. You can select the required support class package based on the xfire features required by different projects. The conservative approach is to include all the external support class packages (. Jar files) in the web Project ).

    • Manual (directory)

      The manual directory contains the help documentation for the xfire framework, from which developers can learn more about and skills for implementing SOA using the xfire framework.

    • Modules (directory)

      The modules directory contains Binary Package files compiled by the xfire framework based on different features. When publishing a web project based on the xfire framework, you can choose to use all. Jar files under this directory, or you can select a XFire-all-1.2.6.jar file.

    • XFire-all-1.2.6.jar

      The binary package file of the xfire framework contains all modules ).

    • License.txt

      The license.txt file contains the xfire Framework Authorization protocol.

    • Notice.txt
    • Readme.txt

      These two files contain some useful information when xfire is released.



Back to Top

Xfire framework supporting environment

The xfire framework is a servlet-based SOA application development framework. To run enterprise applications developed based on the xfire application framework normally, in addition to the xfire framework itself, JDK and servlet containers are also required.

1. Select, download, and install the JDK version

Xfire supports many features. Different features have different requirements for JDK versions. For example, if you choose to publish a Web Service Based on the jsr181 standard in the project, you need to select JDK 5 or later, if you only want to bind the Web service to the simplest pojo, you only need to select jdk1.4.

All JDK versions can be downloaded from the java.sun.com website. For how to install JDK, see Sun's related technical documentation and JDK help documentation.

2. Download and install servlet containers

Xfire is a servlet-based SOA application development framework that requires the support of servlet containers. Xfire supports running in multiple servlet containers, including websphere, WebLogic, and tomcat. To simplify the description, we chose Tomcat (version 5.0.30) as the xfire running container. All the configuration and release steps are for Tomcat, if you use other servlet containers other than tomcat or select another version of Tomcat, the following configuration process and steps may need to be adjusted, make adjustments according to the help document of the actual servlet container.

Tomcat versions can be downloaded from tomcat.apache.org. For how to correctly install the Tomcat server, see the help documentation of the Tomcat server.

3. xalan

Xfire requires support from the xalan project. However, there is no corresponding JAR file in version 1.2.6. Therefore, visit xml.apache.org to download the Binary Package of the xalan project.



Back to Top

Xfire application configuration

In the previous section, we downloaded and installed the xfire installation package and the required support environment. Now we start to learn how to create an xfire application development environment from scratch. All the following configuration and release steps are for the Tomcat (version 5.0.30) server. If other servlet containers are selected, the following configuration process and steps may need to be adjusted, make adjustments according to the help document of the actual servlet container.

1. Create a web application directory and Basic Elements

    1. Create a new Web application directory "xfire" under the % atat_home %/webapps directory"

      [Note] % atat_home % points to the tomcat installation directory.

    2. Create the WEB-INF directory under the xfire directory,
    3. Create the lib directory and classes directory under the WEB-INF directory
    4. Create the web application description file web. xml under the WEB-INF directory. The content of the web. xml file is shown in Listing 1-1.

Listing 1-1 WEB-INF \ WEB. xml

1. <? XML version = "1.0" encoding = "ISO-8859-1"?> 2, <web-app xmlns = "http://java.sun.com/xml/ns/j2ee" 3, xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4, xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 5, version = "2.4"> 6, 7, <display-Name> xfire instance </display-Name> 8, <description> 9, based on xfire example 10, </description> 11, 12, </Web-app>

2. Copy the support class package files required by xfire.

Copy all files under the % xfire_home %/lib directory to the "lib" directory created in "1. Create web application directory and basic elements, copy the % xfire_home %/XFire-all-1.2.6.jar file to the lib directory created in 1, create web application directory and basic elements. Copy all jar files in the xalan installation package and the required jar files to the same "lib" directory.

[Note]To reduce the number of jar files to be copied, developers can select the jar files to be copied as needed, instead of copying them all, visit the xfire site to learn how to copy appropriate class package files as needed.

3. Configure the servlet required for running the xfire framework

Modify the Web. xml file and add the following servlet definition content.

1. <servlet> 2. <servlet-Name> xfireservlet </servlet-Name> 3. <display-Name> xfire servlet </display-Name> 4. <servlet-class> 5. org. codehaus. xfire. transport. HTTP. xfireconfigurableservlet6, </servlet-class> 7, </servlet> 8, 9, <servlet-mapping> 10, <servlet-Name> xfireservlet </servlet-Name> 11, <URL-pattern>/servlet/xfireservlet/* </url-pattern> 12, </servlet-mapping> 13, 14, <servlet-mapping> 15, <servlet-Name> xfireservlet </servlet-Name> 16, <URL-pattern>/services/* </url-pattern> 17, </servlet-mapping>

4. Create the Service Release file services. xml of the xfire framework.

    1. Create a directory under the classes directory created in create web application directory and basic elements META-INF \ xfire ";
    2. Create the services. xml file under the "xfire" file directory created in Step A). The default content of the file is shown in Listing 1-2.

Listing 1-2 WEB-INF \ Classes \ META-INF \ xfire \ Services. xml

1. <beans xmlns = "http://XFire.codehaus.org/config/1.0"> 2. </beans>



Back to Top

Publish pojo as a Web Service

In the xfire framework, pojo can be published as a Web service in two ways:

    • One way is to directly use the Web Service Interface and Web Service implementation class (pojo) for release;
    • Another method is to publish the annotated pojo as a Web Service Based on the jsr181 standard and annotation technology;

In the following chapter, we will learn how to use the first method to publish pojo Web Services. We will use the classic "Hello world !" This example demonstrates the steps required to publish pojo as a web service. However, we do not simply access a Java method to output "Hello World !" String, but is implemented in the SOA environment: the Web service client accesses the server and publishes it to the pojo of the Web service to obtain the returned "Hello World !" String and then output to the console of the client.

The basic steps for publishing pojo to a Web Service are as follows:

    1. Create a Web Service Interface and declare the interface exposed by the web service;
    2. Create a web service implementation class to provide implementation for Web service interfaces;
    3. Modify the Service Release file ---- services. xml of the xfire framework and publish pojo as a web service.

Create "Hello world !" Example to illustrate how to implement these three steps.

1. Create a Web Service Interface ---- helloworldservice

To publish a pojo as a web service, you must first create a web service interface to declare the interface that the Web service needs to be exposed.

Create the "helloworldservice" Web service interface as needed and declare a "sayhello" method in it. This method returns the content of the "string" type ." The Java file code corresponding to the helloworldservice interface is shown in List 1-3.

Listing 1-3 WEB-INF \ Classes \ org \ vivianj \ xfire \ pojo \ helloworldservice. Java

 
1. package Org. vivianj. xfire. pojo; 2.3. /** 4. * All Java Methods declared in helloworldservice that need to be published as Web Services 5. * helloworldservice serves as a Web Service Interface 6. */7. public interface helloworldservice {8. /** 9. * The sayhello method declares the interface exposed by the web service 10.*11. * @ return refers to the string 12 returned to the client. */13. public String sayhello (); 14 .}

2. Create a web service implementation class "helloworldserviceimpl"

Create a web service implementation class "helloworldserviceimpl", which inherits the helloworldservice interface created in "1. Create a Web Service Interface-helloworldservice" and provide specific implementation for the "sayhello" method it declares: returns the string "Hello World! ". The Java file code corresponding to the "helloworldserviceimpl" class is shown in Listing 1-4.

Listing 1-4 WEB-INF \ Classes \ org \ vivianj \ xfire \ pojo \ helloworldserviceimpl. Java

 
1. package Org. vivianj. xfire. pojo; 2.3. /** 4. * helloworldserviceimpl provides specific implementation for all Java Methods declared in the Web service interface. 5. * helloworldserviceimpl is a Web service implementation Class 6. */7. public class helloworldserviceimpl implements helloworldservice {8.9. /* 10. * The sayhello method provides specific implementation 11 for the sayhello method defined by the helloworldservice service interface. * 12. * @ see Org. vivianj. xfire. pojo. helloworldservice # sayhellotoxfire () 13. */14. public String sayhello () {15. return "H Ello world! "; 16.} 17.18 .}

3. Modify services. xml and publish pojo as a Web Service

We can <beans…> In the WEB-INF \ Classes \ META-INF \ xfire \ Services. xml file Add the following XML content to the </beans> element to publish the created helloworldservice as a web service.

1. <service> 2. <Name> helloworldservice </Name> 3. <namespace> http://vivianj.org/HelloWorldService </namespace> 4. <serviceclass> 5.org. vivianj. xfire. pojo. helloworldservice6. </serviceclass> 7. <implementationclass> 8.org. vivianj. xfire. pojo. helloworldserviceimpl9. </implementationclass> 10. </service>

The functions of each element are as follows:

    • Service

      The service tag and Its XML content provide a complete description of the pojo published as a web service.

    • Name

      The unique name used when the web service is published.

    • Namespace

      The namespace used when the web service is released.

    • Serviceclass

      The full name of the web service interface class, including the package name and class name.

    • Implemetationclass

      The full name of the web service implementation class, including the package name and class name.

For more sub-elements of service elements and their usage, refer to the xfire site.

Through the above three steps, we have released the newly created helloworldservice as a web service. We can use the following steps to test whether the created web service can run properly:

    1. Compile the Java interfaces and classes created in the above steps;
    2. Start the Tomcat server.
    3. After the Tomcat server is fully started, open the browser and enter http: // localhost: 8080/xfire/services/helloworldservice in the address bar? WSDL.

Helloworldservcie is defined by the service \ name element in the configuration file. The "WSDL" parameter indicates viewing the Web Service's WSDL (Web Service Description Language) file.

If similar content appears in the browser, it indicates that the Web service has been successfully published. You can write a client to access the Web Service and obtain the returned string from the server, the downloaded files provided in this document include the client class Org. vivianj. xfire. pojo. client. helloworldserviceclient.

Figure: browser access Effect

If an error message appears in the browser, follow the steps and instructions above to check whether the development and configuration processes have been completed are completely correct.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.