Giving an Actor access to S

5 views
Skip to first unread message

Bryan

unread,
May 25, 2009, 2:39:05 PM5/25/09
to Lift
How can I give an Actor access to S? I need this for adding results
to a SessionVar of a HashMap.

If I call show() directly, then I see "localhost" logged. If I call
show() from within an actor, then it logs "nowhere_123.com":
listener ! 'show.

val listener: Actor = actor {
loop {
react {
case 'show => show()
}
}
}

def show() {
Log.info("show's host name: " + S.hostName)
}

marius d.

unread,
May 25, 2009, 5:01:15 PM5/25/09
to Lift
CometActors are asynchronous components that live beyond the scope of
a given request. From a CometActor you can have access to LiftSession
or SessionVars meaning that you could potentially "store" the "last-
seen" from the last request host name in a SessionVar an then access
it anytime from your actor.

Not sure why you need the hostName from inside an actor but you know
best what application you are building.

Br's,
Marius

Timothy Perrett

unread,
May 25, 2009, 6:42:56 PM5/25/09
to Lift
My understanding was always that CometActor's "faked" S so its usage
was fairly transparent.... of course, thats specific to CometActors
and wont wash with a normal scala.actor.Actor etc

Cheers, Tim

marius d.

unread,
May 26, 2009, 1:42:01 AM5/26/09
to Lift
Well and S object is initiated but in an asynchronous operation you
wont really have request information cause you are beyond a request
scope. Some things from S maybe available at that time.

Br's,
Marius

Bryan.

unread,
May 26, 2009, 9:26:15 AM5/26/09
to Lift
Thanks for the reply, guys. I just need it to set a SessionVar.

I have these actors that fetch some data. When I instantiate my Actor
class (in an instantiated class that has access to all of S), I pass
in an anonymous function to process the data. I would like to store
these results in a SessionVar, but I am currently not able to.

These results are short lived and should expire at the end of a user's
session.

Thanks,
Bryan

marius d.

unread,
May 26, 2009, 10:00:59 AM5/26/09
to Lift
Hmmm ...

object myVar extends SessionVar("")

val listener: Actor = actor {
loop {
react {
case 'show => myCometActor ! SetMyVar("some value")
}
}

}


then have a CometActor that listens for this message and inside the
Comet actor you already have access to the LiftSession, meaning that
you can just set your session var.

If you don't like the CometActor approach you'll have to provide the
LiftSession reference to your actor ... depending of course when you
are starting your actor.

As an example:

// somewhere in a snippet or a user processing function that is
already in the context of S
...

val listener: Actor = actor {
val ownSession = S.session

loop {
react {
case 'show => S.initIfUninitted(ownSession) {
myVar("some value")
}
}
}

...

Br's,
Marius

David Pollak

unread,
May 26, 2009, 11:33:27 AM5/26/09
to lif...@googlegroups.com
As long as you have access to the LiftSession, you can initialize S and use it correctly:

case MyMessage =>
S.initIfUninitted(theSession) {
... code to execute in the actor scope
}

Thanks,

David
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Bryan.

unread,
May 26, 2009, 11:40:52 AM5/26/09
to Lift
Thanks.

--Bryan

On May 26, 11:33 am, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> As long as you have access to the LiftSession, you can initialize S and use
> it correctly:
>
> case MyMessage =>
> S.initIfUninitted(theSession) {
> ... code to execute in the actor scope
>
> }
>
> Thanks,
>
> David
>
>
>
> On Mon, May 25, 2009 at 11:39 AM, Bryan <germ...@gmail.com> wrote:
>
> > How can I give an Actor access to S?  I need this for adding results
> > to a SessionVar of a HashMap.
>
> > If I call show() directly, then I see "localhost" logged.  If I call
> > show() from within an actor, then it logs "nowhere_123.com":
> > listener ! 'show.
>
> > val listener: Actor = actor {
> >  loop {
> >    react {
> >      case 'show => show()
> >    }
> >  }
> > }
>
> > def show() {
> >  Log.info("show's host name: " + S.hostName)
> > }
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890
Reply all
Reply to author
Forward
0 new messages