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

📄 ch09.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT>186:                     account, pin + 1, 250.00) &lt;&lt; endl;</TT><TT>187:         } catch (AuthorizationException&amp;) {</TT><TT>188:             cout &lt;&lt; &quot;ATMClient: Exception caught: Invalid PIN or &quot;</TT><TT>189:                     &lt;&lt; &quot;No authorization (as expected)&quot; &lt;&lt; endl;</TT><TT>190:         } catch (InvalidAmountException&amp;) {</TT><TT>191:             cout &lt;&lt; &quot;ATMClient: Exception caught: Invalid amount&quot;</TT><TT>192:                     &lt;&lt; endl;</TT><TT>193:         } catch (InsufficientFundsException&amp;) {</TT><TT>194:             cout &lt;&lt; &quot;ATMClient: Exception caught: Insufficient &quot; &lt;&lt;</TT><TT>195:                     &quot;funds&quot; &lt;&lt; endl;</TT><TT>196:         }</TT><TT>197: </TT><TT>198:         // Get rid of the Account.</TT><TT>199:         try {</TT><TT>200:             cout &lt;&lt; &quot;  Deleting Account.&quot; &lt;&lt; endl;</TT><TT>201:             bank-&gt;deleteAccount(account);</TT><TT>202: </TT><TT>203:             // Attempt to delete the Account again, just for kicks.</TT><TT>204:             // This should result in an exception being thrown.</TT><TT>205:             cout &lt;&lt; &quot;  Attempting to cause an exception by &quot; &lt;&lt;</TT><TT>206:                     &quot;deleting Account again.&quot; &lt;&lt; endl;</TT><TT>207:             bank-&gt;deleteAccount(account);</TT><TT>208:         } catch (const InvalidAccountException&amp;) {</TT><TT>209: </TT><TT>210:             // Sure enough, the exception was thrown.</TT><TT>211:             cout &lt;&lt; &quot;ATMClient: Exception caught: Invalid &quot; &lt;&lt;</TT><TT>212:                     &quot;Account (as expected)&quot; &lt;&lt; endl;</TT><TT>213:         }</TT><TT>214:     } catch (const CORBA::Exception&amp; ex) {</TT><TT>215: </TT><TT>216:         // Some operation on the Account failed...</TT><TT>217:         cout &lt;&lt; &quot;ATMClient: Error accessing Account:&quot; &lt;&lt; endl;</TT><TT>218:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT>219:         return 1;</TT><TT>220:     }</TT><TT>221: </TT><B><TT>222:     // Sleep for long enough to catch an Account update message or</TT></B><B><TT>223:     // two.</TT></B><B><TT>224:     Sleep(120000);</TT></B><TT>225: </TT><TT>226:     // When this point is reached, the application is finished.</TT><TT>227:     return 0;228: }</TT></FONT></PRE><H2><A NAME="Heading9"></A><FONT COLOR="#000077">Running the Application</FONT></H2><P>Once again, you're ready to run the modified application. The process is exactlythe same as in the previous chapter, but the output from the various applicationswill be slightly different, as you would expect. Again, start by running the <TT>BankServer</TT>application:</P><PRE><FONT COLOR="#0066FF"><TT>BankServer</TT></FONT></PRE><P>Again, the output of the <TT>BankServer</TT> will be this:</P><PRE><FONT COLOR="#0066FF"><TT>BankServer ready.</TT></FONT></PRE><P>You're now ready to start the <TT>Bank</TT> application</P><PRE><FONT COLOR="#0066FF"><TT>Bank &quot;First Bank&quot;</TT></FONT></PRE><P>which, again, will output this:</P><PRE><FONT COLOR="#0066FF"><TT>Bank &quot;First Bank&quot; ready.</TT></FONT></PRE><P>Meanwhile, the <TT>BankServer</TT> will output this:</P><PRE><FONT COLOR="#0066FF"><TT>BankServerImpl: Registering Bank &quot;First Bank&quot;.</TT></FONT></PRE><P>Now you'll start the <TT>ATM</TT> application:</P><PRE><FONT COLOR="#0066FF"><TT>ATM &quot;First Bank ATM&quot;</TT></FONT></PRE><P>The <TT>ATM</TT> application will display the following:</P><PRE><FONT COLOR="#0066FF"><TT>ATM &quot;First Bank ATM&quot; ready.</TT></FONT></PRE><P>The <TT>BankServer</TT>, again, will output the message:</P><PRE><FONT COLOR="#0066FF"><TT>BankServerImpl: Registering ATM &quot;First Bank ATM&quot;.</TT></FONT></PRE><P>Finally, you're ready to run the <TT>ATMClient</TT> application. You can do soby typing the following:</P><PRE><FONT COLOR="#0066FF"><TT>ATMClient &quot;Jeremy Rosenberger&quot; 123456789 &quot;123 Main Street&quot; Doe 1234</TT></FONT></PRE><P>The <TT>ATMClient</TT> will again display the following:</P><PRE><FONT COLOR="#0066FF"><TT>ATMClient: Creating new Customer:</TT><TT>  name: Jeremy Rosenberger</TT><TT>  Social Security number: 123456789</TT><TT>  address: 123 Main Street</TT><TT>  mother's maiden name: Doe</TT><TT>ATMClient: Successfully bound to a BankServer.</TT><TT>ATMClient: Using Bank &quot;First Bank&quot; and ATM &quot;First Bank ATM&quot;.</TT><TT>ATMClient: Opened new Account:</TT><TT>  account number: Account00000000</TT><TT>  creation date: Oct 20 1997</TT><TT>  account balance: 0</TT><TT>ATMClient: Getting ATMCard from Bank.</TT><TT>ATMClient: Performing transactions.</TT><TT>  Depositing $250.00...</TT><TT>  New balance is $250</TT><TT>  Withdrawing $500.00...</TT><TT>ATMClient: Exception caught: Insufficient funds</TT><TT>ATMClient: Performing more transactions.</TT><TT>  Depositing $500.00...</TT><TT>  New balance is $750</TT><TT>  Withdrawing $250.00 with incorrect PIN...</TT><TT>ATMClient: Exception caught: Invalid PIN or No authorization (as expected)</TT><TT>  Deleting Account.</TT><TT>  Attempting to cause an exception by deleting Account again.</TT><TT>ATMClient: Exception caught: Invalid Account (as expected)</TT></FONT></PRE><P>At this point, the <TT>ATMClient</TT> will sleep for two minutes while waitingfor messages from the <TT>Bank</TT>. Be patient, and the <TT>ATMClient</TT> willeventually output</P><PRE><FONT COLOR="#0066FF"><TT>CustomerImpl: Received account update:</TT></FONT></PRE><PRE><FONT COLOR="#0066FF"><TT>  New balance is $750</TT></FONT></PRE><P>All this will go by very quickly, but after it's all over, you can go to the otherapplication windows and see some evidence of what transpired here. Looking firstat the <TT>BankServer</TT> application, you'll see this (with new output messageshighlighted in <B>bold</B>):</P><PRE><FONT COLOR="#0066FF"><TT>BankServer ready.</TT><B><TT>BankServerImpl: Returning list of 1 Banks.</TT></B><B><TT>BankServerImpl: Returning list of 1 ATMs.</TT></B></FONT></PRE><P>The output of the other applications will be the same as last time, except forthe <TT>Bank</TT> application. Turn your attention to the window in which the <TT>Bank</TT>is running and you will see the following, familiar output:</P><PRE><FONT COLOR="#0066FF"><TT>Bank &quot;First Bank&quot; ready.</TT><TT>BankImpl: Creating new CheckingAccount for Customer Jeremy Rosenberger.</TT><TT>AccountImpl: Insufficient funds to withdraw specified amount.</TT><TT>BankImpl: Deleting Account &quot;Account00000000&quot;.</TT><TT>BankImpl: Attempted to delete invalid Account.</TT></FONT></PRE><P>Stay tuned for a few moments, and you will see the following (if it took you aminute or so to bring the <TT>Bank</TT> output window up, this might already be onyour screen):</P><PRE><FONT COLOR="#0066FF"><TT>BankImpl: Updating Accounts.</TT></FONT></PRE><P>Recall that this message is output just before the second thread in the <TT>Bank</TT>application sends the update messages to all the <TT>Account</TT> owners.<H2><A NAME="Heading10"></A><FONT COLOR="#000077">Ideas for Future Enhancements</FONT></H2><P>You've only begun to scratch the surface of what can be done with CORBA. As faras the <TT>Bank</TT> application is concerned, there are a number of possible enhancements.As you progress into advanced CORBA topics in the upcoming days, you'll make a fewmore enhancements to the <TT>Bank</TT> application, but the possibilities for enhancementsare limitless. If you want to further experiment with the <TT>Bank</TT> application,here are a few ideas:<UL>	<LI><TT>SavingsAccount</TT>s already support an interest rate, but the interest is	never added to the account balances. Implement a mechanism--probably using a separate	thread in the <TT>Bank</TT> application--that periodically adds interest to each	<TT>SavingsAccount</TT>.<BR>	<BR>		<LI><TT>CheckingAccount</TT>s typically feature overdraft protection; that is, withdrawing	a greater amount than is available in the account automatically dips into the customer's	line of credit.<BR>	<BR>		<LI>As mentioned numerous times before, the sample code presented here is not thread-safe.	Modify the code so that it is thread-safe.<BR>	<BR>		<LI>Add a new type of account--perhaps a mutual fund or stock market account. As	an added feature, make it possible for customers to subscribe to a service that will	automatically inform them--using push messaging--of the account's performance.</UL><P>Again, the possibilities for enhancements are endless. Adding features or robustnessto the <TT>Bank</TT> application on your own will help you to hone your skills fordeveloping CORBA applications.<H2><A NAME="Heading11"></A><FONT COLOR="#000077">Summary</FONT></H2><P>Today you added a simple capability to the <TT>Bank</TT> application--for the<TT>Bank</TT> server to push updated information to the bank customers. Althoughthe implementation for this capability is simple, the potential of the push architectureis very great. Indeed, as this book is being written, a number of companies are vyingto create the de facto standard for pushing content to users on the Internet.</P><P>You were also reminded--albeit briefly--of the importance of writing thread-safecode in a multithreaded environment. Although this book takes a &quot;do as I say,not as I do&quot; approach to writing thread-safe code, it is very important thatwhen writing multithreaded applications, you take care to ensure thread safety. Ina sample application with only one user, thread safety is not likely to be an issuebecause chances are small that two threads will use the same data at the same time.However, in a production system--particularly an enterprisewide system--the penaltyfor writing non-thread-safe code can be stiff, usually resulting in the corruptionof data.</P><P>On Day 10 you'll shift gears into some more advanced CORBA topics--a number ofdesign issues that are involved with CORBA, along with a few suggestions about howto deal with those issues. You'll get a small break from further developing the <TT>Bank</TT>application, but the example does return in future chapters when you study additionaladvanced CORBA topics, such as the use of the Dynamic Invocation Interface (DII),CORBAservices and CORBAfacilities, and using Java with CORBA.<H2><A NAME="Heading12"></A><FONT COLOR="#000077">Q&amp;A</FONT></H2><DL>	<DD><B>Q What's the big deal about push technology anyway?</B><BR>	<B><BR>	A</B> Properly implemented, a push architecture can save users the trouble of actively	searching for desired information. (However, if the application goes overboard with	the information pushed to the user, the user might suffer from the new problem of	information overload.) Also, push technology has the potential to conserve system	resources. Information can be delivered to users as it is updated (as opposed to	requiring users to periodically check for updated data, which can be inefficient	if the data doesn't change very often).<BR>	<B><BR>	Q It seems to me that push method calls can almost always be <TT>oneway</TT> calls.	Is this accurate?<BR>	<BR>	A</B> To the extent that the pushed information is not considered essential for the	purposes of the application, this is true. Using <TT>oneway</TT> calls allows for	more efficient server implementations (because the server does not have to wait for	a reply from the clients), at the expense of reliability of message delivery. In	the case of a server that delivers account balance updates or stock quote updates	to casual subscribers, it usually doesn't matter if an occasional update message	is lost. When the information being updated is considered essential, <TT>oneway</TT>	is usually not a good choice.</DL><H2><A NAME="Heading13"></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="Heading14"></A><FONT COLOR="#000077">Quiz</FONT></H3><DL>	<DD><B>1</B>. Why does the issue of thread safety become important in the sample	application developed in this chapter?<BR>	<B><BR>	2</B>. Instead of using <TT>oneway</TT> methods to notify clients of updates, can	you think of another way to efficiently send update messages to clients? (<B>Hint:</B>	Multithreading could come in handy here.)</DL><H3><A NAME="Heading15"></A><FONT COLOR="#000077">Exercises</FONT></H3><DL>	<DD><B>1</B>. It was noted earlier in the chapter that no facility currently exists	to cancel the automatic account update service. Provide an IDL method signature for	such an operation. Don't forget to include appropriate exceptions, if any.<BR>	<B><BR>	2</B>. Implement the account update cancellation method from Exercise 1.<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../ch08/ch08.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch10/ch10.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 + -