Finally got it all figured out. You have to delete both the memcache
session entry and the datastore record. The sessions are not managed
this way othe dev server it seems, so it won't work in that context.

Here is the code:

                                     if(foundUser.getSessionId() !=
null) {

                                                MemcacheService ms =
MemcacheServiceFactory.getMemcacheService();

                                                String mkey = "_ahs" + 
foundUser.getSessionId();
                                                if(ms.contains(mkey)){
                                                        log.info("user already 
logged in sending logout.");
                                                        ms.delete(mkey);

                                                        DatastoreService ds =
DatastoreServiceFactory.getDatastoreService();
                                                        
com.google.appengine.api.datastore.Query q = new
com.google.appengine.api.datastore.Query("_ah_SESSION");
                                                        Key key = 
KeyFactory.createKey("_ah_SESSION","_ahs" +
foundUser.getSessionId());
                                                        
q.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.EQUAL,
key);

                                                        Entity e = 
ds.prepare(q).asSingleEntity();
                                                        if(e != null) {
                                                                Response r = 
new Response(RESPONSES.LOGGED_OUT);
                                                                SendMessageTask 
smt = new
SendMessageTask(pm.detachCopy(foundUser), r, false);
                                                                TaskOptions 
taskOptions = withUrl(Constants.MATCH_QUEUE);
                                                                
defer(smt,"matchQueue", taskOptions);

                                                                
com.google.appengine.api.datastore.Transaction txn =
ds.beginTransaction();
                                                                try {

                                                                        
ds.delete(key);

                                                                        
txn.commit();
                                                                } finally {
                                                                        if 
(txn.isActive()) {
                                                                                
txn.rollback();
                                                                        }
                                                                }
                                                        }
                                                }

                                        }



Here is what is going on:
* I check to see if the user found in the datastore has a session id
saved.
* I check the memcache to see if the "_ahs" + session id object exist.
* I delete the memcache object.
* I delete the entity in "_ah_SESSION".
* I send a message via a channel to the logged in device telling it to
log out. If this device is off or the browser is closed, this message
will just expire.

This is a pretty inefficient way of going about this, but I don't
expect it to happen all that often.






On Feb 27, 10:51 am, Scott <shathaw...@gmail.com> wrote:
> Thanks for the response Didier!
>
> I know I can store the session id, but how do I then retrieve the
> session object by id? I have been looking at ways to access
> _ah_SESSION entities using the low level datastore api like this:
>
>            DatastoreService ds =
> DatastoreServiceFactory.getDatastoreService();
>            com.google.appengine.api.datastore.Query q = new
> com.google.appengine.api.datastore.Query("_ah_SESSION");
>
> I can set a filter to return a single object matching a key value, but
> is the session id the key value? This is difficult to test, because it
> seems the _ah_SESSION entities are not created on the dev server.
>
> I have also read that session objects could be stored in the memcache.
> I have been trying this as well using the following code:
>
>             MemcacheService ms =
> MemcacheServiceFactory.getMemcacheService();
>             ms.get(?);
>
> I am again not certain what to use for the key value in the ms.get()
> call. I know the key is prefixed with _ahs, but I don't know the rest.
> Is this prefix appended to the session id in some way? This is also
> difficult to test on the dev server, because the sessions don't seem
> to be stored in the memcache.
>
> Does anyone know the difference in session management on the dev
> server vs the production server?
>
> Thanks,
> Scott
>
> On Feb 27, 2:15 am, Didier Durand <durand.did...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi,
>
> > Yes, the simplest way is to store the session id in the datastore.
>
> > You can get a session id via javax.servlet.http .HttpSession.getId();
>
> > regards
>
> > didier
>
> > On Feb 26, 7:51 pm, Scott <shathaw...@gmail.com> wrote:
>
> > > Howdy,
>
> > > I want to only allow my users to have a single session at a time. The
> > > act of logging in should invalidate a session that might have been
> > > created from another devices as well as sending that device a notice
> > > via the channel that the session had ended.
>
> > > I actually had this working on my dev server. I used a singleton to
> > > hold a copy of the session object and channel information, and when
> > > the user logged in from a different device, I would invalidate and
> > > send a message. The problem is that since GAE the production server is
> > > distributed, I have no guarantee of the same JVM on each call.
>
> > > Reading through docs and group post, the only possibility I could
> > > think of was to retrieve the session object from the datastore or
> > > memcache. Is it possible to read the session object from the either of
> > > these locations? If so what is the key and object type?
>
> > > Thanks a lot,
> > > Scott

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to