HomeAbout Mark van HedelContact Mark van Hedel



Blaze DS and Spring :: Part 4

Let's do the last part of our demo, this is the Flex part on the client that we want connected. First we finish the application with our logon, we logged on, and sent our username to the server, so now all we need to do is subscribe to receive tasks from the other Flex client. To do this we add a consumer tag to our code and subscribe to messages on the server.

So now we add our consumer to the source for receiving messages :

<mx:Consumer
      destination="tasks"
      id="taskConsumer" />

Next I'll place the entire actionscript for this file here.

/* ----------------------------------------------------------------------------
         EVENT HANDLER for popup display and animation
         ---------------------------------------------------------------------------- */

         private function onDisplayPopup(event:Event):void
         {
            PopUpManager.removePopUp(popupWindow);
            
            popupWindow = TitleWindow(PopUpManager.createPopUp(this, Login, true));
            
            popupWindow.width = 350;
            popupWindow.height = 350;
            popupWindow.move((this.width/2) - 175, (this.height/2) - 175);
            
            popupWindow.addEventListener("userNameChosen", userSelectedHandler);
            
            popupWindow.data = "Inloggen";
         }
         
         private function userSelectedHandler(event:ClosePopupEvent):void{
            userName = event.userName;
            taskConsumer.subscribe();
            taskConsumer.addEventListener(MessageEvent.MESSAGE, incomingMessageHandler);
         }
         
         private function incomingMessageHandler(event:MessageEvent):void {
          var newTask:Task = event.message.body.task as Task;
          myTasks.addItem(newTask);
         }

So first when we open the popup we add an event listener to listen to our custom close event and receive our username from there. Only at that point do we subscribe to the messaging destination to receive messages and we add a message listener there to listen to all messages we might receive. As soon as we receive a message we will add this to an existing ArrayCollection of tasks that we want to display. Now in the main application I've added 2 tags, a Consumer to receive user messages, this when a user comes online or goes offline. I've also added a tag to send out tasks so another producer tag.

<mx:Producer
      destination="tasks"
      id="taskProducer" />


   <mx:Consumer
      destination="taskusers"
      id="userConsumer" />

The Consumer subscribes to the same destination our Spring Service is sending the message to, so this will be received as soon as the user message is send out and our producer is using the tasks destination in the messaging-config file to send tasks there. These are the tasks received by the application on top.

Now the last part is the actionscript part of this file, so here we go.

/**
* on creation complete we listen for messages with user information from the server.
*/
private function creationCompleteHandler():void {
   userConsumer.subscribe();
   userConsumer.addEventListener(MessageEvent.MESSAGE, userMessageHandler);
   createSubTasks();
}

We handle these messages here :

/**
*   When we receive a message we add it to a user ArrayCollection or in case the user
* already in there we update the user object
*/
private function userMessageHandler(event:MessageEvent):void {
   var updateUser:User = new User();
   var userFound:Boolean = false;
   if (event.message.body == null){
      Alert.show(event.message.headers.username);      
   } else {
      updateUser = event.message.body.newUser as User;
      for (var i:int = 0;i < users.length;i++){
         if ((users[i] as User).id == updateUser.id){
            (users[i] as User).online = updateUser.online;
            (users[i] as User).userName = updateUser.userName;
            userFound = true;
         }
      }
      if (!userFound){
         users.addItem(updateUser);
      }
   }
}

Now see that we do receive a user object here, this user object is an instance of the actionscript user class that maps to the Spring class on the server. By adding a RemoteClass tag here we define what server class this Class maps to.

package vo
{
   [Bindable]
   [RemoteClass(alias="org.flexpair.flex.presentation.task.manager.core.domain.User")]
   public class User
   {
      public var id:Number;
      public var userName:String;
      public var online:Boolean;      
      
   }
}

The last part of our application is sending out tasks to the other application, we do that with messaging just like we used messaging to send messages from the server, so here we create another AsyncMessage and populate it with our data and send it to the destination we created on the server.

private function assignTaskHandler():void {
   var newTask:Task = new Task();
   newTask.subTasks = selectedSubTasks;
   newTask.summary = taskName.text;
   newTask.description = taskDescription.text;
   newTask.user = userGrid.selectedItem as User;
   var message:AsyncMessage = new AsyncMessage();
   message.headers.user = newTask.user.userName;
   message.body.task = newTask;
   taskProducer.send(message);
}

This should be all that's needed to get the application to work, if there are any questions please contact me, also if there are any mistakes in my code here just let me know, I had to make a few changes to it since J-spring.

Related Blog Entries

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner