Writing a Basic Service


Writing a Basic Client

Our first goal in this chapter is to write a client application in Java that allows you to use the BasicAPI service hosted on the Caucho.com Web site. Listing 8.1 contains the code for the client.

Listing 8.1: A basic Burlap client for the BasicAPI Web service.

start example
 Line 1: import com.caucho.hessian.client.BurlapProxyFactory; import com.caucho.burlap.test.BasicAPI; import java.net.MalformedURLException; public class BasicClient {   static String url =     "http://www.caucho.com/burlap/test/basic"; Line 10:  public static void main(String[] args) {     BasicAPI basic = null;     BurlapProxyFactory factory = new BurlapProxyFactory();     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

Executing the Code

To test the code in Listing 8.1, you need to download the burlap-test.jar JAR file found on the Caucho Web site at www.caucho.com/burlap/index.xtp. This JAR contains the BasicAPI interface needed to build a local object reference to the Web service on the Caucho site. Once you have downloaded this JAR, place it in your classpath. (If you haven't already downloaded the burlap JAR, you can obtain it at the same URL.)

In the directory where you placed the code from Listing 8.1, compile the test code using the following command:

 javac BasicClient.Java 

Now execute the code:

 java BasicClient 

You should see output shown in Figure 8.1.

click to expand
Figure 8.1: The Burlap BasicClient output.

So what did this small snippet of code do? In Listing 8.1, the code started by instantiating an object factory that allows you to create local references to remote services. At this point, you have only created a factory supplied by the Burlap implementation from Caucho.

Next, the code enters a try/catch block where an attempt is made to connect with the remote service and create an object. The object you are wanting to instantiate is called basic, defined as type BasicAPI. Web services provide the BasicAPI interface, defined in the burlap-test.jar file, to developers who want to use the service. All of the work for the remote instantiation takes place in this code:

 basic = (BasicAPI) factory.create(BasicAPI.class, url); 

Here you are asking the Burlap factory to connect to a service URL and attempt to instantiate an object of the BasicAPI.class type. If the protocol is successful, a reference to the remote object is returned and assigned to the basic variable. Finally, you use the newly instantiated object by calling the hello() method and displaying the output.




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