Cette application Flex déployée sur AIR permet de suivre presque en temps réel l’évolution de la mémoire utilisée sur votre serveur.
Elle communique avec un script herbergé sur votre serveur via un RemoteObject (donc par AMF). Libre à vous de changer cela vers du XML … Le script fourni est un script Coldfusion, libre à vous également de l’adapter à votre langage:
Les sources
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5" implements="mx.rpc.IResponder">
<mx:Script>
<![CDATA[
import mx.formatters.NumberFormatter;
import mx.collections.ArrayCollection;
import flash.utils.setTimeout;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
[Bindable] private var listData:ArrayCollection = new ArrayCollection();
private function refresh():void
{
rmt.getMemory().addResponder(this);
}
public function result(data:Object):void
{
var result:Object = data.result;
this.lblFree.text = formatSize(result.free);
this.lblTotal.text = formatSize(result.total);
this.lblMax.text = formatSize(result.max);
listData.addItem(result);
if (listData.length > 100)
{
listData.removeItemAt(listData.length - 1);
}
if (chk.selected)
{
setTimeout(refresh,1000);
}
}
private function formatSize(value:Number):String
{
var f:NumberFormatter = new NumberFormatter();
f.precision = 2;
if (value < 1024)
{
return f.format(value) + " MB";
}
else
{
return f.format(value / 1024) + " GB";
}
}
public function fault(info:Object):void
{
Alert.show((info as FaultEvent).message.toString() + "","Error");
}
private function getManagerFile():void
{
}
]]>
</mx:Script>
<mx:RemoteObject id="rmt" destination="ColdFusion" source="JavaGetMemory" endpoint="{txtUrl.text}" showBusyCursor="true" />
<mx:ApplicationControlBar width="100%">
<mx:Label text="Url:" />
<mx:TextInput id="txtUrl" text="http://127.0.0.1/flex2gateway" />
<mx:Spacer width="100%" />
<mx:Button label="Refresh" enabled="{!chk.selected}" click="refresh()" />
<mx:CheckBox id="chk" label="Continue" click="refresh()" />
</mx:ApplicationControlBar>
<mx:Accordion width="100%" height="100%">
<mx:VBox width="100%" height="100%" label="Data">
<mx:Panel width="100%" title="Raw Data">
<mx:Form>
<mx:FormItem label="Free Memory:">
<mx:Label id="lblFree" />
</mx:FormItem>
<mx:FormItem label="Total Memory:">
<mx:Label id="lblTotal" />
</mx:FormItem>
<mx:FormItem label="Max Memory:">
<mx:Label id="lblMax" />
</mx:FormItem>
</mx:Form>
</mx:Panel>
<mx:Panel width="100%" height="100%" title="Graph" layout="vertical">
<mx:LineChart id="lc" width="100%" height="100%" dataProvider="{listData}">
<mx:series>
<mx:LineSeries yField="free" displayName="Free Memory" />
<mx:LineSeries yField="total" displayName="Total Memory" />
<mx:LineSeries yField="max" displayName="Max Memory" />
</mx:series>
<mx:horizontalAxis>
<mx:CategoryAxis categoryField="m" />
</mx:horizontalAxis>
</mx:LineChart>
<mx:Legend dataProvider="{lc}" />
</mx:Panel>
</mx:VBox>
<mx:VBox width="100%" height="100%" label="Server File: JavaGetMemory.cfc">
<mx:Text width="100%" text="Just copy the text below in a file called JavaGetMemory.cfc on the root of your Dev Site:" />
<mx:TextArea width="100%" height="100%">
<mx:text>
<![CDATA[
<cfcomponent>
<cffunction access="remote" name="getMemory">
<cfscript>
var runtime = CreateObject("java","java.lang.Runtime").getRuntime();
var result = StructNew();
result["free"] = runtime.freeMemory() / 1024 / 1024;
result["total"] = runtime.totalMemory() / 1024 / 1024;
result["max"] = runtime.maxMemory() / 1024 / 1024;
return result;
</cfscript>
</cffunction>
</cfcomponent>
]]>
</mx:text>
</mx:TextArea>
</mx:VBox>
</mx:Accordion>
</mx:WindowedApplication>
