3

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.

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 ?

Joel Blum
  • 7,750
  • 10
  • 41
  • 60

2 Answers2

2

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.

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
  • 1)Isn't EC2 also a distributed service ? Will I not face the same problem there ? 2) Is Memcache really a solution for my problem ? Several guides say Memcache is unreliable and is suited for mostly static pages , not constantly changing ones , and that the info should be backed up in the database . Is it not 'insane' for me to constantly read and write to the database like that ? insane meaning expensive moneywise – Joel Blum Apr 18 '12 at 15:48
  • 1) No. It's not. EC2 gives you exactly the number of servers you ask for 2) Memcache is reliable for this purpose, but yes, if you want the data to persist across server restarts you need to persist it. Depending on the load of your application you're going to run into concurrency issues though. The problem with the datastore is eventual consistency, if your app is realtime you will have to wrap your puts in a transaction. How many users are you expecting on this app? – Rick Mangi Apr 18 '12 at 15:58
  • I suppose I'll go with Memcache since the whole thing is written for GAE . So to conclude , is it an ok solution to update my global data (e.g arraylist of online users) into the memcache ? my app works the same as gmail\facebook - it wants to refresh the online users (lets say refresh every minute) . that's tons of updating , I wouldn't want to have to access a db every minute. to be clear Im not really expecting anyone to use my app (I'd be delighted , but surprised) but I want to understand how to do this the right way . Thanks for your help Rick – Joel Blum Apr 18 '12 at 16:27
  • Updating every minute is fine, you don't have to really worry about concurrency until you're doing multiple updates a second. – Rick Mangi Apr 18 '12 at 17:51
  • Last question : What about using a regular class (not a servlet) with static variables for storage ? will it be shared among all servlets or spread across several JVMs? – Joel Blum Apr 19 '12 at 04:44
  • 1
    No it will be local to each instance. – Rick Mangi Apr 19 '12 at 04:46
0

In google app engine, application state is usually shared between instances using the datastore. As your requirement is more real-time and might not behave nicely using polling you should use the Channel API (perhaps in addition to the datastore):

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

Quoting from that page:

The Channel API creates a persistent connection between your application and Google servers, allowing your application to send messages to JavaScript clients in real time without the use of polling. This is useful for applications designed to update users about new information immediately.

Dave Moten
  • 11,957
  • 2
  • 40
  • 47