12 12Java RMI
121 RMI 2
121 RMI 3 - RMI, CORBA
121 RMI RMI RMI (remote object) 4 - ( ) UnicastRemoteObject,
121 RMI 5 class A - class B - ( ) class A a() class Bb()
121 RMI 6 RMI /
121 RMI RMI 1 2 ( 7) 3 ( ) 4 stubskeleton 5 registry 6 - javarmiremote, javarmiserver Remote Remote public interface Remote { } RemoteObject - RMI
122 RMI 1 sayhello() Hello : Hellojava 1 package hello; 82 3 import javarmi*; 4 5 public interface Hello extends Remote { 6 7 public String sayhello() throws javarmiremoteexception; 8 9 } 1) javarmiremote 2) public
122 RMI 9 2 Hello HelloImpl : HelloImpljava 1 package hello; 2 3 import javarmi*; 4 import javarmiserver*; 5 import javanet*; 6 7 public class HelloImpl extends UnicastRemoteObject implements Hello { 8 9 public HelloImpl() throws RemoteException { 10 super(); 11 } 12 13 public String sayhello() throws RemoteException { 14
122 RMI 15 return "Hello World"; 16 } 17 18 public static void main(string[] args) { 19 SystemsetSecurityManager(new RMISecurityManager()); 20 21 try { 22 HelloImpl h = new HelloImpl(); 23 Namingrebind("//localhost/hello", h); 24 Systemoutprintln("Hello Server ready"); 25 } catch (RemoteException re) { 26 Systemoutprintln("Exception in HelloImplmain: " + re); 27 } catch (MalformedURLException mfe) { 28 Systemoutprintln("MalformedURLException in HelloImplmain" + mfe); 29 } 30 } 31 } 10
122 RMI bindrebind 11 rebind() bind(), bind() javarmialreadyboundexception, rebind() RMI URL RMI URL rmi://host:port/object_name hello : Namingrebind("rmi://localhost:1099/hello", h); RMI 1099 1 2 RemoteExceptionthrows
122 RMI % javac -d Hellojava HelloImpljava 12 3 HelloImpl ( ) : HelloClientjava 1 package hello; 2 3 import javarmi*; 4 5 public class HelloClient { 6 7 public static void main(string[] args) { 8 9 SystemsetSecurityManager(new RMISecurityManager()); 10 11 try {
122 RMI 1312 Hello h = (Hello) Naminglookup("rmi://mycomsoongsilackr/hello"); 13 14 String message = hsayhello(); 15 Systemoutprintln("HelloClient: " +message); 16 } catch(exception e) { 17 Systemoutprintln("Exception in main: "+ e); 18 } 19 } 20 } 4 rmic stubskeleton, jar % rmic -d hellohelloimpl % jar cvf hellojar hello/*class 1) rmicstubskel
122 RMI 14 5 rmiregistry % unsetenv CLASSPATH % rmiregistry & [1] 7382 6 : javapolicy 1 grant{ 2 permission javanetsocketpermission "*:1024-65535", 3 "connect,accept"; 4 permission javanetsocketpermission "*:80", "connect"; 5 }; 7 rmi % java -Djavasecuritypolicy=javapolicy hellohelloimpl Hello Server ready
122 RMI 15 7 % java -Djavasecuritypolicy=javapolicy hellohelloclient HelloClient: Hello World RMI : HelloAppletjava 1 package hello; 2 3 import javaawt*; 4 import javaapplet*; 5 import javarmi*; 6 7 public class HelloApplet extends Applet { 8 String message = ""; 9 10 public void init() {
122 RMI 1611 try { 12 Hello obj = (Hello)Naminglookup("//"+getCodeBase() gethost() + "/hello"); 13 message = objsayhello(); 14 } catch(exception e) { 15 Systemoutprintln("HelloApplet exception: " + egetmessage()); 16 } 17 } 18 19 public void paint(graphics g) { 20 gdrawstring(message, 25, 50); 21 } 22 }
122 RMI 17 : Hellohtml 1 <html><title>hello World</title> 2 <applet code=hellohelloapplet archive=hellojar width=400 height=400> 3 </applet> 4 </html> % javac -d HelloAppletjava % jar cvf hellojar hello/*class % appletviewer Hellohtml
123 RMI RMI TCP/IP 3 Stub/Skeleton - Remote Reference - Transport - 18 RMI, (marshal stream) (marshaling) (unmarshaling)
123 RMI 19 : SetMessagejava 1 package message; 2 3 import javarmi*; 4 5 public interface SetMessage extends Remote { 6 public void setmessage(string msg) throws RemoteException ; 7 public String getmessage() throws RemoteException ; 8 } : SetMessageImpljava 1 package message; 2 3 import javarmi*; 4 import javarmiserver*; 5 import javanet*;
123 RMI 20 6 7 public class SetMessageImpl extends UnicastRemoteObject implements SetMessage { 8 private String msg; 9 10 public SetMessageImpl() throws RemoteException { 11 super(); 12 msg = ""; 13 } 14 15 public String getmessage() throws RemoteException { 16 return msg; 17 } 18 19 public void setmessage(string msg) throws RemoteException { 20 thismsg = msg; 21 Systemoutprintln("message: "+msg); 22 }
123 RMI 21 23 } 24 25 class SetMessageServer { 26 public static void main(string args[]) { 27 SystemsetSecurityManager(new RMISecurityManager()); 28 try { 29 SetMessageImpl s = new SetMessageImpl(); 30 Namingrebind("setmessage", s); 31 Systemoutprintln("SetMessage Server is ready"); 32 } catch (RemoteException e) { 33 Systemoutprintln(e); 34 } catch (Exception ex) { 35 Systemoutprintln(ex); 36 } 37 } 38 }
123 RMI 22 : SetMessageClientjava 1 package message; 2 3 import javaawt*; 4 import javaawtevent*; 5 import javaapplet*; 6 import javarmi*; 7 8 public class SetMessageClient extends Applet implements Runnable, ActionListener { 9 Thread runner; 10 SetMessage obj; 11 String message = ""; 12 TextField input; 13 14 public void init() { 15 setlayout(new BorderLayout()); 16 input = new TextField(10); 17 inputaddactionlistener(this); 18 add("south", input);
123 RMI 23 19 runner = new Thread(this); 20 runnerstart(); 21 22 try { 23 obj = (SetMessage) Naminglookup("//"+getCodeBase()getHost() + "/setmessage"); 24 } catch (Exception e) { 25 Systemoutprintln(e); 26 } 27 } 28 29 public void run() { 30 while(true) { 31 try { 32 Threadsleep(500); 33 message = objgetmessage(); 34 } catch(exception ex) { 35 message = extostring(); 36 } 37 repaint();
123 RMI 24 38 } 39 } 40 41 public void paint(graphics g) { 42 gdrawstring(message, 25, 25); 43 } 44 45 public void actionperformed(actionevent e) { 46 Component c = (Component) egetsource(); 47 48 if(c == input) { 49 try { 50 objsetmessage(inputgettext()); 51 } catch (Exception ex) {} 52 inputsettext(""); 53 } 54 } 55 }
123 RMI 25 : SetMessageClienthtml 1 <table border=1><tr> 2 <td><applet code=messagesetmessageclientclass archive=messagejar 3 width=300 height=100> 4 </applet> 5 <td><applet code=messagesetmessageclientclass archive=messagejar 6 width=300 height=100> 7 </applet> 8 </tr> 9 </table>
124 RMI (callback) 26 register() update()
124 RMI (callback) : RMIChatServerjava 1 package rmichat; 2 3 import javarmi*; 4 5 public interface RMIChatServer extends Remote { 6 public void register(rmichatclient client) throws RemoteException; 7 public void broadcast(string msg) throws RemoteException; 8 public void unregister(rmichatclient client) throws RemoteException; 9 } 27 register() unregister(), broadcast()
124 RMI (callback) 28 : RMIChatServerImpljava 1 package rmichat; 2 3 import javarmiserver*; 4 import javarmi*; 5 import javautil*; 6 7 public class RMIChatServerImpl extends UnicastRemoteObject implements RMIChatServer { 8 Vector clientlist; 9 public RMIChatServerImpl() throws RemoteException { 10 super(); 11 clientlist = new Vector(5, 5); 12 } 13 14 public synchronized void register(rmichatclient client) throws RemoteException { 15 clientlistaddelement(client); 16 } 17
124 RMI (callback) 29 18 public void broadcast(string msg) throws RemoteException { 19 synchronized(clientlist) { 20 Enumeration e = clientlistelements(); 21 while(ehasmoreelements()) { 22 RMIChatClient c = (RMIChatClient) enextelement(); 23 csetmessage(msg); 24 } 25 } 26 } 27 28 public synchronized void unregister(rmichatclient client) throws RemoteException { 29 clientlistremoveelement(client); 30 } 31 32 public static void main(string args[]) { 33 SystemsetSecurityManager(new RMISecurityManager()); 34 try { 35 RMIChatServerImpl c = new RMIChatServerImpl(); 36 Namingrebind("rmi://mycomsoongsilackr/chat", c);
124 RMI (callback) 37 Systemoutprintln("RMI Chat Server is ready"); 38 } catch(exception ex) { 39 Systemoutprintln(ex); 40 } 41 } 42 } : RMIChatClientjava 1 package rmichat; 2 3 import javarmi*; 4 5 public interface RMIChatClient extends Remote { 6 public void setmessage(string msg) throws RemoteException; 7 } 30 RMIChatClient setmessage()
124 RMI (callback) 31 : RMIChatClientAppjava 1 package rmichat; 2 3 import javaawt*; 4 import javaawtevent*; 5 import javaapplet*; 6 import javarmi*; 7 import javarmiserver*; 8 9 public class RMIChatClientApp extends Frame implements RMIChatClient, ActionListener { 10 TextArea display; 11 TextField input; 12 RMIChatServer server; 13 14 public RMIChatClientApp() { 15 super("chat Client"); 16 setlayout(new BorderLayout()); 17 add("center", display = new TextArea()); 18 displayseteditable(false);
124 RMI (callback) 32 19 addwindowlistener(new WindowAdapter() { 20 public void windowclosing(windowevent e) { 21 setvisible(false); 22 try { 23 serverunregister(rmichatclientappthis); 24 } catch(exception ex) {} 25 dispose(); 26 Systemexit(0); 27 } 28 }); 29 30 add("south", input = new TextField()); 31 inputaddactionlistener(this); 32 33 try { 34 UnicastRemoteObjectexportObject(this); 35 server = (RMIChatServer) Naminglookup("rmi://mycomsoongsilackr/ chat"); 36 serverregister(this); 37 } catch (Exception ex) {
124 RMI (callback) 33 38 displayappend(extostring()); 39 } 40 setsize(400, 300); 41 setvisible(true); 42 } 43 44 public synchronized void setmessage(string msg) { 45 try { 46 displayappend(msg + ""); 47 } catch (Exception e) { 48 displayappend(etostring()); 49 } 50 } 51 52 public void actionperformed(actionevent e) { 53 Component c = (Component) egetsource(); 54 55 if(c == input) { 56 try { 57 serverbroadcast(inputgettext());
124 RMI (callback) 34 58 inputsettext(""); 59 } catch (Exception ex) { 60 displayappend(extostring()); 61 } 62 } 63 } 64 65 public static void main(string args[]) { 66 new RMIChatClientApp(); 67 } 68 }
124 RMI (callback) RMI : RMIChatClientAppletjava 351 package rmichat; 2 3 import javaawt*; 4 import javaawtevent*; 5 import javaapplet*; 6 import javarmi*; 7 import javarmiserver*; 8 9 public class RMIChatClientApplet extends Applet implements RMIChatClient, ActionListener { 10 TextArea display; 11 TextField input; 12 RMIChatServer server; 13 14 public void init() {
124 RMI (callback) 36 14 public void init() { 15 setlayout(new BorderLayout()); 16 add("center", display = new TextArea()); 17 displayseteditable(false); 18 add("south", input = new TextField()); 19 inputaddactionlistener(this); 20 21 try { 22 UnicastRemoteObjectexportObject(this); 23 Systemoutprintln(getCodeBase()getHost()); 24 server = (RMIChatServer) Naminglookup("// + getcodebase()gethost() + "/chat"); 25 serverregister(this); 26 } catch (Exception ex) { 27 displayappend(extostring()); 28 } 29 } 30
124 RMI (callback) 37 31 public synchronized void setmessage(string msg) { 32 try { 33 displayappend(msg + ""); 34 } catch (Exception e) { 35 displayappend(etostring()); 36 } 37 } 38 39 public void actionperformed(actionevent e) { 40 Component c = (Component) egetsource(); 41 42 if(c == input) { 43 try { 44 serverbroadcast(inputgettext()); 45 inputsettext(""); 46 } catch (Exception ex) { 47 displayappend(extostring());
124 RMI (callback) 38 RMI HTMLapplet HTMLConverter
125 JDBC + RMI = 3 tier? 39 2-tier /, 2-tier 3-tiermulti-tier / JDBC 2-tier Alumnijava RMI (middle tier) 3-tier / AlumniRecord AlumniRecord (serializable)
125 JDBC + RMI = 3 tier? 40 % psql alumni alumni=> Database = alumni +------------------+----------------------------------+----------+ Owner Relation Type +------------------+----------------------------------+----------+ jmchoi cs table +------------------+----------------------------------+----------+ alumni=> cs Table = cs +----------------------------------+----------------------------------+-------+ Field Type Length +----------------------------------+----------------------------------+-------+ num varchar 10 name varchar 10 phone varchar 25 company varchar 40 email varchar 40 +----------------------------------+----------------------------------+-------+
125 JDBC + RMI = 3 tier? 41