MessageHandlers

(This tag must be placed inside an <EventMap> tag)

The MessageHandlers tag allows you to register a list of handlers as a consumer of a Flex Messaging Service. All the tags inside the MessageHandlers tag will be executed in order when a Message matching the criteria is received. This tag accepts the same attributes as the <mx:Consumer> tag.

<MessageHandlers destination="YourGateway">
   ... here what you want to happen when this message is received ...
</MessageHandlers>

The above example would be the same as doing the following:

<mx:Consumer destination="YourGateway" />

You would also need to create event handlers for the message and fault. That is not necessary when using the MessageHandlers tag. Inner tags such as MethodInvoker are executed when the message is received. If you need to handle a fault, you can use the faultHandlers inner tag. See "Handling a subscription fault".

Attributes

debug

Boolean

Whether to show debugging information for this handlers block. If true, Console output will show debugging information as the list runs.

Other attributes

See mx.messaging.Consumer for a list of attributes.

Inner tags

faultHandlers

A list of handlers to run if the subscription attempt throws an error. See "Handling a subscription fault" below.

Other allowed inner tags:

Handling a subscription fault

When the MessageHandlers tag is placed in the EventMap, it automatically subscribes to the destination provided. The subscription attempt may throw an error (ie: attempting to subscribe to a subtopic in a channel that doesn't accept subtopics). If you want to perform some action when the subscription cannot be made, you can use a <faultHandlers> inner tag. Inside that handlers list, you can use the same tags you would in the main body of an <EventHandlers> or <MessageHandlers> tag.

10 responses so far

  1. Would be nice to be able to specify clientID for subscription.
  2. For those using BlazeDS and Mate:

    Is anyone else getting a Type #1009 error from their MessageHander after switching to Flex 3.2 from Flex 3.1? The root cause is some null value in the ServerConfig.as.

    Works fine in 3.1, breaks horribly in 3.2. :/
  3. Dyre,
    Do you think it has anything to do with this?
    http://bugs.adobe.com/jira/browse/BLZ-300
  4. Yep. That's the problem exactly.

    Unfortunately, it looks like they only fixed it on trunk, rather than their release branches.

    Thanks for the link, Laura ^_^
  5. Hi,
    I am couple of weeks old doing flex :-)

    I was evaluating Mate for our team and have run into an issue wrt to MessageHandlers.... When I use this tag the application never comes up/ loads on screen. Now I know it auto subscribes. So i turned off auto subscription and it works. (I don't know how to turn it back on later so have not tested that yet).

    But point being if I use Action Script or m:consumer tag my stuff works fine. The remote channel is active and working fine. Only the MessageHandler is doing something funky... following is my code...

    <MessageHandlers destination="feed" autoSubscribe="true" debug="true">
    <MethodInvoker generator="{FeedViewManager}" method="processRecievedQuoteEvent" arguments="{resultObject}" />
    <faultHandlers>
    <MethodInvoker generator="{FeedViewManager}" method="processFault" arguments="{resultObject}"/>
    </faultHandlers>

    </MessageHandlers>

    I am not sure yet how to see application level faul;t logs...(sorry java background) tells me I should be something somewhere. If anybody can point me to right direction...much appreciated.

    Thanks,
    Vineet
  6. I got my debugger going ... The problem is same as what Dyre reported.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.


    This brings up another question.... How do I get a handle on MessageHandler from outside of MainEventMap file. (Maybe should not be done at all.) but still what would be the work around currently in Mate.... as in dont subscribe and then subscribe at a later stage?

    Cheers!
    Vineet
  7. Hi there. I ended up solving my problem by back-porting their bug fix for BLZ-300 from Flex 4.0 to my local copy of Flex 3.2. There's an NPE in ServerConfig.as on line 519 that is easily fixed.
  8. Hi folks,
    Currently i am working on producer, consumer in my project.Actually i have not yet implemented MessageHandlers and i so much curious to see the functionality of MessageHandlers, how it works like producer and consumer. so please can i have any example,which is based on MessageHandlers. It will help me lot.

    Thanks & Regard:
    Gourav Rai
  9. Vin,

    We had a similar problem where the mx:Consumer works, but Mate's MessageHandler would not.

    Apparently the MessageHandler tries to subscribe twice (!) during its initialization process after which the mx:Concumer refuses to work.

    We submitted a fix to Mate for this. Anyway, find our fixed MessageHandler here:

    /*
    Copyright 2008 Nahuel Foronda/AsFusion

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License. Y
    ou may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, s
    oftware distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License

    Author: Nahuel Foronda, Principal Architect
    nahuel at asfusion dot com

    @ignore
    */
    package com.asfusion.mate.actionLists
    {   
       import com.asfusion.mate.events.UnhandledMessageFaultEvent;
       import com.asfusion.mate.utils.debug.DebuggerUtil;
       import com.asfusion.mate.core.mate;
       
       import mx.messaging.*;
       import mx.messaging.events.MessageEvent;
       import mx.messaging.events.MessageFaultEvent;
       
       use namespace mate;
       /**
        * The <code>MessageHandlers</code> tag allows you to register a set of handlers as a consumer of a Flex Messaging Service.
        * All the tags inside the <code>MessageHandlers</code> tag will be executed in order when a <code>Message</code> matching the
        * criteria is received. This tag accepts the same attributes as the <code>Consumer</code> tag.
        */
       public class MessageHandlers extends AbstractHandlers
       {
          
          /**
           * An internal reference to the Consumer instance
           */
          protected var consumer:Consumer;
          
          /*-----------------------------------------------------------------------------------------------------------
          * Public Setters and Getters
          -------------------------------------------------------------------------------------------------------------*/
          
          /*-.........................................autoSubscribe ..........................................*/
          /**
           * If this flag is true the consumer will be automatically subscribed when the <code>MessageHandlers</code> is created.
           */
          private var _autoSubscribe:Boolean = true;
          private var _consumerSubscribedCalled:Boolean = false;
          public function get autoSubscribe ():Boolean
          {
             return _autoSubscribe;
          }
          public function set autoSubscribe (value:Boolean):void
          {
             _autoSubscribe = value;
          }
          
          /*-.........................................destination ..........................................*/
          /**
           * Provides access to the destination for the <code>MessageAgent</code>. Changing the destination will disconnect the
           * <code>MessageAgent</code> if it is currently connected.
           */
          public function get destination ():String
          {
             return consumer.destination;
          }
          public function set destination (value:String):void
          {
             consumer.destination = value;
             invalidateProperties();
             
          }
          
          /*-.........................................selector..........................................*/
          /**
           * The selector for the Consumer. This is an expression that is passed to the destination which
           * uses it to filter the messages delivered to the Consumer.
           *
           * <p>Before a call to the subscribe() method, this property can be set with no side effects.
           * After the Consumer has subscribed to its destination, changing this value has the side effect
           * of updating the Consumer's subscription to use the new selector expression immediately.</p>
           *
           * <p>The remote destination must understand the value of the selector expression.</p>
           */
          public function get selector():String
          {
             return consumer.selector;
          }
          public function set selector(value:String):void
          {
             consumer.selector = value;
             invalidateProperties();
          }
          
          
          /*-.........................................subtopic..........................................*/
          /**
           * Provides access to the subtopic for the remote destination that the MessageAgent uses.
           */
          public function get subtopic():String
          {
             return consumer.subtopic;
          }
          public function set subtopic(value:String):void
          {
             consumer.subtopic = value;
             invalidateProperties();
          }
          
          
          /*-.........................................resubscribeAttempts..........................................*/
          /**
           * The number of resubscribe attempts that the Consumer makes in the event that the destination is unavailable
           * or the connection to the destination fails. A value of -1 enables infinite attempts.
           * A value of zero disables resubscribe attempts.
           *
           * <p>Resubscribe attempts are made at a constant rate according to the resubscribe interval value.
           * When a resubscribe attempt is made if the underlying channel for the Consumer is not connected or attempting
           * to connect the channel will start a connect attempt. Subsequent Consumer resubscribe attempts that occur while
           * the underlying channel connect attempt is outstanding are effectively ignored until the outstanding
           * channel connect attempt succeeds or fails.</p>
           */
          public function get resubscribeAttempts():int
          {
             return consumer.resubscribeAttempts;
          }
          public function set resubscribeAttempts(value:int):void
          {
             consumer.resubscribeAttempts = value;
          }
          
          
          /*-.........................................resubscribeInterval..........................................*/
          /**
           * The number of milliseconds between resubscribe attempts. If a Consumer doesn't receive an acknowledgement
           * for a subscription request, it will wait the specified number of milliseconds before attempting to resubscribe.
           * Setting the value to zero disables resubscriptions.
           *
           * <p>Resubscribe attempts are made at a constant rate according to this value.
           * When a resubscribe attempt is made if the underlying channel for the Consumer is not connected or
           * attempting to connect the channel will start a connect attempt. Subsequent Consumer resubscribe attempts
           * that occur while the underlying channel connect attempt is outstanding are effectively ignored until
           * the outstanding channel connect attempt succeeds or fails.</p>
           */
          public function get resubscribeInterval():int
          {
             return consumer.resubscribeInterval;
          }
          public function set resubscribeInterval(value:int):void
          {
             consumer.resubscribeInterval = value;
          }
          
          
          /*-.........................................faultHandlers..........................................*/
          private var _faultHandlers:Array;
          /**
           *    An array of actions (IAction) contained in this fault action-list.
           * If the consumer dispatches a <code>faultEvent</code>,
           * the handlers are processed in the order in which they were added to the list.
           *
           * @default null
           * */
          public function get faultHandlers():Array
          {
             return _faultHandlers;
          }
          
          [ArrayElementType("com.asfusion.mate.actions.IAction")]
          public function set faultHandlers(value:Array):void
          {
             _faultHandlers = value;
          }
          

          /*-----------------------------------------------------------------------------------------------------------
          * Constructor
          -------------------------------------------------------------------------------------------------------------*/   
          /**
           * Constructor
           */
          public function MessageHandlers()
          {
             super();
             getConsumer();
             
          }
          
          /*-.........................................getConsumer..........................................*/
          /**
           * Returns an instance of the <code>Consumer</code> used in this <code>MessageHandlers</code>
           */
          public function getConsumer():Consumer
          {
             if(!consumer)
             {
                consumer = new Consumer();
                consumer.addEventListener(MessageEvent.MESSAGE, fireEvent,false,0,true);
                consumer.addEventListener(MessageFaultEvent.FAULT, fireFaultEvent,false,0,true);
             }
             return consumer;
          }
          
          /*-.........................................subscribe..........................................*/
          /**
           * Subscribes to the remote destination.
           */
          
          public function subscribe(clientId:String = null):void
          {
             consumer.subscribe(clientId);
          }
          
          /*-.........................................unsubscribe..........................................*/
          /**
           * Unsubscribes from the remote destination. In the case of durable JMS subscriptions,
           * this will destroy the durable subscription on the JMS server.
           */
          public function unsubscribe():void
          {
             consumer.unsubscribe();
          }
          
          /*-----------------------------------------------------------------------------------------------------------
          * Override Public methods
          -------------------------------------------------------------------------------------------------------------*/
          /*-.........................................toString..........................................*/
          /**
           * @inheritDoc
           */
          override public function errorString():String
          {
             var str:String = "Destination:"+ destination + ". Error was found in a MessageHandlers list in file "
                         + DebuggerUtil.getClassName(document);
             return str;
          }
          
          /*-.........................................clearReferences..........................................*/
          /**
           * @inheritDoc
          */
          override public function clearReferences():void
          {
             consumer.unsubscribe();
             consumer.removeEventListener(MessageEvent.MESSAGE, fireEvent);
             consumer.removeEventListener(MessageFaultEvent.FAULT, fireFaultEvent);
          }
          /*-----------------------------------------------------------------------------------------------------------
          * Protected methods
          -------------------------------------------------------------------------------------------------------------*/
          /*-.........................................fireFaultEvent..........................................*/
          /**
           * Called by the consumer when the fault event gets triggered.
           * This method creates a scope and then runs the sequence.
          */
          protected function fireFaultEvent(event:MessageFaultEvent):void
          {
             if(faultHandlers && faultHandlers.length > 0)
             {
                var currentScope:MessageScope = new MessageScope(event,debug, map, inheritedScope);
                currentScope.owner = this;
                currentScope.message = event.message;
                currentScope.currentEvent = event;
                runSequence(currentScope, faultHandlers);
             }
             else
             {
                var faultEvent:UnhandledMessageFaultEvent = new UnhandledMessageFaultEvent(UnhandledMessageFaultEvent.FAULT);
                faultEvent.faultCode = event.faultCode;
                faultEvent.faultDetail = event.faultDetail;
                faultEvent.faultString = event.faultString;
                faultEvent.message = event.message;
                faultEvent.rootCause = event.rootCause;
                dispatcher.dispatchEvent(faultEvent);
             }
          }
          
          /*-.........................................fireEvent..........................................*/
          /**
           * Called by the consumer when the message event gets triggered.
           * This method creates a scope and then runs the sequence.
          */
          protected function fireEvent(event:MessageEvent):void
          {
             var currentScope:MessageScope = new MessageScope(event,debug, map, inheritedScope);
             currentScope.owner = this;
             currentScope.message = event.message;
             currentScope.currentEvent = event;
             runSequence(currentScope, actions);
          }
          /*-----------------------------------------------------------------------------------------------------------
          * Override Protected methods
          -------------------------------------------------------------------------------------------------------------*/
          
          /*-.........................................commitProperties..........................................*/
          /**
           * Processes the properties set on the component.
          */
          override protected function commitProperties():void
          {
             if(autoSubscribe)
             {
                if (!_consumerSubscribedCalled)
                {
                   consumer.subscribe();
                   _consumerSubscribedCalled = true;
                }
             }
          }
       }
    }
  10. Newbie question, I understand MessageHandler is the Mate equivalent to the Consumer tag but what is the Mate equivalent to the Producer tag?

Leave a response

If you need help or want to comment on something not related to this page, please post in the forums. Thanks!

Leave this field empty