简体   繁体   English

ActionScript和Java EE中的静态变量之间的区别

[英]Difference between static variable in ActionScript and in Java EE

Please verify my understanding. 请核实我的理解。 When a static variable is defined in a Java EE application, the variable is visible and can be altered by any other user. 在Java EE应用程序中定义静态变量时,该变量是可见的,并且可以由任何其他用户更改。 But in case of actionscript, the static variable's scope is the application running in browser's window. 但是在actionscript的情况下,静态变量的范围是在浏览器窗口中运行的应用程序。

In that case, is it possible to define a variable in actionscript to behave like the static variable in Java EE, beacause actionscript is client side and Java EE is server side ? 在这种情况下,是否可以在actionscript中定义一个变量,使其行为类似于Java EE中的静态变量,因为actionscript是客户端而Java EE是服务器端?

Your understanding of static is correct. 您对静电的理解是正确的。

But this leed to the problem, that you want to share values between several Clients. 但这导致问题,您想要在几个客户端之间共享值。 But there is no simple solution. 但是,没有简单的解决方案。 What you need is a Server, which store the value and the clients have to ask the server for this value and ask the server to update the value. 您需要的是一个服务器,它存储该值,客户端必须向服务器询问此值并要求服务器更新该值。

I guess that you need so called 'singleton' object - one object which is accessed by your whole system. 我想你需要所谓的'singleton'对象 - 一个对象可以被整个系统访问。

Even 'static' from Java won't help you here too much: there can be more than one copy of a static variable in JavaEE, for example when you use clustering, each machine in the cluster will run its own copy of the application, so will have its own copy of the 'static' variable. 即使来自Java的'静态'也不会对你有太大的帮助:JavaEE中可能有多个静态变量副本,例如当你使用集群时,集群中的每台机器都会运行自己的应用程序副本,因此将拥有自己的“静态”变量副本。 If you have (let's say) 10 clients (in Flash or Flex), and 10 clustered servers, each client may get its own server, so each client may get its own static variable on the server. 如果你有(比方说)10个客户端(在Flash或Flex中)和10个集群服务器,每个客户端可能会得到自己的服务器,因此每个客户端可能在服务器上获得自己的静态变量。

What's more, when the server is restarted, but the client is still alive, all your static variables are lost. 更重要的是,当服务器重新启动但客户端仍处于活动状态时,所有静态变量都将丢失。

The only way to make the true, system-level singleton is to use a more complex solution, like: 制作真正的系统级单例的唯一方法是使用更复杂的解决方案,例如:

  • save the 'singleton' data to some file on the server side, so all nodes in the cluster and all clients can access this file; 将“单例”数据保存到服务器端的某个文件中,因此集群中的所有节点和所有客户端都可以访问该文件;
  • save the 'singleton' data to the database; 将“单一”数据保存到数据库中;
  • use application-scoped Enterprise Java Beans; 使用应用程序范围的Enterprise Java Bean;
  • use Terracotta, which can turn 'static' variables into really static, persistent ones automatically with no changes in your code. 使用Terracotta,它可以自动将“静态”变量转换为真正的静态变量,而不会对代码进行任何更改。

You can always pass the variable to browser via Javascript or to server via socket/urlloader/javascript. 您始终可以通过Javascript将变量传递给浏览器,或者通过socket / urlloader / javascript传递给服务器。

Though to be more precise, static variable in AS3 has scope as you declare it - public, protected, internal, private - and is attached to specific class definition. 虽然更确切地说,AS3中的静态变量具有声明它的范围 - public,protected,internal,private - 并附加到特定的类定义。

You can share values via P2P mesh network with the recently added Real Time Media Flow Protocol in FP10.1. 您可以通过P2P网状网络与FP10.1中最近添加的实时媒体流协议共享值。 Otherwise you can use a remote shared object to share values between different client machines (but requires a server side remoting solutions like FMS, WebOrb, FluorineFX, AMFPHP, etc). 否则,您可以使用远程共享对象在不同客户端计算机之间共享值(但需要服务器端远程处理解决方案,如FMS,WebOrb,FluorineFX,AMFPHP等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM