Code Examplets phần 7 docx

30 155 0
Code Examplets phần 7 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

java.rmi Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Starting Up the RMI Registry Starting up the RMI registry allows you to create and export remote objects. > rmiregistry Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 24-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/277.html [8/1/2000 7:50:04 AM] java.rmi Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Defining and Exporting a Remote Object 1. Define the remote interface. import java.rmi.*; public interface RObject extends Remote { void aMethod() throws RemoteException; } 2. Define the remote object implementation. import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RObjectImpl extends UnicastRemoteObject implements RObject { public RObjectImpl() throws RemoteException { super(); } // All remote methods must //throw RemoteException public void aMethod() throws RemoteException { } } 3. Compile the remote object implementation. > javac RObject.java RObjectImpl.java 4. Generate the skeletons and stubs. > rmic RObjectImpl Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/278.html (1 of 2) [8/1/2000 7:50:05 AM] 5. Create an instance of the remote object and bind it to the RMI Registry. try { RObject robj = new RObjectImpl(); Naming.rebind( "//localhost/RObjectServer", robj); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (RemoteException e) { } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 31-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/278.html (2 of 2) [8/1/2000 7:50:05 AM] java.rmi Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Looking Up a Remote Object and Invoking a Method try { // Look up remote object RObject robj = (RObject) Naming.lookup( "//localhost/RObjectServer"); // Invoke method on remote object robj.aMethod(); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (NotBoundException e) { } catch (RemoteException e) { } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 19-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/279.html [8/1/2000 7:50:05 AM] java.rmi Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Passing Parameters to a Remote Method Arguments to remote methods must be primitive, serializable, or Remote. This example demonstrates the declaration and use of all three parameter types. 1. Define the remote interface. import java.rmi.*; public interface RObject extends Remote { // This parameter is primitive. void primitiveArg(int num) throws RemoteException; // This parameter implements Serializable. void byValueArg(Integer num) throws RemoteException; // This parameter implements Remote. void byRefArg(ArgObject arg) throws RemoteException; } public interface ArgObject extends Remote { int aMethod() throws RemoteException; } 2. Define the remote object implementation. import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RObjectImpl extends UnicastRemoteObject implements RObject { public RObjectImpl() Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/280.html (1 of 3) [8/1/2000 7:50:06 AM] throws RemoteException { super(); } public void primitiveArg(int num) throws RemoteException { } public void byValueArg(Integer num) throws RemoteException { } public void byRefArg(ArgObject arg) throws RemoteException { } } 3. Compile the remote object implementation. > javac RObject.java RObjectImpl.java 4. Generate the skeletons and stubs. > rmic RObjectImpl 5. Create an instance of the remote object and bind it to the RMI Registry. try { RObject robj = new RObjectImpl(); Naming.rebind("//localhost/RObjectServer", robj); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (RemoteException e) { } 6. Look up the remote object and pass the parameters. try { // Look up the remote object RObject robj = (RObject) Naming.lookup( "//localhost/RObjectServer"); // Pass a primitive value as argument robj.primitiveArg(1998); // Pass a serializable object as argument robj.byValueArg( new Integer(9)); // Pass a Remote object as argument robj.byRefArg( new ArgObjectImpl()); Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/280.html (2 of 3) [8/1/2000 7:50:06 AM] } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (NotBoundException e) { } catch (RemoteException e) { } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 31-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/280.html (3 of 3) [8/1/2000 7:50:06 AM] java.rmi Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Returning Values from a Remote Method Return values from remote methods must be primitive, serializable, or Remote. This example demonstrates the declaration and use of all three return types. 1. Define the remote interface. import java.rmi.*; public interface RObject extends Remote { // This return value is primitive. int primitiveRet() throws RemoteException; // This return value implements Serializable. Integer byValueRet() throws RemoteException; // This return value implements Remote. ArgObject byRefRet() throws RemoteException; } public interface ArgObject extends Remote { int aMethod() throws RemoteException; } 2. Define the remote object implementation. import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RObjectImpl extends UnicastRemoteObject implements RObject { public RObjectImpl() throws RemoteException { super(); } public int primitiveRet() throws RemoteException { return 3000; } Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/281.html (1 of 3) [8/1/2000 7:50:07 AM] public Integer byValueRet() throws RemoteException { return new Integer(2000); } public ArgObject byRefRet() throws RemoteException { return new ArgObjectImpl(); } } 3. Compile the remote object implementation. > javac RObject.java RObjectImpl.java 4. Generate the skeletons and stubs. > rmic RObjectImpl 5. Create an instance of the remote object and bind it to the RMI Registry. try { RObject robj = new RObjectImpl(); Naming.rebind("//localhost/RObjectServer", robj); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (RemoteException e) { } 6. Look up the remote object, invoke the methods, and receive the return values. try { // Look up the remote object RObject robj = (RObject) Naming.lookup( "//localhost/RObjectServer"); // Receive the primitive value as return value int r1 = robj.primitiveRet(); // Receive the serializable object as return value Integer r2 = robj.byValueRet(); // Receive the Remote Object as return value ArgObject aobj = robj.byRefRet(); } catch (MalformedURLException e) { } catch (UnknownHostException e) { } catch (NotBoundException e) { } catch (RemoteException e) { } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/281.html (2 of 3) [8/1/2000 7:50:07 AM] [ This page was updated: 19-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/281.html (3 of 3) [8/1/2000 7:50:07 AM] [...]... call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/java.rmi/282.html (2 of 2) [8/1/2000 7: 50: 07 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples... (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.sampled/3 17. html (2 of 2) [8/1/2000 7: 50:10 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples... Microsystems, call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.midi/3 27. html [8/1/2000 7: 50:15 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.midi Code Samples Index These code examples... (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.sampled/313.html (2 of 2) [8/1/2000 7: 50:08 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples... call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.sampled/318.html [8/1/2000 7: 50:10 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples... call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.sampled/319.html [8/1/2000 7: 50:11 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples... call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.sampled/320.html [8/1/2000 7: 50:11 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples... Microsystems, call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.midi/324.html [8/1/2000 7: 50:13 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.midi Code Samples Index These code examples... Microsystems, call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.midi/326.html [8/1/2000 7: 50:14 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.midi Code Samples Index These code examples... call: (800) 78 6 -76 38 Outside the U.S and Canada, dial your country's AT&T Direct Access Number first Copyright © 1995-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.sound.sampled/316.html [8/1/2000 7: 50:09 AM] Code Samples from the Java Developers Almanac 2000 javax.sound.sampled Code Samples Index These code examples . Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/ 277 .html [8/1/2000 7: 50:04 AM] java.rmi Code Samples Index These code. Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/ 279 .html [8/1/2000 7: 50:05 AM] java.rmi Code Samples Index These code. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/java.rmi/ 278 .html (2 of 2) [8/1/2000 7: 50:05 AM] java.rmi Code Samples

Ngày đăng: 06/08/2014, 02:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan