AS3: Creating static XML with ColdFusion and AIR

01/02/2010 in RIA View Comments

stockAIR
To build off of a couple posts earlier, the following is an example of how to access the Yahoo stock information and create a static XML with AIR file that Flash can access. I went this route after seeing the server hit that we took when 3k+ users logged in at the same time and tried to access the stock ticker. The stock ticker was hitting the Yahoo service every few seconds and was significantly slowing things down. What I decided to do was to still use the CF service to access the stocks, but instead hit it with an AIR app that then takes that information and creates a static XML file for the Flash stock ticker to access. This also solved the “after hours” stock information as the Flash file would always be hitting the static XML, which wouldn’t change after 5:00 PM. So, to define the process, Yahoo delivers stock information, the AIR application is triggered by the server via a scheduled task, AIR accesses the ColdFusion file that hits Yahoo for the stock information, then AIR takes that information and creates a static XML file. This basically took the hit to Yahoo to once every 5 minutes by ONE application, rather than once every few seconds by any number of web hits…typically at least 1k…

Here is the ColdFusion code to hit Yahoo and gain access to the stock information:

<cfset urlAddress_Google="http://download.finance.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgv&e=.csv" />
<cfhttp url="#urlAddress_Goog#" method="GET" resolveurl="Yes" throwOnError="Yes"/>
<cfset stockCompanyName01 = "Google">
<cfset stockInfo01 = CFHTTP.FileContent>
 
<cfset urlAddress_Dow="http://download.finance.yahoo.com/d/quotes.csv?s=^DJI&f=sl1d1t1c1ohgv&e=.csv" />
<cfhttp url="#urlAddress_Dow#" method="GET" resolveurl="Yes" throwOnError="Yes"/>
<cfset stockCompanyName02 = "Dow">
<cfset stockInfo02 = CFHTTP.FileContent>
 
<cfprocessingdirective suppresswhitespace="Yes">
<cfcontent type="text/xml; charset=utf-16">
 
<cfxml variable="xmlobject">
<stocks>
<company name="<cfoutput>#stockCompanyName01#</cfoutput>" >
<cfoutput>#stockInfo01#</cfoutput>
</company>
<company name="<cfoutput>#stockCompanyName02#</cfoutput>" >
<cfoutput>#stockInfo02#</cfoutput>
</company>
</stocks>
</cfxml>
 
<cfset baseStock=toString(xmlobject)>
<cfset finalStock=replace(baseStock, "UTF-8", "utf-16")>
 
<cfoutput>#finalStock#</cfoutput>
 
</cfprocessingdirective>

Here is the AIR code to hit the ColdFusion file:

<![CDATA[ 
import mx.rpc.events.FaultEvent;
import mx.rpc.Fault; 
import mx.rpc.events.ResultEvent; 
 
//VARIABLES 
[Bindable] 
private var stockXML:XML; 
 
[Bindable] 
private var sourcePath:String = "[PATH TO CF FILE]"; 
 
[Bindable] 
private var stream:FileStream; 
 
[Bindable] 
private var stockSourcePath:String = "[PATH TO SAVE XML]"; 
 
//INITIALIZATION METHODS 
private function init():void{ stockService.send(); } 
private function stockResultHandler(event:ResultEvent):void{ 
     stockXML = event.result as XML; saveFile(); 
} 
private function stockFaultHandler(event:FaultEvent):void{ //Fault Handler } ]]>
 
</mx:Script>
 
<mx:Script>
<![CDATA[ 
 
public var stockFile:File = new File(); 
public function openFile():void{ 
     stockFile.nativePath = stockSourcePath; stream = new FileStream(); 
     stream.open(stockFile, FileMode.READ); 
     var str:String = stream.readUTFBytes(stream.bytesAvailable); 
     stream.close(); 
     saveFile(); 
} 
 
public function saveFile():void{ 
     stockFile.nativePath = stockSourcePath; 
     var stream:FileStream = new FileStream() 
     stream.open(stockFile, FileMode.WRITE); 
     var str:String = stockXML; 
     stream.writeUTFBytes(str); 
     stream.close(); 
 
//Auto closes the AIR App 
     this.close(); } ]]>

Other posts you may find helpful:

, ,

Leave a Reply

Gravatars are enabled. Register for free!

blog comments powered by Disqus