KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > examples > HelloWorldCommand


1 package org.sapia.ubik.rmi.examples;
2
3 import org.sapia.ubik.net.Connection;
4 import org.sapia.ubik.net.TCPAddress;
5 import org.sapia.ubik.rmi.server.RMICommand;
6 import org.sapia.ubik.rmi.server.transport.TransportManager;
7
8 import java.io.IOException JavaDoc;
9
10 import java.rmi.RemoteException JavaDoc;
11
12
13 /**
14  * @author Yanick Duchesne
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class HelloWorldCommand extends RMICommand {
22   /**
23    * @see org.sapia.ubik.rmi.server.RMICommand#execute()
24    */

25   public Object JavaDoc execute() throws Throwable JavaDoc {
26     return "Hello World";
27   }
28
29   public static void main(String JavaDoc[] args) {
30     // creating address of server we wish to connect to
31
TCPAddress addr = new TCPAddress("localhost", 7070);
32
33     Connection conn = null;
34
35     try {
36       // acquiring connection
37
conn = TransportManager.getConnectionsFor(addr).acquire();
38     } catch (RemoteException JavaDoc e) {
39       e.printStackTrace();
40       System.exit(1);
41     }
42
43     try {
44       conn.send(new HelloWorldCommand());
45     } catch (IOException JavaDoc e) {
46       e.printStackTrace();
47       System.exit(1);
48     }
49
50     // always perform the receive!!!
51
try {
52       Object JavaDoc response = conn.receive();
53
54       if (response instanceof Throwable JavaDoc) {
55         Throwable JavaDoc err = (Throwable JavaDoc) response;
56         err.fillInStackTrace();
57         err.printStackTrace();
58       } else {
59         // should print 'Hello World'
60
System.out.println(response);
61       }
62
63       // Very important: allows transport
64
// providers to implement connection
65
// pooling.
66
TransportManager.getConnectionsFor(addr).release(conn);
67     } catch (RemoteException JavaDoc e) {
68       e.printStackTrace();
69       System.exit(1);
70     } catch (IOException JavaDoc e) {
71       e.printStackTrace();
72       System.exit(1);
73     } catch (ClassNotFoundException JavaDoc e) {
74       e.printStackTrace();
75       System.exit(1);
76     }
77   }
78 }
79
Popular Tags