Writing a Basic Service


Writing a Basic Client

Your first goal in this chapter is to write a client application in Java that will let you use the BasicAPI service hosted on the caucho.com Web site. The code for the client is shown in Listing 9.1.

Listing 9.1: Basic Hessian client for the BasicAPI Web service.

start example
 Line 1: import com.caucho.hessian.client.HessianProxyFactory; import com.caucho.hessian.test.BasicAPI; import java.net.MalformedURLException; public class BasicClient {   static String url =     "http://www.caucho.com/hessian/test/basic"; Line 10:  public static void main(String[] args) {     BasicAPI basic = null;     HessianProxyFactory factory = new HessianProxyFactory();     try {       basic = (BasicAPI) factory.create(BasicAPI.class, url);     } catch (MalformedURLException e) {         System.out.println("Bad URL");     } Line 20:    if (basic != null)       System.out.println("hello(): " + basic.hello());   } } 
end example

Downloading the Interface Class

To write a client using the Hessian protocol and the BasicAPI service, you must download the interface class for BasicAPI from the Caucho Web site. The URL for the download is www.caucho.com/hessian/download/hessian-test.jar. Place the file hessian-testjar in a directory where the client can be built.

Next, put the code from Listing 9.1 in the same directory and compile the code with this command:

 javac -classpath ".\hessian-3.0.2.jar;.\hessian-test.jar" Basic Client.java 

Execute the code with the following command:

 Java -classpath ".\hessian-3.0.2.jar;.\hessian-test.jar;" BasicClient 

You should see the text hello(): hello on the console window.

Let's walk through the code:

  • Line 1 imports the class used for building a proxy to the Web service object you are using. Line 2 imports the interface built for the Web service with which you are connecting. This is a very important step, because your application needs to know the methods made available by the remote object.

  • Lines 7 and 8 represent the URL where the Web service has been made available. The URL follows the naming guidelines presented earlier, so you know the object you're accessing is called basic.

  • Line 11 creates an object of type BasicAPI, which is the name of the class made available at the Caucho Web site. The object is set to NULL for the time being.

  • Line 13 instantiates an object factory provided by the Hessian protocol. This factory is used to build a proxy object in the local machine to act as a representation of the remote object.

  • Line 15 does the actual work of attempting to create a new local object based on the remote object.

  • Lines 20 and 21 check to make sure the object creation was successful and make a call to the hello() method of the remote object. Notice that the call is no different for the remote object than for a local object.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net