0
votes

Thank you for all the documentation, and the great forum.

I have a question about the Java Factory; I've read somewhere that: "... the JavaFactory will check if there is already an instance in the scope and return that. If the object is not available then it is instantiated ...", but I don't understand the expression "if the object is not available"?

I am having trouble to find and/or assign classes already instantiated (by another class) in the server, to a declared destination.

Here is the scenario:

I'm developing an application in Java:

  • J2EE.
  • I'm running it on a Tomcat 6 server
  • I'm using BlazeDS to communicate with Flex.
  • I'm using the RPC's Remoting Service (through RemoteObjects)

There are two java classes that handle Client (Flex) interaction, say ApplicationClass and UserApplicationClass:

  • ApplicationClass has a reference (attribute) to a UserApplicationClass instance
  • ApplicationClass should be the one and only entry point (although BlazeDS requires an empty constructor) to instantiate a **User*ApplicationClass*;

UserApplicationClass is instantiated, right after the attribute User has been set on ApplicationClass.

**//Java Code**


public class ApplicationClass {
        private User user = null;
        private UserApplicationClass userApplicationClass = null;
        ...
        public ApplicationClass {}
        ...
        public void setUser(User user) {
            this.user = user;
            this.userApplicationClass = new UserApplicationClass(user);
        }
        ...
    }

Up to here everything is fine:

From the Flex client I am able to use ApplicationClass methods, set its User and get the UserApplicationClass attribute from ApplicationClass.

But here is the problem:

When I try to use any method of UserApplicationClass, It doesn't find the class already created on the server, but instead, it instantiates a brand new one (with all of its attributes set to null by using the default constructor).

How could I assure that the destination points to a specified instance on the server, created by another class, and not instantiating a new one?

**// Flex Client Code**


<mx:Application 
        ...
        creationComplete="invokeService()">
        ...
        <mx:Script>
            <![CDATA[
                import application.UserApplicationClass;
                ...
                private var userApplicationClass    :UserApplicationClass;
                ...
                private function invokeService():void
                {
                    applicationClassRemoteObject.getUser(); // Ok
                    userApplicationClass.getUser(); // Ok
                    userApplicationClassRemoteObject.getUser(); **// Fails; user null; the JavaFactory 
                                              //doesn't find the UserApplicationClass instance on the 
                                              //server and creates a new instance.**
                }
                ...
                private function applicationClass_getUser(event:ResultEvent):void
                {   
                    // Validate null ResultEvent ...
                    userApplicationClass = UserApplicationClass(event.result);
                }
                ...
            ]]>
        </mx:Script>

        <mx:RemoteObject 
            id="applicationClassRemoteObject" 
            destination="***ApplicationClass***Destination"
            showBusyCursor="true">
            ...
            <mx:method 
                name="getUserApplicationClass"
                result="applicationClass_getUser(event)"
            />      
        </mx:RemoteObject>

        <mx:RemoteObject 
            id="userApplicationClassRemoteObject" 
            destination="***UserApplicationClass***Destination"
            showBusyCursor="true">
            <mx:method 
                name="getUser"
                result="userApplicationClass_getUser(event)"
            />
            ...
        </mx:RemoteObject>
        ...
    </mx:Application>

Note: scope attribute (on remoting-config.xml) on both destinations is set to session.

I hope I have been clear; I would really appreciate if you could help me.

Thanks in advance,

AM

2

2 Answers

1
votes

JoseVelas, Even I faced the same problem as you mentioned. After googling in summary I figured out the problem with remote-config file.. If you try to give the following code in java Factory

<destination id=”my-destination”>

<properties>

<source>flex.samples.EmployeeService </source>

**<scope>application</scope>**

</properties>

</destination>

It works fine for me. The scope plays a major role here, I hope it may help you.

0
votes

You should look for implementing a Staful Service in BlazeDS. Look for available scopes for the services. e.g. you can have this ApplicationClass to be the same for all users or have one for each user.

By default, it is stateless, in that a new object is instantiated for each request. If the destination configuration contains a stateful flag, the HTTP session is searched for an existing instance of the object, so multiple requests from the same client will be invoked on a stateful component. from Class JavaAdapter

I hope these links give you the direction: http://flexuniverse.wordpress.com/2010/05/20/setting-up-gilead-for-blazeds-in-statefull-mode/ http://sujitreddyg.wordpress.com/2009/01/20/how-remoting-service-in-blazeds-works/