Announcement Announcement Module
Collapse
No announcement yet.
Externalizing ChannelSet Config WIth Spring Actionscript Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Externalizing ChannelSet Config WIth Spring Actionscript

    Here are a few tips gained from how I managed to externalize ChannelSet configuration in a Flex Client using Spring Actionscript (SAS).

    Disclaimers
    1) I'm pretty new at this, but what I've done works
    2) Parsley or Swiz or whatever other framework may be better but I used SAS because it leverages what little I know about Spring Framework for Java.
    3) These are just notes on things that caught me - you need to read the SAS documentation carefully, it is very clear and precise, but some important details are easy to miss if you go too quickly

    Problem:

    I have an Flex client and want it to talk to one server during test and another after deployment. In fact there are several deployments of the same application differing exclusively by which server they talk to.

    The examples of Flex-Blaze integration I see out there tend to have the channel set hard-coded in the client and clearly this will not work for me (or you?). The examples often acknowledge that hard-coding is bad practice and say "use a framework like Spring Actionscript, Swiz or Parsley". So I did. Here's what I learned:

    1) download SAS *with dependencies* (Obvious, right? Wasn't to me.)

    2) Make sure you add both the SAS "dist" and "lib" directories to your Flex Builder Path. (Aside: there's other cool stuff in the as3-commons libraries - I like their Assert class for example.)

    3) Set the Flex Compiler Option -keep-as3-metadata += Autowired. You want Autowired because it will let you grab stuff from the SAS context whenever you need it. Like, for example, any place you want to create a service and configure it with a channelSet from the context.

    4) write your context configuration XML file as per the documentation. In mine I configure a channelSet as follows when working against a local server - and then simply change it when I deploy.
    Code:
        <object id="channelSet" class="mx.messaging.ChannelSet">
            <method-invocation name="addChannel">
                <arg>
                    <object id="amfChannel" class="mx.messaging.channels.AMFChannel">
                        <property name="url"
                            value="http://localhost:8080/MutantManager/messagebroker/amf" />
                    </object>
                </arg>
            </method-invocation>
        </object>
    5) To make autowiring work, you must also include this in your SAS context config file:

    Code:
        
    <object id="autowiringStageProcessor"  class="org.springextensions.actionscript.stage.DefaultAutowiringStageProcessor" />
    6) Oh yes, but of course nowhere in your code does anyone include that lovely DefaultAutowiringStageProcessor so of course you get a runtime error when you first try to run it. You solve that little obstacle in one of the 6 ways described in the last section of the SAS documentation. Cutting to the chase, I do the following in my Application:

    Code:
                import org.springextensions.actionscript.stage.DefaultAutowiringStageProcessor;
                ...
                // just to get the class loaded - it's a Spring Actionscript thing
                public var _dummy:DefaultAutowiringStageProcessor;
    7) also in your main code you need to deal with a bit of timing so that the SAS context is loaded up before you start doing the heavy lifting. Here's what I wrote: (In case it is not obvious, MainView holds my client application, and I named my config file client-config.xml which is not the default.)

    Code:
                // this is the Spring ActionScript Container wich holds 
                // externally configured objects and dependencies.
                public var sasContext:FlexXMLApplicationContext;
                            
                protected function applicationCompleteHandler(event:FlexEvent):void
                {
                    sasContext = new FlexXMLApplicationContext();
                    sasContext.addConfigLocation("client-context.xml");
                    sasContext.addEventListener(Event.COMPLETE, sasContextLoaded_handler);
                    sasContext.load();
                    
                }
                
                // Timing note: for autowiring, we need to ensure that the 
                // Spring Actionscript context is loaded before we add anything to the stage.
                public function sasContextLoaded_handler(e:Event):void {
                    
                    var mainView:MainView = new MainView();
                    addElement(mainView);
                                    
                }
    8) Within the staged objects, you can now pull the channelSet out of the SAS context to configure services anywhere in your want with this little bit of code:

    Code:
                [Autowired]
                public var channelSet:ChannelSet;

    I suppose that there are a million other ways to have accomplished this, but I hope this helps someone. But please, read SAS documentation.

    If you are critical of this approach - I am more than happy to learn why.

    Ted Moens

  • #2
    This question comes up all the time, so I'm sure this example will be very helpful, thanks!

    Comment

    Working...
    X