示例:高速缓存 Web Service

可对 Web Service 应用程序构建一组高速缓存策略和 SOAP 消息。

以下是为简单的 Web Service 应用程序构建一组高速缓存策略的示例。 本示例中的应用程序存储股票报价,并且可以执行以下操作:读取和更新指定股票的价格以及购买该股票。

以下是应用程序可以接收的两个 SOAP 消息示例,带有 HTTP 请求头。

第一个消息样本包含针对 GetQuote 操作的 SOAP 消息,请求 IBM®的报价。 这是从后端获取其数据的只读操作,并且是高速缓存的良好候选者。 在此示例中,高速缓存 SOAP 消息并且将超时放入其条目以保证它返回的引用是当前的。

消息示例 1
POST /soap/servlet/soaprouter  
HTTP/1.1  
Host: www.myhost.com  
Content-Type: text/xml; charset=utf-8  
SOAPAction: urn:stockquote-lookup
<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
SOAP-ENV:encodingStyle=http://schemas.xmlsoap.org/soap/encoding/>
<SOAP-ENV:Body>
<m:getQuote xmlns:m=urn:stockquote>
<symbol>IBM</symbol>
</m:getQuote>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>     
该请求中的 SOAPAction HTTP 头在 SOAP 规范中定义,并且由 HTTP 代理服务器使用,把请求分派到特定的 HTTP 服务器。 WebSphere® Application Server 动态缓存可在其缓存策略中使用该标头来建立 ID,而无需解析 SOAP 消息。

消息示例 2 描述 BuyQuote 操作的 SOAP 消息。 当消息 1 可高速缓存时,此消息却不能,这是因为它更新后端数据库。

消息示例 2
POST /soap/servlet/soaprouter 
HTTP/1.1  
Host: www.myhost.com 
Content-Type: text/xml; charset=utf-8 
SOAPAction: urn:stockquote-update
<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
SOAP-ENV:encodingStyle=http://schemas.xmlsoap.org/soap/encoding/>
<SOAP-ENV:Body>
<m:buyStock xmlns:m=urn:stockquote>
<symbol>IBM</symbol>
</m:buyStock>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>  
下面的图形描述了如何用 SOAP 消息调用方法。 在 Web Service 术语中,尤其是 Web Service 描述语言 (WSDL),服务是操作(例如 getQuote 和 buyStock)的集合。 主体元素名称空间(在本示例中为 urn:stockquote)定义服务,第一个主体元素的名称表明操作。
使用 SOAPmessages:caching 调用方法

以下是 getQuote 操作的 WSDL 的示例:

<?xml version=1.0?>
<definitions name=StockQuoteService-interfacetargetNamespace=http://www.getquote.com/StockQuoteService-interfacexmlns:tns=http://www.getquote.com/StockQuoteService-interfacexmlns:xsd=http://www.w3.org/2001/XMLSchemaxmlns=soap=http://schemas.xmlsoap.org/wsdl/soap/xmlns=http://schemas.xmlsoap.org/wsdl/<message name=SymbolRequest>
<part name=return type=xsd:string/>
</message>
<portType name=StockQuoteService>
<operation name=getQuote>
<input message=tns:SymbolRequest/>
<output message=tns:QuoteResponse/>
</operation>
</portType>
<binding name=StockQuoteServiceBindingtype=tns:StockQuoteService>
<soap:binding style=rpc transport=http://schemas.xmlsoap.org/soap/http/>
<operation name=getQuote>
<soap:operation soapAction=urn:stockquote-lookup/>
<input>
<soap:body use=encoded namespace=urn:stockquoteencodingStyle=http://schemas.xmlsoap.org/soap/encoding//>
</input>
<output>
<soap:body use=encoded namespace=urn:stockquotesencodingStyle=http://schemas.xmlsoap.org/soap/encoding//>
</output>
</operation>
</binding>
</definition>

要为 Web Service 应用程序构建一组高速缓存策略,请配置 WebSphere Application Server 动态高速缓存以识别操作的可高速缓存服务操作。

WebSphere Application Server 检查 请求,以确定是否可根据为应用程序定义的缓存策略缓存传入的信息。 HTTP 在本示例中,不高速缓存 buyStock 和 stock-update,但高速缓存 stockquote-lookup。 在此 Web 应用程序的 cachespec.xml 文件中,需要为这些服务定义高速缓存策略以便动态高速缓存能处理 SOAPAction 和服务操作。

WebSphere Application Server 使用 Web Service 高速缓存标识中的操作和消息体,每个标识都具有与其关联的组件。 因此,每个 Web Service <cache-id> 规则均仅包含两个组件。 第一个用于操作。 因为您可以使用 SOAPAction 头或主体中的服务操作来执行 stockquote-lookup 操作,所以您必须定义两种不同的 <cache-id> 元素,其中一个元素对应一种方法。 第二个组件的类型为 body,用于定义 WebSphere Application Server 应如何将消息体合并到高速缓存标识中。 您可以使用主体的散列,虽然可以在标识中使用文字进入消息。

传入的 HTTP 请求由 WebSphere Application Server 进行分析,以确定哪些 <cache-id> 规则与之匹配。 接着,就应用这些规则以形成高速缓存或无效标识。

以下是 cachespec.xml 文件的样本代码,定义 SOAPAction 和 servicesOperation 规则:

<cache>
<cache-entry>
	<class>webservice</class>
	<name>/soap/servlet/soaprouter</name>
	<sharing-policy>not-shared</sharing-policy>
	<cache-id>
		<component id= type=SOAPAction>
			<value>urn:stockquote-lookup</value>
		</component>
		<component id=Hash type=SOAPEnvelope/>
			<timeout>3600</timeout>
			<priority>1<priority>
		</component>
	</cache-id>
	<cache-id>
		<component id= type=serviceOperation>
			<value>urn:stockquote:getQuote</value>
		</component>
		<component id=Hash type=SOAPEnvelope/>
			<timeout>3600</timeout>
			<priority>1</priority>
		</component>
	</cache-id>
</cache-entry>
</cache>