弹性+ Java + BlazeDS:Flex 无法使用服务器上已创建的一些 Java 类实例!

发布于 2024-10-16 17:47:37 字数 3768 浏览 4 评论 0原文

感谢提供的所有文档和精彩的论坛。

我有一个关于 Java 工厂的问题;我在某处读到:“... JavaFactory 将检查范围内是否已经有一个实例并返回该实例。如果该对象不可用,则将其实例化...”,但我不明白表达式“如果对象不可用”?

我无法找到和/或将服务器中已实例化(由另一个类)的类分配到声明的目的地。

以下是场景

我正在使用 Java 开发一个应用程序:

  • J2EE。
  • 我在 Tomcat 6 上运行它 服务器
  • 我使用 BlazeDS 进行通信的 与Flex。
  • 我正在使用 RPC 的远程处理 服务(通过 RemoteObjects)

两个 java 类处理客户端 (Flex) 交互,例如 ApplicationClassUserApplicationClass

  • >ApplicationClass 有一个参考 (属性)到 UserApplicationClass 实例
  • ApplicationClass 应该是 唯一的入口点(尽管 BlazeDS 需要一个空的 构造函数)来实例化一个 **用户*ApplicationClass*;

UserApplicationClass 在 ApplicationClass 上设置 User 属性后立即实例化。

**//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);
        }
        ...
    }

到目前为止,一切都很好:

从 Flex 客户端,我可以使用 ApplicationClass 方法,设置其 User 并从 ApplicationClass 获取 UserApplicationClass 属性。

但是问题在于

当我尝试使用UserApplicationClass的任何方法时,它找不到服务器上已创建的类<​​/strong>强>,但相反,它实例化一个全新的(通过使用默认构造函数将其所有属性设置为 null)。

如何确保目标指向服务器上由另一个类创建的指定实例,而不是实例化新实例?

**// 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>

注意:范围属性(在远程处理上)两个目标上的 config.xml)都设置为会话。

我希望我已经说清楚了;如果您能帮助我,我将不胜感激。

提前致谢,

AM

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2024-10-23 17:47:37

JoseVelas,即使我也遇到了你提到的同样的问题。经过谷歌搜索总结后,我发现了远程配置文件的问题。如果您尝试在 java Factory 中给出以下代码,

<destination id=”my-destination”>

<properties>

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

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

</properties>

</destination>

它对我来说效果很好。范围在这里起着重要作用,希望它可以对您有所帮助。

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.

淡紫姑娘! 2024-10-23 17:47:37

您应该寻求在 BlazeDS 中实现 Staful 服务。寻找服务的可用范围。例如,您可以让所有用户都使用相同的应用程序类,或者为每个用户使用一个应用程序类。

默认情况下,它是无状态的,因为每个请求都会实例化一个新对象。如果目标配置包含有状态标志,则会在 HTTP 会话中搜索该对象的现有实例,因此将在有状态组件上调用来自同一客户端的多个请求。 来自 JavaAdapter 类

我希望 这些链接为您提供方向:
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/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文