Google App Engine全局变量(ServletContext)

Google App Engine全局变量(ServletContext)

问题描述:

我正尝试在JAVA中的GAE上构建一个聊天应用程序。我需要保持所有在线用户及其网络(某种聊天室)的数量,并且这些信息需要不断更新。
我有(错误地)假设我可以使用Java的SerlvetContext和Set / Get Attribute方法来更新online \ offline用户并与所有servlet共享这些信息。正如我已经知道的(有可爱的bug),由于GAE是分布式服务,它并没有有效地实现ServletContext.setAttribute--意味着我的应用程序可能运行在多个JVM上,并且ServletContext上的信息是共享的仅在属于相同JVM的servlet之间。

I'm trying to build a chat application on GAE in JAVA . I have the need to keep count of all online users and their networks (chat rooms of some sort) , and this info needs to be updated constantly . I have (wrongly?) assumed that I can just use Java's SerlvetContext and Set/Get Attribute methods to update online\offline users and share that information with all servlets . As I have come to know(with lovely bugs) , since GAE is distributed\cloud service , it doesn't effectively implement ServletContext.setAttribute - meaning is that my app probably runs on more than one JVM , and information on ServletContext is shared only between servlets belonging to the same JVM.

当然,这对我来说是一个巨大的问题。
几个问题 -
1)ServletContext在GAE上确实无法正常工作吗?
2)GAE对于像我这样的初学者网络开发人员来说是不是一个好选择?在我看来,我总是发现新的问题和不符合Servlet的规则。由于初学者很难学习Servlets,可能GAE不是正确的选择吗?
3)我怎样才能在Servlet之间共享信息?

This is a huge problem for me , of course . Several Questions - 1)Does ServletContext indeed won't work properly on GAE? 2)Is GAE a bad choice for beginner web developpers like myself ? It seems to me that I always find new problems and things that don't correspond with Servlet\JSP rules. Since it is hard enough for a beginner to learn Servlets , maybe GAE isn't the right choice? 3)How then can I share information between Servlets ?

如果你真的只是为了自己的目的而学习Java EE,我可能会因为你提到的原因而避免GAE 。这是一个非常好的服务,但是,是的,它有自己的一系列警告,可能会阻碍你的学习。你可能更适合为你的目的打开一个EC2实例。

If you're really just trying to learn Java EE for your own purposes I would probably avoid GAE for the reasons you mention. It's a perfectly good service, but yes, it has its own set of caveats that might get in the way of your learning. You might be better off just spinning up an EC2 instance for your purposes.

也就是说 - 你是对的,AppEngine会旋转实例来服务请求。如果你想共享状态,你应该使用在实例间共享的memcache,但是你必须管理对memcache对象的访问,以便多个用户同时写入它。

That said - you are correct, AppEngine will spin up and down instances to serve requests. If you want shared state you should use memcache which is shared across instances, but you have to manage access to the memcache objects for the possibility of multiple users writing to it at the same time.