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

📄 lib0024.html

📁 java外企软件工程师就业班 J2EE方向 《J2EE架构师手册》 电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Selecting a Communication Method</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0023.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0025.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch04"></a>
<div class="section">
<h2 class="first-section-title"><a name="101"></a><a name="ch04lev1sec1"></a>Selecting a Communication Method</h2><p class="first-para">Communication with another application is either synchronous or asynchronous. With <i class="emphasis">synchronous communication,</i> the transmission occurs immediately and an error is generated if the external application is down. Examples of synchronous communication technologies include EJBs, RMI, Web services, CORBA, and HTTP. And I have even seen some projects use HTTP <a name="102"></a><a name="IDX-35"></a>programmatically as a method for communication. With <i class="emphasis">asynchronous communication,</i> application processing continues while the communication is being completed. The application your communication is directed at may not receive your message immediately. Examples of technologies that can be used asynchronously include messaging/JMS and Web services.</p>
<p class="para">Whether you choose synchronous or asynchronous communication depends on the business requirements of the application. Use cases requiring external interfaces should describe the business process in enough detail that you can make this choice. If the external application requires immediate action, your best choice is synchronous communication. On the other hand, if delayed processing is acceptable from a business perspective, your best choice is asynchronous communication. The following sections provide more detail on each communication method.</p>
<div class="section">
<h3 class="sect3-title">
<a name="103"></a><a name="ch04lev2sec1"></a>Asynchronous Communication</h3>
<p class="first-para">Asynchronous communication is typically implemented via messaging technologies, known in the Java world as JMS. You can also use asynchronous communication for broadcasts, commonly known as publish/subscribe capability. That is, you can send a message to any number of applications that care to listen. The message content is usually informational rather than some type of processing instruction.</p>
<p class="para">Depending on your messaging vendor, messaging can be platform independent. Most of my clients use IBM's MQ/Series. MQ has client interfaces for most platforms and would be a viable choice for communicating between a Windows .Net application and a J2EE application, for example.</p>
<p class="last-para">Messaging technologies have very loose coupling. Applications can generally make any technology change the way they want to without affecting messaging interfaces, as long as they continue to support the message transmission protocol and format. For example, you could convert one of the applications from COBOL to Java without requiring any changes to other message senders or receivers.</p>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="104"></a><a name="ch04lev2sec2"></a>Synchronous Communication</h3>
<p class="first-para">Although you can implement synchronous communication with messaging, it is more common to implement it using Web services: RMI/EJB, CORBA, or HTTP. If you use messaging for synchronous communication, it is generally point-to-point, not any type of broadcast. Synchronous communication is generally configured to generate an error if the external application is not available and structured to require a response. You can think of <a name="105"></a><a name="IDX-36"></a>synchronous communication as an external call. Web services are just different ways to structure that call.</p>
<p class="para">Using messaging in a synchronous fashion confuses some people, but it is common in the healthcare industry. I've also seen it used in utility companies as well. Consider the following example of how a purchasing system uses synchronous messaging to obtain available credit information from an accounting application:</p>
<ol class="orderedlist">
<li class="first-listitem">
<p class="first-para">The purchasing system creates an XML document containing instructions to return the amount of credit available to a specific customer.</p>
</li>
<li class="listitem">
<p class="first-para">The purchasing system sends this XML document as a message to a specific queue monitored by the accounting application.</p>
</li>
<li class="listitem">
<p class="first-para">The purchasing system waits for a response from the accounting application.</p>
</li>
<li class="listitem">
<p class="first-para">The accounting application receives the message, parses the XML text, and obtains the needed credit information.</p>
</li>
<li class="listitem">
<p class="first-para">The accounting application creates an XML document with the credit information and responds to the message received, incorporating the XML text in the response.</p>
</li>
<li class="listitem">
<p class="first-para">The purchasing system receives the response, parses the XML document created by the accounting application, and continues processing.</p>
</li>
</ol>
<p class="para">Increasingly, Web services are being used for synchronous communication. The primary advantages of Web services are (1) they are platform independent to the point that the external application platform is not relevant, and (2) they are more loosely coupled than CORBA or RMI/EJB calls. Web services are a good choice for synchronously communicating with non-Java applications (such as .Net applications).</p>
<p class="para">Using HTTP to communicate with another application has similar advantages and disadvantages to using Web services because they both use HTTP. HTTP communication does require that you adopt some type of application protocol because it doesn't natively use SOAP.</p>
<p class="para">To understand the concept of using HTTP as a communication method, consider the following example of a purchasing application using HTTP to request information from an inventory management system:</p>
<a name="106"></a><a name="IDX-37"></a>
<ol class="orderedlist">
<li class="first-listitem">
<p class="first-para">The purchasing application issues an HTTP request (using java.net.URL) to the inventory application requesting current inventory levels for an item (e.g., laundry detergent).</p>
</li>
<li class="listitem">
<p class="first-para">A servlet in the inventory management system receives the request and initiates processing.</p>
</li>
<li class="listitem">
<p class="first-para">The inventory management system examines its database and determines the quantity of the requested item available at all warehouses.</p>
</li>
<li class="listitem">
<p class="first-para">The inventory management system constructs an XML document containing the quantity information.</p>
</li>
<li class="listitem">
<p class="first-para">The servlet in the inventory management system returns the XML document to the caller in the same way it would return HTML to a browser.</p>
</li>
<li class="listitem">
<p class="first-para">The purchasing application receives the XML document, parses it, and continues processing using the inventory information received.</p>
</li>
</ol>
<p class="para">In this example, the XML format used must have the same design as XML used with messaging technologies.</p>
<p class="para">CORBA allows external calls to applications written on any platform that supports CORBA. In this sense, CORBA is more platform independent than is RMI/EJB but less independent than Web services or HTTP. CORBA is also slightly more mature than Web services or RMI/EJB.</p>
<p class="para">Both RMI services and J2EE enterprise beans are restricted to Java applications. This tight coupling between applications can create deployment difficulties because both applications need to use compatible versions of common classes to avoid marshalling errors.</p>
<p class="last-para">All types of synchronous communication require an error-processing strategy if the external application is unavailable or a transaction cannot be processed properly. Your options are either to produce an error or to develop a mechanism within your own application to retry later. I prefer the latter when possible because, although this does introduce complexity into the application, the administrative overhead dealing with outages is too high otherwise.</p>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="107"></a><a name="ch04lev2sec3"></a>Comparing the Two Methods</h3>
<p class="first-para">Let's look at some examples of each method. If you send a synchronous message or call another application, giving it an instruction to do something for a customer, your application waits for that transmission to occur before <a name="108"></a><a name="IDX-38"></a>continuing processing. Consequently, your application will know when there is a communication failure. If a response is expected, your application will know when processing errors occur with the request. Unfortunately, because your application is waiting for the communication and possibly a response, there is a portion of application performance you don't control.</p>
<p class="para">If you sent the same message asynchronously, your application would not wait for the communication. Because the communication physically happens <i class="emphasis">after</i> an application initiates it, it often appears faster than synchronous communication. And asynchronous communication is more fault tolerant. If the target application is temporarily down, the message will be delivered automatically when that application comes back up, with no manual intervention. The disadvantage is that asynchronous communication cannot detect processing errors and does not know when your transaction will be physically processed.</p>
<p class="para">
<a class="internaljump" href="#ch04table01">Table 4.1</a> summarizes the features of each method.</p>
<a name="109"></a><a name="ch04table01"></a>
<table class="table" border="1">
<caption class="table-title">
<span class="table-title"><span class="table-titlelabel">Table 4.1: </span>Features of Synchronous and Asynchronous Communication Methods</span>
</caption>
<thead>
<tr valign="top">
<th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">Feature</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">EJB</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">Web Services</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">Messaging/ JMS</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">RMI</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">HTTP</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">CORBA</b>
</p>
</th>
</tr>
</thead>
<tbody>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Caller platform requirements</b>
</p>
</td><td class="td" align="left">

⌨️ 快捷键说明

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