Remote ActionScript Dynamic Endpoints

When using remote objects in Flex (for BlazeDS) you may want to change the endpoint (server, port and context root) and switch between servers. This is useful if your development environment contains both testing and production servers (as it should).

The usual way is to define these settings in the Flex/Flash Builder project when the project is created but this is not easily changed once it's been set up.

An alternative way to set endpoints is is to set them via ActionScript. If you do this you can set up the project as an ordinary Flex project with no server settings.

Here's a class you can use that extends RemoteObject. It creates the endpoint in it's constructor from static constants but these could be passed in if required. It assumes that you are using the basic amf protocol over http (ie REST style interfaces) but again this is easily changed.

view plain print about
1package com.classsoftware.utils
3    import mx.messaging.ChannelSet;
4    import mx.messaging.channels.AMFChannel;
5    import mx.rpc.remoting.mxml.RemoteObject;
7    public dynamic class AutoEndpointRemoteObject extends RemoteObject
8    {
9        public static const IP:String = "x.x.x.x";
10        public static const PORT:String = "xx";
11        public static const CONTEXTROOT:String = "xxxx";
13        public function AutoEndpointRemoteObject(destination:String=null)
14        {
15            super(destination);
17            var amfPath:String = "http://" + AutoEndpointRemoteObject.IP + ":" + AutoEndpointRemoteObject.PORT + "/" + AutoEndpointRemoteObject.CONTEXTROOT + "/messagebroker/amf";    
            var channelSet:ChannelSet = new ChannelSet();
19            var channel:AMFChannel = new AMFChannel("
my-amf", amfPath);
20            channelSet.addChannel(channel);
21            this.channelSet = channelSet;
22            this.endpoint = amfPath;
23        }
25    }

To use this class to call a method "myMethod" on a remote object "myObject" you would do the following.

view plain print about
1var remoteObject:AutoEndpointRemoteObject = new AutoEndpointRemoteObject("myObject")

Due to how the Flex compiler handles <mx:method>you can't use <mx:method> as a child of this class if you use it in MXML. You can only use this class via ActionScript.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)