Google App Engine Global Variables (ServletContext)

I am trying to create a chat application in GAE in JAVA. I have a need to keep a record of all online users and their networks (in some chats), and this information should be constantly updated. I (erroneously?) Assumed that I could just use the Java SerlvetContext and Set / Get Attribute methods to update online users \ offline users and share this information with all servlets. Since I found out (with wonderful errors), since GAE is distributed \ cloud service, it does not efficiently implement ServletContext.setAttribute - this means that my application probably runs on several JVMs, and information about ServletContext is available only for sharing between servlets, belonging to the same JVM.

This is a huge problem for me, of course. A few questions - 1) Will ServletContext really not work properly in GAE? 2) Is GAE a bad choice for novice web developers like me? It seems to me that I always find new problems and things that do not comply with the rules of Servlet \ JSP. Since it’s quite difficult for beginners to learn servlets, is GAE probably not the right choice? 3) How then can I exchange information between servlets?

+3
java-ee google-app-engine servlets
source share
2 answers

If you are really just trying to learn Java EE for your purposes, I would probably avoid GAE for the reasons you mentioned. This is a great service, but yes, it has its own set of warnings that may interfere with your learning. You might be better off just deploying an instance of EC2 for your purposes.

However, you're right, AppEngine will deploy up and down instances to serve requests. If you want to use shared state, you must use memcache, which is common to all instances, but you need to control access to memcache objects to allow multiple users to enter at the same time.

+2
source share

In the Google app engine, application state is typically used between instances using data storage. Since your requirement is larger in real time and may behave badly with polling, you should use the channel API (perhaps in addition to the data store):

https://developers.google.com/appengine/docs/java/channel/

Quote from this page:

Channel API creates a permanent connection between your applications and Google servers, allowing your application to send real-time JavaScript messages to JavaScript clients without using a poll. This is useful for applications designed to update users. about new information immediately.

0
source share

All Articles