Flex sample application

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (35)

View page history
import mx.controls.Alert;
import mx.messaging.messages.IMessage;
import flash.utils.Timer;
import flash.events.TimerEvent;


[Bindable]
private var userItems:ArrayCollection;

private var userItem:UserItem;

private var timer:Timer;

public function subscribe(): void
{
if(timer) {
timer.stop();
}
timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, timerCompleted);
timer.start();
subscribeButton.enabled=false;
unsubscribeButton.enabled=true;
}

public function unsubscribe(): void
{
timer.stop();
subscribeButton.enabled=true;
unsubscribeButton.enabled=false;
}

public function timerCompleted(evt:TimerEvent): void
{
getList();
}

public function isSubscribeEnabled(): Boolean
{
if(timer.running) {
Alert.show("running");
} else {
Alert.show("stopped");
}

return !timer.running;
}

public function save():void
{
loginText.text = "";
}

public function onFault(event:FaultEvent):void
{

private function messageHandler(message:IMessage):void
{
getList();
}
]]>
</mx:Script>

<mx:Form width="100%" height="100%" defaultButton="{saveButton}">
<mx:FormHeading label="User List" width="100%"/>
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="100%" height="100%">
<mx:Button label="Subscribe for updates" id="subscribeButton" click="subscribe()" enabled="true"/> click="consumer.subscribe()" enabled="{!consumer.subscribed}"/>
</mx:GridItem>
<mx:GridItem width="100%" height="100%">
<mx:Button label="Unsubscribe from updates" id="unsubscribeButton" click="unsubscribe()" enabled="false"/> click="consumer.unsubscribe()" enabled="{consumer.subscribed}"/>
</mx:GridItem>
</mx:GridRow>
<mx:method name="remove" result="onResultRemove(event)" fault="onFault(event)"/>
<mx:method name="getList" result="onResultGetList(event)" fault="onFault(event)"/>
</mx:RemoteObject>

<mx:Consumer id="consumer" destination="feed" message="messageHandler(event.message)"/>

</mx:TitleWindow>
{code}
Create file services-config.xml at the root of source code

We use Consumer/Producer communication model to receive events from server side.
If you need to configure Flex server side you have to create file services-config.xml and put it at the package org.sevenhats.flex. Also you need to share access to it from Manifest.mf
{code}
Export-Package: org.sevenhats.flex
{code}

File services-config.xml
{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<services>
<service-include file-path="remoting-config.xml" />
</services>
<service id="message-service" class="flex.messaging.services.MessageService">
<adapters>
<adapter-definition id="actionscript"
class="flex.messaging.services.messaging.adapters.ActionScriptAdapter"
default="true" />
</adapters>
<default-channels>
<channel ref="my-streaming-amf" />
<channel ref="my-polling-amf" />
</default-channels>
<destination id="feed">
<!--
Destination specific channel configuration can be defined if needed
<channels> <channel ref="my-streaming-amf"/> </channels>
-->
</destination>
</service>
</services>
<!-- Sevenhats factory registration -->
<factories>
<factory id="sevenhats" class="org.sevenhats.flex.SevenhatsFactory" />
</factories>
<channels>
<channel-definition id="channel-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
<factories>
<factory id="sevenhats" class="org.sevenhats.flex.SevenhatsFactory" />
</factories>
<channels>
<channel-definition id="channel-amf"
class="mx.messaging.channels.AMFChannel">
<endpoint
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint" />
<properties>
<polling-enabled>false</polling-enabled> <properties>
</properties>
</channel-definition>
</channels>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>
<channel-definition id="my-streaming-amf"
class="mx.messaging.channels.StreamingAMFChannel">
<endpoint
url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf"
class="flex.messaging.endpoints.StreamingAMFEndpoint" />
</channel-definition>
<channel-definition id="my-polling-amf"
class="mx.messaging.channels.AMFChannel">
<endpoint
url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling"
class="flex.messaging.endpoints.AMFEndpoint" />
<properties>
<polling-enabled>true</polling-enabled>
<polling-interval-seconds>4</polling-interval-seconds>
</properties>
</channel-definition>
</channels>
</services-config>
{code}

Please pay attention that endpoints at SevenHats framework should match pattern '/messagebroker/*' for endpoints as in example above.

Create file remoting-config.xml at the root of source code too. package org.sevenhats.flex.
{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.RemotingService">
class="flex.messaging.services.RemotingService">

<adapters>
<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true"/>
</adapters>

</service>
{code}

org.sevenhats.flexsample.api.UserService is service interface at OSGi environment. You can see how we define this service and its implementation below in flexsample-context.xml and flexsample-context-osgi.xml.

&nbsp;Compile it to swf with Ant
{code:xml}
<mxmlc file="org.sevenhats.flexsample/src-flash/index.mxml" keep-generated-actionscript="false"
output="org.sevenhats.flexsample/src/org/sevenhats/flexsample/flash/index.swf">
<compiler.services>org.sevenhats.flexsample.flex.config/src/org/sevenhats/flex/services-config.xml</compiler.services>
<compiler.context-root>/</compiler.context-root>
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>