Accelerate Your Java Web Service Development with XFire, Page 3
Coding the StockQuote Web Service Client
You create a Java class file called XFireClientTest in the 'src' directory. The 'src' directory should now look like this:
Fill in the XFireClientTest.java file with the following Java code:
import net.xmethods.services.stockquote.StockQuoteClient;
import net.xmethods.services.stockquote.StockQuoteSoap;
public class XFireClientTest
{
public static void main(String[] args) {
StockQuoteClient client = new StockQuoteClient();
StockQuoteSoap service = client.getStockQuoteSoap();
String quote = service.getQuote("HOT");
System.out.println(quote);
}
}
Your client gets the client proxy for the StockQuote service. It then uses it to get the StockQuote service and call the 'getQuote()' service method, passing in the company symbol of 'HOT'. It then prints out the result to the console. That is all there is for you to code; XFire has generated all the other marshalling code that will convert your input parameter into a SOAP request.
Compiling the XFireDemo Project
Now that you have your Stock Quote web service client, you can go ahead and compile your project using Ant. Running 'ant build.xml' produces the following output, indicating that it compiled your source code and created your XFireClientTest and supporting class files in the 'bin' directory:
Buildfile: C:Documents and SettingsdominicworkspaceXFireDemo
build.xml
clean:
[delete] Deleting 8 files from C:Documents and Settingsdominic
workspaceXFireDemobin
wsgen:
[wsgen] Jun 30, 2007 5:02:46 PM
org.codehaus.xfire.gen.Wsdl11Generator generate
[wsgen] INFO: Generating code for WSDL at
http://www.webservicex.net/stockquote.asmx?WSDL with a
base URI of http://www.webservicex.net/stockquote.asmx?WSDL
[wsgen] Jun 30, 2007 5:02:51 PM
org.codehaus.XFire.gen.jsr181.AbstractServiceGenerator
generate
[wsgen] INFO: Creating class
net.xmethods.services.stockquote.StockQuoteSoap
[wsgen] Jun 30, 2007 5:02:51 PM
org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator
generate
[wsgen] INFO: Creating class
net.xmethods.services.stockquote.StockQuoteImpl
[wsgen] netwebservicexGetQuote.java
[wsgen] netwebservicexGetQuoteResponse.java
[wsgen] netwebservicexObjectFactory.java
[wsgen] netwebservicexpackage-info.java
[wsgen] netxmethodsservicesstockquoteStockQuoteClient.java
[wsgen] netxmethodsservicesstockquoteStockQuoteImpl.java
[wsgen] netxmethodsservicesstockquoteStockQuoteSoap.java
compile:
[javac] Compiling 8 source files to C:Documents and Settings
dominicworkspaceXFireDemobin
[javac] Note: C:Documents and Settingsdominicworkspace
XFireDemosrcnetxmethodsservicesstockquote
StockQuoteClient.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
BUILD SUCCESSFUL
Total time: 14 seconds
Running the StockQuote Service SOAP Client Test
You can run your client directly from Eclipse by right-clicking the XFireClientTest class and selecting 'Run As->Java Application':
Click here for a larger image.
Figure 4:Running XFireClientTest in Eclipse
When you run the SOAP client test class, XFireClientTest, using a company symbol of 'HOT' (Starwood Hotels and Resorts), you will get the following response from the Stock Quote web service, indicating a successful test:
<StockQuotes>
<Stock>
<Symbol>HOT</Symbol>
<Last>67.07</Last>
<Date>6/29/2007</Date>
<Time>4:00pm</Time>
<Change>-1.13</Change>
<Open>68.04</Open>
<High>68.77</High>
<Low>66.035</Low>
<Volume>3752226</Volume>
<MktCap>14.427B</MktCap>
<PreviousClose>68.20</PreviousClose>
<PercentageChange>-1.66%</PercentageChange>
<AnnRange>49.68 - 74.35</AnnRange>
<Earns>5.237</Earns>
<P-E>13.02</P-E>
<Name>STARWOOD HOTELS&R</Name>
</Stock>
</StockQuotes>
Conclusion
This article took a look at XFire, a Java SOAP framework that makes working with web services fast and simple. You created a SOAP client and saw how easy it is to use XFire. In the next article in this XFire series, you will create your own SOAP web service using XFire.
References
- Codehaus XFire: http://xfire.codehaus.org/
- Sun Java: http://java.sun.com/
- Apache Ant: http://ant.apache.org/
- Eclipse: http://www.eclipse.org/
- XMethods: http://www.xmethods.net/
- SilvaSoft, Inc. weblog: http://jroller.com/page/silvasoftinc
About the Author
Dominic Da Silva (http://www.dominicdasilva.com/) is the President of SilvaSoft, Inc., a software consulting company specializing in Java- and Ruby-based web and web services development. He has worked with Java since the year 2000 and is a Linux user from the 1.0 days. He is also Sun Certified for the Java 2 platform. Born on the beautiful Caribbean island of Trinidad and Tobago, he now makes his home in sunny Orlando, Florida.
Page 3 of 3
