📄 311.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><!-- This file edited for Java Jumpstart CD --> <HEAD><META VALUE="keyword" CONTENTS="codesamples"> <TITLE> Code Samples from the Java Developers Almanac 2000 </TITLE> <META NAME="AUTHOR" CONTENT="Author's Name Goes Here"> <META NAME="DESCRIPTION" CONTENT="Description Goes Here"> <META NAME="KEYWORDS" CONTENT="Keyword Listing Goes Here"> <META NAME="OWNER" CONTENT="Editorial/JDC"> <META NAME="revision" CONTENT="@(#)index2.src 1.2 06/09/99 JDC"> <STYLE TYPE="text/css"> <!-- CODE {font-family: Courier, Monospace; font-size: 12pt} PRE {font-family: Courier, Monospace; font-size: 11pt} --> </STYLE> </HEAD> <!-- Start Body Insert--><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066"><!-- End Body Insert--> <!-- Start PageTop Insert --><TD> <P> <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0"> <TR VALIGN="TOP"> <TD WIDTH="100%"><IMG SRC="../../../../images/chiclet.row.gif" WIDTH="55" HEIGHT="18" ALT=""></TD> <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152"><IMG SRC="../../../../images/developer.connection.header.gif"BORDER="0" HEIGHT="42" WIDTH="319"></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%"> <IMG SRC="../../../../images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%"> <IMG SRC="../../../../images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"><TD><IMG SRC="../../../../images/codesamples.gif" WIDTH="165" HEIGHT="22" ALT="Code Samples" BORDER=0></TD> </TR> </TABLE> </TD> </TR></TABLE><!-- End PageTop Insert --> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"> <TR> <TD> <FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <!-- Template Version 2.0 --> <!-- ================== --> <!-- Start Main Content --> <!-- ================== --> <DIV ALIGN="RIGHT"> <FONT SIZE="3"><B>javax.rmi</B></FONT><BR> <FONT SIZE="-1"><A HREF="../index.html">Code Samples Index</A></FONT> </DIV> <P> <FONT SIZE="-1"> These code examples and other materials are subject to Sun Microsystems, Inc. <a href="../../../../share/text/SMICopyright.html">Legal Terms</a> </FONT> <P> <H3>Defining and Exporting a Portable Remote Object</H3> <P>1. Define the remote interface. <PRE> import java.rmi.*; public interface <font color="#0066ff"><I>RObject</I></font> extends Remote { void <font color="#0066ff"><I>aMethod</I></font>() throws RemoteException; } </PRE> 2. Define the portable remote object implementation. <PRE> import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class <font color="#0066ff"><I>RObjectImpl</I></font> extends UnicastRemoteObject implements <font color="#0066ff"><I>RObject</I></font> { public <font color="#0066ff"><I>RObjectImpl</I></font>() throws RemoteException { super(); } // All remote methods must throw RemoteException public void <font color="#0066ff"><I>aMethod</I></font>() throws RemoteException { } } </PRE> 3. Compile the remote object implementation. <PRE> > javac <font color="#0066ff"><I>RObject</I></font>.java <font color="#0066ff"><I>RObjectImpl</I></font>.java </PRE> 4. Generate the skeletons and stubs. <PRE> <font color="#0066ff"><I>Using RMI over IIOP</I></font> > rmic -iiop <font color="#0066ff"><I>RObjectImpl</I></font> <font color=#0066ff><I>Using RMI over JRMP</I></font> > rmic <font color=#0066ff><I>RObjectImpl</I></font> </PRE> 5. Create an instance of the remote object and bind it to a name service. <PRE> import javax.naming.*; import java.rmi.RemoteException; // JRMP: name = <font color=#0066ff><I>"rmi://localhost/RObjectServer"</I></font>; // IIOP: name = <font color=#0066ff><I>"iiop://localhost/RObjectServer"</I></font>; try { <font color=#0066ff><I>RObject</I></font> robj = new <font color=#0066ff><I>RObjectImpl()</I></font>; new InitialContext().bind(name, robj); } catch (NamingException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } </PRE> <P> <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0> <TR><TD VALIGN=MIDDLE> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> Examplets<SUP><FONT SIZE="-2">TM</FONT></SUP> are Copyrighted 2000 by Addison-Wesley and Patrick Chan <BR>and are provided as a courtesy for your educational, non-commercial use.<BR><BR> Order this book from <a href="http://www.amazon.com/exec/obidos/ASIN/0201432994/xeo">Amazon</a> </FONT> </TD></TR></TABLE> <!-- ================ --> <!-- End Main Content --> <!-- ================ --> </FONT> </TD> </TR> </TABLE> <!-- Copyright Insert --><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"><TR> <TD VALIGN="TOP"> <P ALIGN=CENTER> [ This page was updated: <!-- new date --> 11-Sep-2000 ]</TD></TR><TR><TD BGCOLOR="#CCCCCC"><SPACER TYPE="BLOCK" HEIGHT="1" WIDTH="1"></TD></TR></TD></TR></TABLE><TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/tollfree/international/dialguide/index.html">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="../../../../images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-2000 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <A HREF="../../../../share/text/termsofuse.html">Terms of Use</A>. <A HREF="http://www.sun.com/privacy/index.html">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --> </BODY> </HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -