If you are using Flex Remoting to communicate with Java classes on the server, you might want to maintain instance of an object across a user session. For example you might want to store the User object after successful authentication of a user. BlazeDS provides class named FlexContext.
Please visit the BlazeDS Developer guide at this URL for more details on FlexContext class and session data management. BlazeDS Developer Guide
Updated: Free AIR based Tool to generate Flex code for consuming/exposing Java classes as BlazeDS Remoting services. Visit this URL for more details http://sujitreddyg.wordpress.com/2009/05/07/blazemonster/
I created a simple Java class which will add objects to session. It will also retrieve objects from session and modify them. I access this Java class from a Flex application to get the data which is stored in user session. In this sample we will be creating one Java class and one .mxml file.
Pre-requisite: Knowledge on how to invoke Java methods from Flex applications and also have BlazeDS set up on your server. If you have not done this, please visit this URL for details on how to access Java methods from Flex applications. Invoking Java methods from Flex applications
Let’s move into details. First create a Java class as shown below.
Download this file from this URL: MySessionHandler.java
In the constructor I am checking if object already exists in the session and add objects if necessary. We have two objects in the session. One is userName which is object of type java.lang.String and myCounter which is object of type java.lang.Integer and is used to store a counter. We will provide method to increment this counter.
if(mySession.getAttribute(“myCounter”) == null)
mySession.setAttribute(“myCounter”, new Integer(1));
if(mySession.getAttribute(“userName”) == null)
mySession.setAttribute(“userName”, “Sujit Reddy G”);
In this method we are incrementing the value of the object stored in the session.
public void increaseCounter()
Integer i = (Integer) mySession.getAttribute(“myCounter”);
i = i + 1;
You can find more methods in Java file attached. Download the file and place the compiled file into WEB-INF/classes folder of the web application where your BlazeDS is present. You should add flex-messaging-core.jar to your project build path in Eclipse to compile this class.
You should add a destination to your remoting-config.xml. I have added the following under the service tag in my remoting-config.xml.
<destination id=”MySessionHandler” channels=”my-amf”>
Download this file from this URL: SessionSample.mxml
In this Flex application we have three buttons which will invoke methods on the Java class created above to get the results. There are three methods which we will invoke.
getUserName() – this will return the user name stored in the session
getCounter() – this will return the value of the counter
increaseCounter() – this will increment the counter value
You have to map your Flex application to the web application with BlazeDS as described in the pre-requisite for this blog.
That is all you need to do. It’s that easy to manage session data while using Flex Remoting.