⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT> 4:     try {</TT><TT> 5: </TT><TT> 6:         // Get the valid stock symbols from the StockServer.</TT><TT> 7:         String[] stockSymbols = myStockServer.getStockSymbols();</TT><TT> 8: </TT><TT> 9:         // Display the stock symbols and their values.</TT><TT>10:         for (int i = 0; i &lt; stockSymbols.length; i++) {</TT><TT>11:             System.out.println(stockSymbols[i] + &quot; &quot; +</TT><TT>12:                     myStockServer.getStockValue(stockSymbols[i]));</TT><TT>13:         }</TT><TT>14:     } catch (org.omg.CORBA.SystemException ex) {</TT><TT>15:         System.err.println(&quot;Fatal error: &quot; + ex);</TT><TT>16:     }17: }</TT> </FONT></PRE><P>In Listing 4.6, the <TT>StockServer</TT> is first asked, through a call to <TT>getStockSymbols()</TT>,for a list of all stock symbols recognized by the server. The client then iteratesthrough the list of stock symbols and queries the server, using <TT>getStockValue()</TT>,for the value of each stock. Each stock symbol and its respective value are printedto standard output.<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Compiling and Running the Client</FONT></H3><P>The entire listing for <TT>StockMarketClient.java</TT> appears in Listing 4.7.Note that most of the work for the client is done in the <TT>connect()</TT> and <TT>doSomething()</TT>methods, which you've already looked at.<H4><FONT COLOR="#000077">Listing 4.7. StockMarketClient.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // StockMarketClient.java</TT><TT> 2: </TT><TT> 3: package StockMarket;</TT><TT> 4: </TT><TT> 5: import org.omg.CORBA.ORB;</TT><TT> 6: import org.omg.CosNaming.NameComponent;</TT><TT> 7: import org.omg.CosNaming.NamingContext;</TT><TT> 8: import org.omg.CosNaming.NamingContextHelper;</TT><TT> 9: </TT><TT>10: // StockMarketClient is a simple client of a StockServer.</TT><TT>11: public class StockMarketClient {</TT><TT>12: </TT><TT>13:     // Create a new StockMarketClient.</TT><TT>14:     StockMarketClient() {</TT><TT>15: </TT><TT>16:     }</TT><TT>17: </TT><TT>18:     // Run the StockMarketClient.</TT><TT>19:     public void run() {</TT><TT>20: </TT><TT>21:         connect();</TT><TT>22: </TT><TT>23:         if (myStockServer != null) {</TT><TT>24:             doSomething();</TT><TT>25:         }</TT><TT>26:     }</TT><TT>27: </TT><TT>28:     // Connect to the StockServer.</TT><TT>29:     protected void connect() {</TT><TT>30: </TT><TT>31:         try {</TT><TT>32: </TT><TT>33:             // Get the root naming context.</TT><TT>34:             org.omg.CORBA.Object obj = ourORB.</TT><TT>35:                     resolve_initial_references(&quot;NameService&quot;);</TT><TT>36:             NamingContext namingContext = NamingContextHelper.narrow(obj);</TT><TT>37: </TT><TT>38:             // Attempt to locate a StockServer object in the naming</TT><TT>39:             // context.</TT><TT>40:             NameComponent nameComponent = new NameComponent(&quot;StockServer&quot;,</TT><TT>41:                     &quot;&quot;);</TT><TT>42:             NameComponent path[] = { nameComponent };</TT><TT>43:             myStockServer = StockServerHelper.narrow(namingContext.</TT><TT>44:                     resolve(path));</TT><TT>45:         } catch (Exception ex) {</TT><TT>46:             System.err.println(&quot;Couldn't resolve StockServer: &quot; + ex);</TT><TT>47:             myStockServer = null;</TT><TT>48:             return;</TT><TT>49:         }</TT><TT>50: </TT><TT>51:         System.out.println(&quot;Succesfully bound to a StockServer.&quot;);</TT><TT>52:     }</TT><TT>53: </TT><TT>54:     // Do some cool things with the StockServer.</TT><TT>55:     protected void doSomething() {</TT><TT>56: </TT><TT>57:         try {</TT><TT>58: </TT><TT>59:             // Get the valid stock symbols from the StockServer.</TT><TT>60:             String[] stockSymbols = myStockServer.getStockSymbols();</TT><TT>61: </TT><TT>62:             // Display the stock symbols and their values.</TT><TT>63:             for (int i = 0; i &lt; stockSymbols.length; i++) {</TT><TT>64:                 System.out.println(stockSymbols[i] + &quot; &quot; +</TT><TT>65:                         myStockServer.getStockValue(stockSymbols[i]));</TT><TT>66:             }</TT><TT>67:         } catch (org.omg.CORBA.SystemException ex) {</TT><TT>68:             System.err.println(&quot;Fatal error: &quot; + ex);</TT><TT>69:         }</TT><TT>70:     }</TT><TT>71: </TT><TT>72:     // Start up a StockMarketClient.</TT><TT>73:     public static void main(String args[]) {</TT><TT>74: </TT><TT>75:         // Initialize the ORB.</TT><TT>76:         ourORB = ORB.init(args, null);</TT><TT>77: </TT><TT>78:         StockMarketClient stockClient = new StockMarketClient();</TT><TT>79: </TT><TT>80:         stockClient.run();</TT><TT>81: </TT><TT>82:         // This simply waits forever so that the DOS window doesn't</TT><TT>83:         // disappear (for developers using Windows IDEs).</TT><TT>84:         while (true)</TT><TT>85:             ;</TT><TT>86:     }</TT><TT>87: </TT><TT>88:     // My ORB.</TT><TT>89:     public static ORB ourORB;</TT><TT>90: </TT><TT>91:     // My StockServer.</TT><TT>92:     private StockServer myStockServer;93: }</TT> </FONT></PRE><P>Compiling the client application is an uncomplicated process. Like the server,the client can be compiled simply with the command</P><PRE><FONT COLOR="#0066FF"><TT>javac StockMarket\StockMarketClient.java</TT></FONT></PRE><P>Again, ensure you are in the proper directory when compiling the client. You shouldbe in the same directory as the one in which you compiled the server.</P><P>Assuming the client application compiled successfully, you're just about readyto run the application. First, start the Name Service and the <TT>StockServer</TT>application (as you did previously), if they aren't running already. Then to executethe client application, type the command</P><PRE><FONT COLOR="#0066FF"><TT>java StockMarket.StockMarketClient</TT></FONT></PRE><P>If the client runs successfully, you will see output similar to Listing 4.8.<H4><FONT COLOR="#000077">Listing 4.8. StockMarketClient output.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: Succesfully bound to a StockServer.</TT><TT> 2: PTLF 72.00064</TT><TT> 3: SWPK 37.671585</TT><TT> 4: CHHL 78.37782</TT><TT> 5: JTUX 75.715645</TT><TT> 6: HUPB 41.85024</TT><TT> 7: OHQR 14.932466</TT><TT> 8: YOEX 64.3376</TT><TT> 9: UIBP 75.80115</TT><TT>10: SIPR 91.1368311: XSTD 16.010124</TT> </FONT></PRE><P>The stock symbols and their values will appear exactly as they appear in the serveroutput.<H2><A NAME="Heading10"></A><FONT COLOR="#000077">Summary</FONT></H2><P>In this chapter you started out by defining the interfaces for a CORBA server,using IDL. You then implemented the server using the inheritance approach (as opposedto the delegation approach) and learned a little bit about using the CORBA NamingService along the way. You then created a simple client application that used theservices provided by the <TT>StockServer</TT> application. In the process, you learnedhow to use the Naming Service to locate the <TT>StockServer</TT> object and how theclient stubs fit into the client application. Finally, you ran the CORBA server andclient together, creating what might be your very first distributed CORBA application.Congratulations! This is no small feat.<H4><FONT COLOR="#000077">What Comes Next?</FONT></H4><P>This chapter concludes the first section of this book, which has dealt with basicCORBA architecture and methodology. In the next section of the book, spanning Days5 through 9, you'll design and build a larger, more complex CORBA application, startingwith some basic functionality and adding more capabilities to the application insubsequent chapters. You'll apply the same techniques you've already learned; theonly difference will be that you'll deal with more complex IDL and thus more complexservers and clients. You currently have the basic knowledge required to build anentire CORBA application; the next days will give you the opportunity to practiceapplying that knowledge to a more sophisticated system.<H2><A NAME="Heading11"></A><FONT COLOR="#000077">Q&amp;A</FONT></H2><DL>	<DD><B>Q I'm a C++ programmer, and I'm not sure I understand all the Java syntax.</B><BR>	<B><BR>	A </B>For the most part, Java syntax is very similar to C++, but Java introduces	some constructs of its own. Don't be overly concerned if you don't understand certain	aspects of the language, as long as you can grasp the concepts of what the code is	doing.<BR>	<B><BR>	Q If I implement my server interfaces using the inheritance approach and later rewrite	the server to use the delegation approach, will I have to rewrite my client(s) as	well?</B><BR>	<B><BR>	A </B>Regardless of the approach used to implement the server, the client code remains	the same. Therefore, should you ever need to rewrite a server to use a different	approach, rest assured that no changes will have to be made to the clients.<BR>	<B><BR>	Q How can the classes that implement IDL interfaces have constructors when IDL doesn't	specify any?<BR>	<BR>	A </B>IDL only specifies <I>public</I> interfaces--that is, methods that can be used	by other objects anywhere else on the network. However, the class that implements	an IDL interface can also provide additional methods, although such methods won't	be visible anywhere outside the object's process space. Such methods can be useful	<I>within</I> the server application, though; constructors are an example of this.	(Server objects need to be created somehow.) So feel free to include additional methods	(public, protected, and private) in your server implementations if it makes sense	to do so.</DL><H2><A NAME="Heading12"></A><FONT COLOR="#000077">Workshop</FONT></H2><P>The following section will help you test your comprehension of the material presentedin this chapter and put what you've learned into practice. You'll find the answersto the quiz and exercises in Appendix A.<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Quiz</FONT></H3><DL>	<DD><B>1</B>. What is the purpose of server skeletons and client stubs?<BR>	<B><BR>	2</B>. Why does the server need to register the implementation object with the CORBA	Naming Service?<BR>	<B><BR>	3</B>. Why do the client and server need to catch exceptions, especially when none	are raised by the IDL operations you defined?</DL><H3><A NAME="Heading14"></A><FONT COLOR="#000077">Exercises</FONT></H3><DL>	<DD><B>1</B>. It was pointed out in the <TT>StockMarket</TT> example that it would	be a good idea to raise an exception in the <TT>getStockValue()</TT> method if an	invalid <TT>StockSymbol</TT> was passed in. Modify <TT>StockMarket.idl</TT> so that	the method can raise an <TT>InvalidStockSymbolException</TT>. (You'll also need to	add a definition for this exception.)<BR>	<BR>	<B>2</B>. In the <TT>StockMarket</TT> example, an implementation was provided that	used the delegation approach. Implement the <TT>StockServer</TT> to use the inheritance	approach. (Extra credit: Also include the exception-raising mechanism from the first	exercise.)<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../ch03/ch03.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch05/ch05.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -